zoukankan      html  css  js  c++  java
  • spring纯注解+libreoffice

    spring.version为4.3.10.RELEASE

    aspectjweaver.version为1.8.10

    libreoffice.version为5.2.0(juh、jurt、ridl、unoil四个相关jar包)

    libreoffice核心包jodconverter-core为3.2-xwiki-SNAPSHOT

    一、相关配置类 AppConfiguration

    @Configuration //相当于beans.xml
    @ComponentScan({"com.jingluu.converter.doc"}) //相当于 <context:component-scan base-package="com.jingluu.converter.doc"/>
    @PropertySource({"classpath:converter.properties"}) //相当于 <context:property-placeholder location="classPath:converter.properties" />

    二、web初始化加载 AppWebInitializer implements WebApplicationInitializer

         @Override
    	public void onStartup(ServletContext servletContext) throws ServletException {
    		//注解配置web.xml上下文
    		AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
    		//注册配置类
    		/*
    		 * 相当于加载beans.xml
    		 *  <context-param>
    		 *    	<param-name>contextConfigLocation</param-name>
    		 *    	<param-value>classpath:spring/applicationContext.xml</param-value>
    		 *  </context-param>
    		 */
    		rootAppContext.register(AppConfiguration.class); 
    		//设置ServletContext
    		/*
    		 * servlet上下文,相当于web.xml
    		 */
    		rootAppContext.setServletContext(servletContext);
    		
    		//添加监听器
    		/*
    		 * 相当于添加一个上下文监听
    		 * <listener>
    		 *      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    		 * </listener>
    		 */
    		ContextLoaderListener contextLoaderListener = new ContextLoaderListener(rootAppContext);
    		/*
    		 * 将配置的一个监听的机制存放到web.xml中
    		 */
    		servletContext.addListener(contextLoaderListener);
    	}
    

    三、ConverterProperties (获取我们在配置文件中内容)

    @Component
    public class ConverterProperties {
    	
    	@Value("${converter.office.home}")
    	private String officeHome;
    	
    	@Value("${converter.office.ports}")
    	private String officePorts;
    
    	public String getOfficeHome() {
    		return officeHome;
    	}
    
    	public String getOfficePorts() {
    		return officePorts;
    	}
    	
    }
    

    四、DocConverter 和 DocConverterImpl

    public interface DocConverter {
    	/**
    	 * 文本转换
    	 * @param from 初始文件路径
    	 * @param to 传出文件路径
    	 */
    	void converter(String from,String to);
    	
    	/**
    	 * 文本转换
    	 * @param from 
    	 * @param to
    	 */
    	void converter(File from,File to);
    
    }
    
    /**
     * office文档转换实现类
     * @author kif
     *
     */
    @Component
    public class DocConverterImpl implements DocConverter{
    	
    	@Autowired
    	private ConverterProperties converterProperties;
    	//文件管理
    	private OfficeManager officeManager;
    	//文件转换
    	private OfficeDocumentConverter offDocConverter;
    	
    	/**
    	 * @PostConstruct 声明该方法在构造函数执行完之后被调用,且仅调用一次
    	 * 相当于<init-method></init-method>
    	 */
    	@PostConstruct
    	public void start(){
    		//配置
    		DefaultOfficeManagerConfiguration cfg = new DefaultOfficeManagerConfiguration();
    		//指定OpenOffice或LibreOffice安装的根目录
    		cfg.setOfficeHome(converterProperties.getOfficeHome());
    		//LibreOffice处理进程的端口,多个用逗号隔开(如果设置了多个端口,将会启动多个处理进程)
    		String portsNumbers = converterProperties.getOfficePorts();
    		if(portsNumbers != null && !"".equals(portsNumbers.trim())){
    			String[] ports = portsNumbers.split(",");
    			int[] portList = new int[ports.length];
    			for(int i=0;i<ports.length;i++){
    				portList[i] = Integer.valueOf(ports[i]);
    			}
    			cfg.setPortNumbers(portList);
    		}
    		
    		//officeManager
    		officeManager = cfg.buildOfficeManager();
    		
    		//OfficeDocumentConverter文档转换器
    		offDocConverter = new OfficeDocumentConverter(officeManager);
    		System.out.println("Converter initialized ...");
    		
    		//启动文档转换器进程
    		officeManager.start();
    		System.out.println("converter start ...");
    	}
    
    	/**
    	 * 进行转换
    	 */
    	public void converter(String from, String to) {
    		this.converter(new File(from), new File(to));
    	}
    
    	@Override
    	public void converter(File from, File to) {
    		offDocConverter.convert(from, to);
    	}
    	
    	/**
    	 * 终止处理进程
    	 * @PreDestroy 声明该方法在Bean销毁前被调用,仅调用一次
    	 */
    	@PreDestroy
    	public void stop(){
    		officeManager.stop();
    		System.out.println("converter stop ...");
    		
    	}
    
    }
    
  • 相关阅读:
    valueof这个万能方法,将string转换为int或者int转换为string都可以
    java的数组index[]方括号内是可以进行算数运算的
    项目工程的包package与文件夹的关系
    导入项目后下载jar包问题理解
    MySQL 5.6.19 二进制安装
    左右 android AES 所述机器的一部分 javax.crypto.BadPaddingException: pad block corrupted
    iOS_词典阵列 按key分组和排序
    Maven真——聚合和继承(于)
    机器学习Matlab打击垃圾邮件的分类————朴素贝叶斯模型
    切点算法模板(Cut-vertex)
  • 原文地址:https://www.cnblogs.com/kongkongFabian/p/7497273.html
Copyright © 2011-2022 走看看