需求:将上传的word、excel、ppt生成可以预览的html文件(服务端Linux)
在做这个需求以前,调研过很多方法,后来决定使用openoffice。在开发过程中,遇到一个棘手的问题,soffice的jar包和项目现有的jar包冲突。于是决定将soffice转换的main方法打包成一个单独的jar包,然后在转换的代码中调用这个jar包。不多说了,上代码:
转换的jar包代码:
package com.xyzq;
import java.io.File;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
public class JodConverter {
public static void main(String[] args) {
String sourceFilePath = args[0];
String destFilePath = args[1];
File inputFile = new File(sourceFilePath);
File outputFile = new File(destFilePath);
int retVal = 0;
OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);
try {
con.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(con);
converter.convert(inputFile, outputFile);
} catch (Exception e) {
retVal = 1;
}
finally{
if(con != null){
con.disconnect();
con = null;
}
System.out.println(retVal);
}
}
}
转换的代码:
/**
* convert office file to html file
* @param appPath
* @param sourceFilePath
* @param destFilePath
* @return status, succeed if startwith 0
* @throws Exception
*/
public String convert(String appPath, String sourceFilePath, String destFilePath) throws Exception{
String cmd = "java -jar " + appPath + " " + sourceFilePath + " " + destFilePath;
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = "", retStr = "";
while((s = br.readLine()) != null){
retStr += s;
}
process.waitFor();
retStr = process.exitValue() + retStr;
return retStr;
}
public boolean hasSofficeProcess(){
String cmd = "cmd.exe /c tasklist";
String[] linuxCmd = new String[]{"/bin/sh","-c","ps aux|grep soffice"};
try{
Process process = null;
if(getOSName().toLowerCase().contains("linux")){
process = Runtime.getRuntime().exec(linuxCmd);
}else{
process = Runtime.getRuntime().exec(cmd);
}
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = "";
while((s=br.readLine()) != null){
if(s.contains("soffice.bin")){
return true;
}
}
return false;
}catch(Exception e){
return false;
}
}
/*-----------------------文件转换----------------------*/
if(".ppt".equals(type)||".pptx".equals(type)||".doc".equals(type)||".docx".equals(type)||".xls".equals(type)||".xlsx".equals(type)){
String sourceFilePath = inv.getServletContext().getRealPath("/") + "fileUpload/" + filename;
String destFolderPath = inv.getServletContext().getRealPath("/") + "fileUpload/" + time;
UploadFileTool.mkdirs(destFolderPath);
String destFilePath = destFolderPath + "/"+ time +".html";
String appPath = inv.getServletContext().getRealPath("/") + "fileUpload/JodConverter.jar";
ProcessUtil pu = new ProcessUtil();
if(pu.hasSofficeProcess()){
//进程存在,执行转换
System.out.println("--begin file convert--");
String convResult = pu.convert(appPath, sourceFilePath, destFilePath);
if(convResult.trim().equals("00")){
//转换成功
System.out.println("------------>convert succeed:"+convResult);
}else{
//转换失败
System.out.println("------------>convert failed:"+convResult);
if(pu.hasSofficeProcess()){
String flagFile = inv.getServletContext().getRealPath("/") + "fileUpload/flag";
File ff = new File(flagFile);
ff.createNewFile();
}
deleteFiles(path,filename);
inv.getRequest().setAttribute("fileMax", "文件转换失败,请稍候重试!");
return "fileUpload";
}
}else{
System.out.println("------------>soffice process not exist");
//进程不存在,终止转换,删除文件,flag标记
String flagFile = inv.getServletContext().getRealPath("/") + "fileUpload/flag";
File ff = new File(flagFile);
ff.createNewFile();
deleteFiles(path,filename);
inv.getRequest().setAttribute("fileMax", "文件转换失败,请稍候重试!");
return "fileUpload";
}
}else{
System.out.println("---------什么都不用转换---------");
}
/*---------------------------------------------------*/