soffice实现的文件上传转换

本文介绍了一个在Linux服务端利用openoffice的soffice组件,将上传的Word、Excel、PPT文件转换为预览用的HTML文件的需求。在解决soffice jar包冲突问题上,作者选择将转换功能打包成独立的jar,以避免与其他项目库的冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:将上传的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("---------什么都不用转换---------");
			}
			/*---------------------------------------------------*/


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值