1. 定义文件枚举类
/** * 文件类型枚举类 * */ 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. 定义文件类型的工具类,来判断文件类型
/** * 文件类型判断类 */ 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; }
时间: 2024-11-07 00:17:26