您的位置:首页 > 运维架构

OpenOffice+SWFTools+ FlexPaper实现文件预览

2017-06-27 20:03 423 查看
准备

1.安装OpenOffice
打开cmd
进入OpenOffice\program的安装目录
OpenOffice服务启动命令:soffice -headless —accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
OpenOffice查看服务是否启动命令:
netstat -ano|findstr "8100"
tasklist|findstr "ipd值"
OpenOffce 端口:8100

2安装SWFTools

3.配置FlexPaper:
flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,flexpaper.js,flexpaper_handler.js ,FlexPaperViewer.swf

4.注意点:
OpenOffice对纯文本文件不是很兼容,先renameTo *.odt格式


主要代码

/**
* 文件预览功能
*
* @author pc
*
*/
public class PreviewUtil {

protected final static Logger _logger = LoggerFactory.getLogger(PreviewUtil.class);

/*
* OFFICE-PDF
*/
public static String converPDF(String filePath) throws Exception {
// 源文件
File primaryFile = new File(filePath);
// 转换后的pdf文件
File pdfFile = new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf");
// 如果存在进行转换
// OPENOFFICE 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
try {
// START
connection.connect();
} catch (ConnectException e) {
_logger.error("获取OPENOFFICE连接失败:",e);
throw new Exception("获取OPENOFFICE连接失败:" + e.getMessage());
}
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(primaryFile, pdfFile);
connection.disconnect();
// STOP
return filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf";
}

/*
* 判断系统类型
*/
public static int validateOSType() {
String os = System.getProperty("os.name");
// windows
if (os.toLowerCase().startsWith("win")) {
return PreviewConstant.WINDOWS_ENVIRONMENT;
} else if (os.toLowerCase().startsWith("linux")) {
// Linux
return PreviewConstant.LINUX_ENVIRONMENT;
}
return 0;
}

/*
* PDF-SWF
*/
public static String converSWF(String filePath) throws Exception {
// 新起运行库
Runtime runtime = Runtime.getRuntime();
File pdfFile = new File(filePath);
String swfPath = "webroot/share/upload/"+filePath.substring(filePath.lastIndexOf("/")+1,filePath.lastIndexOf(".")) + ".swf";
if (pdfFile.exists()) {
// WINDOWS下启动SWFTOOLS进程
if (validateOSType() == 1) {
try {
Process process = runtime
.exec(PreviewConstant.WINDOWS_SWFTOOLS_URL + filePath + " -o " + swfPath + " -f -T 9");
new DoOutput(process.getInputStream()).start();
new DoOutput(process.getErrorStream()).start();
try {
// 阻塞当前进程  等待process执行完成
process.waitFor();
} catch (InterruptedException e) {
_logger.error("转换失败:",e);
}
return swfPath;
} catch (IOException e) {
_logger.error("WINDOWS SWFTOOLS进行转换失败:",e);
throw new Exception("WINDOWS SWFTOOLS进行转换失败:" + e.getMessage());
}
} else if (validateOSType() == 2) {
// LINUX下启动SWFTOOLS进程
try {
Process process = runtime
.exec(PreviewConstant.LINUX_SWFTOOLS_URL + " " + filePath + " -o " + swfPath + " -f -T 9");
new DoOutput(process.getInputStream()).start();
new DoOutput(process.getErrorStream()).start();
try {
// 阻塞当前进程,直到cmd执行完
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
//删除PDF文件
pdfFile.delete();
return swfPath;
} catch (IOException e) {
_logger.error("LINUX SWFTOOLS进行转换失败:",e);
throw new Exception("LINUX SWFTOOLS进行转换失败:" + e.getMessage());
}
} else if (validateOSType() == 0) {
throw new Exception("请确认当前操作系统类型");
}
}else{
throw new Exception("PDF-SWF失败:为找到PDF文件!");
}
throw new Exception("文件转换失败!");
}

/*
* 读取转换时cmd进程的标准输出流和错误输出流,这样做是因为如果不读取流,进程将死锁
*/
private static class DoOutput extends Thread {
public InputStream is;

public DoOutput(InputStream is) {
this.is = is;
}

public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
String str = null;
try {
while ((str = br.readLine()) != null)
;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: