常用文件转换公共方法

/**
* 文件转pdf字节数组
* @param file
* @return
* @throws IOException
*/
private static byte[] transform(File file) throws IOException {
String name = file.getName().toLowerCase();
if (name.endsWith(".pdf")) {
return FileUtils.readFileToByteArray(file);
} else if (name.endsWith(".xls")) {
byte[] excelData = FileUtils.readFileToByteArray(file);
return FileTranslateTools.getInstance().excelToPdf(excelData, false);
} else if (name.endsWith(".doc")) {
WordToPdfUtils utils = new WordToPdfUtils();
return utils.wordToPdf(FileUtils.openInputStream(file));
} else {
throw new AppException(file.getName() + "文件类型不正确,必须上传 pdf、xls、doc中的一种。");
}
}

/**
* 将字节写入pdf临时文件
*
* @param other1Byte
* @return
* @throws IOException
*/
private static File tFile(byte[] other1Byte) throws IOException {
String dir = System.getProperty("java.io.tmpdir");
File f = new File(FilenameUtils.concat(dir, System.currentTimeMillis() + ".pdf"));//默认的临时文件路径
FileUtils.writeByteArrayToFile(f, other1Byte);
return f;
}

原文地址:https://www.cnblogs.com/tiansan/p/7346049.html