/** * Gets the MIME type for the specified file name * * @param filename a file name * @return MIME type */ public static String getMimeType(String filename) { File file = new File(filename); if (file.exists()) { return getMimeType(file); } String mimeType = identifier.identify(null, filename, null); if (mimeType == null) { mimeType = DEFAULT_MIME_TYPE; } return mimeType; }
/** * Gets the MIME type for the specified file * * @param file a file * @return MIME type */ public static String getMimeType(File file) { InputStream in = null; try { in = new FileInputStream(file); byte[] inBytes = IOUtil.readBytes(in, identifier .getMinArrayLength()); in.close(); return identifier.identify(inBytes, file.getName(), null); } catch (IOException ioe) { log.warn("Failed to detect MIME type (File): {}", toPrintable(ioe)); } finally { if (in != null) { try { in.close(); } catch (IOException ioe) { } } } return DEFAULT_MIME_TYPE; }
/** * Sets the input stream to access the content for this payload. Note this * stores the stream into memory, proper Payload implementations should * override this method. * * @param in the content input stream */ public void setInputStream(InputStream in) { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { IOUtils.copy(in, out); ramStore = out.toByteArray(); setContentType(MimeTypeUtil.getMimeType(ramStore, getId())); } catch (Exception e) { log.error("Failed to copy content to memory", e); } finally { try { in.close(); } catch (IOException ioe) { log.error("Failed to close content input stream", ioe); } } }
/** * Gets the MIME type for the specified input stream * * @param in an input stream * @return MIME type */ public static String getMimeType(InputStream in) { try { byte[] inBytes = IOUtil.readBytes(in, identifier .getMinArrayLength()); in.close(); return identifier.identify(inBytes, null, null); } catch (IOException ioe) { log.warn("Failed to detect MIME type (InputStream): {}", toPrintable(ioe)); } return DEFAULT_MIME_TYPE; }
/** * Creates an file based payload * * @param id an identifier * @param payloadFile the file for the payload */ public GenericPayload(String id, File payloadFile) { setId(id); setLabel(payloadFile.getPath()); setContentType(MimeTypeUtil.getMimeType(payloadFile)); try { setInputStream(new FileInputStream(payloadFile)); } catch (IOException e) { log.error("Error accessing input stream during payload creation", e); } metaChanged = false; }
File linkFile = new File(linkPath); if (linkFile.exists()) { setContentType(MimeTypeUtil.getMimeType(linkFile)); } else { log.debug("Linked file '{}' no longer exists!", setContentType(MimeTypeUtil.getMimeType(dataFile));
setContentType(MimeTypeUtil.getMimeType(dataFile));
setContentType(MimeTypeUtil.getMimeType(bytes, getId()));