OfficeParse将doc docx xls ppt转换为pdf html htm,别超长就好

Shaka 5月前 ⋅ 368 阅读
public static synchronized String parseOffice(File file, String toFilePath, String fileName, String type) throws IOException {
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(file);
String timesuffix = fileName.substring(0, fileName.indexOf("."));//截取相同文件名
String postfix = fileName.substring(fileName.lastIndexOf(".") + 1);//截取文件后缀
String docFileName = null;
String pdfFileName = null;
String hashCode = file.hashCode() + "";
String lastModified = file.lastModified() + "";
if ("doc".equals(postfix)) {
docFileName = "doc_" + timesuffix + hashCode + hashCode + ".doc";
pdfFileName = "doc_" + timesuffix + hashCode + "." + type;
} else if ("docx".equals(postfix)) {
docFileName = "docx_" + timesuffix + hashCode + ".docx";
pdfFileName = "docx_" + timesuffix + hashCode + "." + type;
} else if ("xls".equals(postfix)) {
docFileName = "xls_" + timesuffix + hashCode + ".xls";
pdfFileName = "xls_" + timesuffix + hashCode + "." + type;
} else if ("xlsx".equals(postfix)) {
docFileName = "xlsx_" + timesuffix + hashCode + ".xlsx";
pdfFileName = "xlsx_" + timesuffix + hashCode + "." + type;
} else if ("ppt".equals(postfix)) {
docFileName = "ppt_" + timesuffix + hashCode + ".ppt";
pdfFileName = "ppt_" + timesuffix + hashCode + "." + type;
} else if ("pdf".equals(postfix)) {
docFileName = "pdf_" + timesuffix + hashCode + ".pdf";
pdfFileName = "pdf_" + timesuffix + hashCode + "." + type;
} else if ("jpg".equals(postfix)) {
docFileName = "jpg_" + timesuffix + hashCode + ".jpg";
pdfFileName = "jpg_" + timesuffix + hashCode + "." + type;
} else if ("png".equals(postfix)) {
docFileName = "png_" + timesuffix + hashCode + ".png";
pdfFileName = "png_" + timesuffix + hashCode + "." + type;
} else if ("gif".equals(postfix)) {
docFileName = "gif_" + timesuffix + hashCode + ".gif";
pdfFileName = "gif_" + timesuffix + hashCode + "." + type;
} else if ("txt".equals(postfix)) {
docFileName = "txt_" + timesuffix + hashCode + ".txt";
pdfFileName = "txt_" + timesuffix + hashCode + "." + type;
} else {
return null;
}
File pdfOutputFile = new File(toFilePath + File.separatorChar + pdfFileName);
File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
if (!new File(toFilePath).exists()) {
docInputFile.getParentFile().mkdirs();
}

//如果文件存在并且未修改直接返回
if (pdfOutputFile.exists()) {
Cache cache = CacheManager.getCacheInfo("office"); //取出保存文件最后修改时间的缓存
if (null != cache) {
Map<String, String> md = (Map<String, String>) cache.getValue();
if (StringUtils.isNotBlank(md.get(hashCode)) && StringUtils.isNotBlank(lastModified)) {
if (!md.get(hashCode).equals(lastModified)) {
pdfOutputFile.delete();
} else { //通过hashCode获取创建缓存时的修改时间对比文件
return pdfFileName; //存在不转换直接返回
}
}
}
}
if (docInputFile.exists()) {
docInputFile.delete();
}
docInputFile.createNewFile();
/**
* 由fromFileInputStream构建输入文件
*/
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}

os.close();
fileInputStream.close();

} catch (IOException e) {
logger.info(e.getMessage());
e.getStackTrace();
}

if ("pdf".equals(postfix) || "html".equals(postfix)) {
return docFileName;
}

// 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
if (Constants.LibreOffice_HOME.charAt(Constants.LibreOffice_HOME.length() - 1) != '\\') {
Constants.LibreOffice_HOME += "\\";
}
// 启动OpenOffice的服务
String command = Constants.LibreOffice_HOME + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
Process pro = Runtime.getRuntime().exec(command);


// 此类在jodconverter中3版本中存在,在2.2.2版本中不存在
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
// libreOffice的安装目录
configuration.setOfficeHome(new File(Constants.LibreOffice_HOME));
// 端口号
configuration.setPortNumber(8100);
OfficeManager officeManager = configuration.buildOfficeManager();
officeManager.start();
logger.info("...start.....");
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
try {
// convert
converter.convert(docInputFile, pdfOutputFile);
//断开连接
officeManager.stop();
} catch (Exception e) {
logger.info(e.getMessage());
e.printStackTrace();
} finally {
// 关闭LibreOffice服务的进程
pro.getInputStream().close();
pro.getOutputStream().close();
pro.destroy();
// 转换完之后删除word文件
docInputFile.delete();
}
Cache cache = CacheManager.getCacheInfo("office");
if (null != cache) {
Map<String, String> map = (Map<String, String>) cache.getValue();
map.put(hashCode, file.lastModified() + "");
cache.setValue(map);
cache.setTimeOut(new Date().getTime());
} else {
Map<String, String> md = new HashMap<String, String>();
md.put(hashCode, file.lastModified() + "");
cache = new Cache();
cache.setKey("office");
cache.setValue(md);
cache.setTimeOut(new Date().getTime());
}
CacheManager.putCache("office", cache);
return pdfFileName;
}

注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: