总结一下以前作的一个在线预览的office的技术。jquery
一、使用的技术:openOffice, jquery.media.jsless
二、提早安装openOffice,socket
三、看代码:code
public class Office2Pdf { // OpenOffice的安装目录,默认会安装到c盘下 private static String OpenOffice_HOME = "C:/Program Files (x86)/OpenOffice 4/program/"; // 启动服务的命令 private static String command = "soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\""; private static Process process = null; /** * 核心转pdf方法 * * @param sourcefile 被转文件 * @param targetfile 转换后的文件 * @return */ public static void convertTo(File sourcefile, File targetfile) { try { // 启动方法 if(process == null){ process = startOpenOffice(); } //8100就是启动openoffice的端口, OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); //建立openoffice文档转换类 DocumentConverter converter = new OpenOfficeDocumentConverter(connection); //转换,传入源文件和目标文件; converter.convert(sourcefile, targetfile); //断开于openoffice服务的链接 connection.disconnect(); } catch (ConnectException e) { e.printStackTrace(); } } /** * 启动openOffice服务 */ public static Process startOpenOffice(){ // 启动OpenOffice的服务的完整命令 String fullCommand = OpenOffice_HOME + command; try { return Runtime.getRuntime().exec(fullCommand); } catch (IOException e) { e.printStackTrace(); } return null; } }