Spring 集成OpenOffice分析

符号 阅读:166 2021-03-31 18:15:05 评论:0

第一步:openoffice jar包依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
  <modelVersion>4.0.0</modelVersion> 
  <parent> 
    <groupId>com.digipower</groupId> 
    <artifactId>digipower-ucas-core</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
  </parent> 
  <artifactId>digipower-ucas-core-openoffice</artifactId> 
   
  <!-- 集中定义管理依赖版本号 --> 
	<properties> 
		<jodconverter.version>2.2.1</jodconverter.version> 
		<jodconverter-core.version>1.0.5</jodconverter-core.version> 
	</properties> 
 
	 
	<dependencies> 
		<dependency> 
			<groupId>com.digipower</groupId> 
			<artifactId>digipower-ucas-core-common</artifactId> 
			<version>0.0.1-SNAPSHOT</version> 
		</dependency> 
		<!--openoffice 依赖jar包 --> 
		 <dependency> 
            <groupId>com.artofsolving</groupId> 
            <artifactId>jodconverter</artifactId> 
            <version>2.2.1</version> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/com.github.livesense/jodconverter-core --> 
        <dependency> 
            <groupId>com.github.livesense</groupId> 
            <artifactId>jodconverter-core</artifactId> 
            <version>1.0.5</version> 
        </dependency> 
	</dependencies> 
   
   
</project>

spring 集成openoffice 核心代码:

package com.****.ucas.openoffice.util; 
 
import java.io.File; 
import java.io.IOException; 
import org.artofsolving.jodconverter.OfficeDocumentConverter; 
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; 
import org.artofsolving.jodconverter.office.OfficeManager; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import com.****.ucas.common.exception.OpenOfficeException; 
 
/** 
 *  
 * @ClassName:  OpentOfficeUtil    
 * @Description: openoffice pdf 转换工具    
 * @author: **** -zzg 
 * @date:   2019年4月26日 上午9:39:11    
 *      
 * @Copyright: 2019 www.digipower.cn  
 * 注意:本内容仅限于深圳市****科技开发有限公司内部使用,禁止用于其他的商业目的 
 */ 
public class OpentOfficeUtil { 
	// 日志记录功能 
	private final static Logger logger = LoggerFactory.getLogger(OpentOfficeUtil.class); 
	 
	private static OfficeManager officeManager; 
    private static String OFFICE_HONE = "C:/Program Files (x86)/OpenOffice 4/"; 
    private static int port[] = { 8100 }; 
     
     
    // set 和   get 方法 
    public static String getOFFICE_HONE() { 
		return OFFICE_HONE; 
	} 
 
	public static void setOFFICE_HONE(String oFFICE_HONE) { 
		OFFICE_HONE = oFFICE_HONE; 
	} 
 
	public static int[] getPort() { 
		return port; 
	} 
 
	public static void setPort(int[] port) { 
		OpentOfficeUtil.port = port; 
	} 
 
	// 打开服务器 
    public static void startService() { 
        DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); 
        try { 
            System.out.println("准备启动服务...."); 
            configuration.setOfficeHome(OFFICE_HONE);// 设置OpenOffice.org安装目录 
            configuration.setPortNumbers(port); // 设置转换端口,默认为8100 
            configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 设置任务执行超时为5分钟 
            configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时 
 
            officeManager = configuration.buildOfficeManager(); 
            officeManager.start(); // 启动服务 
            System.out.println("office转换服务启动成功!"); 
        } catch (Exception e) { 
        	logger.error(e.getMessage(), e); 
        	throw new OpenOfficeException(e.getMessage(), e); 
        } 
    } 
     
    // 关闭服务器 
    public static void stopService() { 
        System.out.println("关闭office转换服务...."); 
        if (officeManager != null) { 
            officeManager.stop(); 
        } 
        System.out.println("关闭office转换成功!"); 
    } 
     
     
    public void convertToPDF(String inputPath, String outputPath){ 
    	 File inputFile = new File(inputPath); 
    	 File outputFile = new File(outputPath); 
    	 convertToPDF(inputFile,outputFile); 
    } 
     
     
    public void convertToPDF(File inputFile, File outputFile){ 
        
		 // 判断inputFile文件存在 
		 if(inputFile.exists()){ 
			 if(!outputFile.exists()){ 
				 try { 
					outputFile.createNewFile(); 
				} catch (IOException e) { 
					// TODO Auto-generated catch block 
					logger.error(e.getMessage(), e); 
					throw new OpenOfficeException(e.getMessage(),e); 
				} 
			 } 
			 // 开始文件转换 
			 startService(); 
			 System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile); 
			  
			 OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); 
			 converter.convert(inputFile, outputFile); 
			 stopService(); 
			 System.out.println(); 
		 } else { 
			 // inputFile文件不存在 
			 throw new OpenOfficeException("inputFile 文件不存在"); 
		 } 
   } 
} 

异常类:

package com.****.ucas.common.exception; 
 
import org.apache.commons.lang.StringUtils; 
 
/** 
 *  
 * @ClassName:  BaseRuntimeException    
 * @Description: 基础异常封装--BaseRuntimeException  
 * @author:**** -zzg 
 * @date:   2019年4月22日 下午2:39:18    
 *      
 * @Copyright: 2019 www.digipower.cn  
 * 注意:本内容仅限于深圳市****科技开发有限公司内部使用,禁止用于其他的商业目的 
 */ 
public class BaseRuntimeException extends RuntimeException { 
	/**    
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)    
	 */    
	private static final long serialVersionUID = 1L; 
	 
	 /** The code. */ 
    private String code; 
 
    /** The params. */ 
    private String[] params; 
 
    /** 
     * Gets the code. 
     *  
     * @return the code 
     */ 
    public String getCode() { 
        return code; 
    } 
 
    /** 
     * Sets the code. 
     *  
     * @param code 
     *            the new code 
     */ 
    public void setCode(String code) { 
        this.code = code; 
    } 
 
    /** 
     * Gets the params. 
     *  
     * @return the params 
     */ 
    public String[] getParams() { 
        return params; 
    } 
 
    /** 
     * Sets the params. 
     *  
     * @param params 
     *            the new params 
     */ 
    public void setParams(String[] params) { 
        this.params = params; 
    } 
 
    /** 
     * Instantiates a new base runtime exception. 
     */ 
    public BaseRuntimeException() { 
        super(); 
    } 
 
    /** 
     * Instantiates a new base runtime exception. 
     *  
     * @param message 
     *            the message 
     * @param e 
     *            the e 
     */ 
    public BaseRuntimeException(String message, Exception e) { 
        super(message, e); 
    } 
 
    /** 
     * Instantiates a new base runtime exception. 
     *  
     * @param message 
     *            the message 
     */ 
    public BaseRuntimeException(String message) { 
        super(message); 
    } 
 
    /** 
     * Instantiates a new base runtime exception. 
     *  
     * @param e 
     *            the e 
     */ 
    public BaseRuntimeException(Exception e) { 
        super(e); 
    } 
 
    /** 
     * Instantiates a new base runtime exception. 
     *  
     * @param code 
     *            the code 
     * @param params 
     *            the params 
     */ 
    public BaseRuntimeException(String code, String[] params) { 
        super(code); 
        this.setCode(code); 
        this.setParams(params); 
    } 
 
    /** 
     * Instantiates a new base runtime exception. 
     *  
     * @param code 
     *            the code 
     * @param params 
     *            the params 
     * @param e 
     *            the e 
     */ 
    public BaseRuntimeException(String code, String[] params, Exception e) { 
        super(e); 
        this.setCode(code); 
        this.setParams(params); 
    } 
 
    /* 
     * (non-Javadoc) 
     *  
     * @see java.lang.Throwable#getMessage() 
     */ 
    @Override 
    public String getMessage() { 
        if (code == null || code.length() == 0) { 
            return super.getMessage(); 
        } 
         
        String paramsStr = "NA"; 
        if (params != null) { 
            paramsStr = StringUtils.join(params, ","); 
        } 
        String codeMessage = "code:" + code + ";parameters:" + paramsStr; 
        return codeMessage; 
    } 
 
    /* 
     * (non-Javadoc) 
     *  
     * @see java.lang.Throwable#toString() 
     */ 
    @Override 
    public String toString() { 
        String s = getClass().getName(); 
        String message = this.getMessage(); 
        return (message != null) ? (s + ": " + message) : s; 
    } 
 
} 
package com.digipower.ucas.common.exception; 
 
public class OpenOfficeException extends BaseRuntimeException { 
 
	/**    
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)    
	 */    
	private static final long serialVersionUID = 1L; 
	 
	public OpenOfficeException(String message,Exception e){ 
		super(message,e); 
	} 
	 
	public OpenOfficeException(String message){ 
		super(message); 
	} 
 
} 

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号