1. 定義文件枚舉類java
/** * 文件類型枚舉類 * */ public enum FileType { /** * JEPG. */ JPEG("FFD8FF"), /** * PNG. */ PNG("89504E47"), /** * GIF. */ GIF("47494638"), /** * TIFF. */ TIFF("49492A00"), /** * TIF. */ TIF("49492A00"), /** * Windows Bitmap. */ BMP("424D"), /** * Rich Text Format. */ RTF("7B5C727466"), /** * XML. */ XML("3C3F786D6C"), /** * HTML. */ HTML("68746D6C3E"), /** * Adobe Acrobat. */ PDF("255044462D312E"), /** * ZIP Archive. */ ZIP("504B0304"), /** * RAR Archive. */ RAR("52617221"), /** * Wave. */ WAV("57415645"), /** * AVI. */ AVI("41564920"); private String value = ""; private FileType(String value) { this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
2. 定義文件類型的工具類,來判斷文件類型app
/** * 文件類型判斷類 */ public final class FileTypeJudge { /** * Constructor */ private FileTypeJudge() {} /** * 將文件頭轉換成16進制字符串 * * @param 原生byte * @return 16進制字符串 */ private static String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } /** * 獲得文件頭 * * @param filePath 文件路徑 * @return 文件頭 * @throws IOException */ private static String getFileContent(String filePath) throws IOException { byte[] b = new byte[28]; InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); inputStream.read(b, 0, 28); } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); throw e; } } } return bytesToHexString(b); } /** * 判斷文件類型 * * @param filePath 文件路徑 * @return 文件類型 */ public static FileType getType(String filePath) throws IOException { String fileHead = getFileContent(filePath); if (fileHead == null || fileHead.length() == 0) { return null; } fileHead = fileHead.toUpperCase(); FileType[] fileTypes = FileType.values(); for (FileType type : fileTypes) { if (fileHead.startsWith(type.getValue())) { return type; } } return null; } }
3. 在使用jai jar包調用時,先判斷類型,再根據具體類型傳遞具體參數工具
/*** * 將單個TIF轉換爲多個JPEG文件 * @param srcFile * @param destFile * @return */ private static List<String> convertMTifToMJPEG(String srcFile, String destFile) { List<String> jpegFiles = new ArrayList<String>() ; try{ FileSeekableStream ss = new FileSeekableStream(srcFile); TIFFDecodeParam param0 = null; TIFFEncodeParam param = new TIFFEncodeParam(); JPEGEncodeParam param1 = new JPEGEncodeParam(); FileType fileType = FileTypeJudge.getType(srcFile); ImageDecoder dec = ImageCodec.createImageDecoder(fileType.toString().toLowerCase(), ss, param0); int count = dec.getNumPages(); param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4); param.setLittleEndian(false); for (int i = 0; i < count; i++) { RenderedImage page = dec.decodeAsRenderedImage(i); File f = new File(destFile + i + DOT + WaterMarkDownloadFileType.JPEG.name().toLowerCase()); ParameterBlock pb = new ParameterBlock(); pb.addSource(page); pb.add(f.toString()); pb.add(WaterMarkDownloadFileType.JPEG.name()); pb.add(param1); RenderedOp r = JAI.create("filestore",pb); r.dispose(); jpegFiles.add(destFile + i + DOT + WaterMarkDownloadFileType.JPEG.name().toLowerCase()) ; } } catch (IOException e) { e.printStackTrace(); } return jpegFiles; }