最近这段时间,在研究lucene实现网页预览文档
使用Openoffic+Swftools+flexpaper实现预览
首先要开启openoffice服务 ,每次都要手动启动,很是麻烦,所以研究了一下windows命令,启动openoffice服务。
思想:将启动openoffice服务的命令封装在.bat文件中,使用java代码调用该.bat文件,实现启动openoffice服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package ytu.botao.util; import java.io.File; import java.io.FileWriter; /** * 功能:开启openoffice服务 * 使用方法:直接生成该类对象 * *****由于本机openoffice安装路径不同,需要更改openoffice的安装路径 * @author botao * */ public class JavaCallOpenoffice { /** * 将启动程序定义在构造函数中,直接生成该类对象,即可启动openoffice服务 */ public JavaCallOpenoffice() { Runtime rn = Runtime.getRuntime(); Process p = null ; try { File file= new File( "d:\\openoprenoffice.bat" ); if ( false ==file.exists()) { System.out.println( "。。。。。。。。。。" ); FileWriter writer = new FileWriter( "d:\\openoprenoffice.bat " ); writer.write( "@echo off " ); writer.write( "\r\n " ); writer.write( "D:" ); writer.write( "\r\n " ); //D:\\Program Files\\OpenOffice 4\\program: openoffice的安装路径路径 writer.write( "cd D:\\Program Files\\OpenOffice 4\\program" ); writer.write( "\r\n " ); writer.write( "soffice -headless -accept=" + "socket,host=127.0.0.1,port=8100;urp;" + " -nofirststartwizard" ); writer.write( "\r\n " ); writer.write( "@echo on " ); writer.close(); } p = rn.exec( "cmd.exe /C d:\\openoprenoffice.bat" ); } catch (Exception e1) { e1.printStackTrace(); } } } |
每次启动openoffice服务,soffice进程都占用100M内存,很是浪费,所以在文档转换完之后,要关闭soffice进程package
ytu.botao.util;
import
java.io.IOException;
import
java.util.Scanner;
/**
* 功能:关闭openoffice服务
* 使用方法:直接生成该类对象即可
* @author botao
*
*/
public
class
DistorySoffice {
private
Process process=
null
;
/**
* 构造方法,实现关闭soffice进程
*/
public
DistorySoffice() {
try
{
//显示进程
process=Runtime.getRuntime().exec(
"tasklist"
);
Scanner in=
new
Scanner(process.getInputStream());
while
(in.hasNextLine()) {
String processString=in.nextLine();
if
(processString.contains(
"soffice.exe"
)) {
//关闭soffice进程的命令
String cmd=
"taskkill /f /im soffice.exe"
;
process=Runtime.getRuntime().exec(cmd);
System.out.println(
"openoffice正常关闭......."
);
}
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
联系客服