赞
踩
利用openoffice插件将上传在服务器端的各种文件转换成pdf实现在线预览功能。
xlx,xlxs,txt,word,ppt等格式都可以。
安装步骤可参考:
https://blog.csdn.net/Howinfun/article/details/80759008
说明:
服务器端安装openoffice插件,安装路径记住,方便后面开启openoffice服务。如下为我在安装的时候服务器端安装路径:D:/openoffice1。(之后程序会用到)
接着cmd到图上program目录,输入:soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
开启openoffice服务
开启成功
上传文件之后直接将文件先转换成pdf,保存在某个路径下,通过源文件路径可以获取对应的pdf路径。这样就缩短了在线预览时候的请求时间。当然根据个人需求可更改。
源码所需的jar包已上传个人的博客中,可直接下载。
jar下载地址:
https://download.csdn.net/download/qq_35901863/9956063
//源文件转换为输出文件,originSourcePath上传的文件路径,outputSourcePath为即将保存的pdf文件路径。可根据原路径找到输出路径。
File pdfFile = DocConverterController.office2pdf(originSourcePath,outputSourcePath);
DocConverterControlle类中的office2pdf方法如下:
/** * 连接OpenOffice.org 并且启动OpenOffice.org * * @return */ public static OfficeManager getOfficeManager() { DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); // 设置OpenOffice.org 3的安装目录 config.setOfficeHome(getOfficeHome()); // 启动OpenOffice的服务 OfficeManager officeManager = config.buildOfficeManager(); officeManager.start(); return officeManager; } /** * 转换文件 * * @param inputFile * @param outputFilePath_end * @param inputFilePath * @param outputFilePath_end * @param converter */ public static File converterFile(File inputFile, String outputFilePath_end, String inputFilePath, OfficeDocumentConverter converter) { File outputFile = new File(outputFilePath_end); // 假如目标路径不存在,则新建该路径 if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } converter.convert(inputFile, outputFile); System.out.println("文件:" + inputFilePath + "\n转换为\n目标文件:" + outputFile + "\n成功!"); return outputFile; } /** * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件<br> * * @param inputFilePath * 源文件路径,如:"e:/test.docx" * 目标文件路径,如:"e:/test_docx.pdf" * @return */ public static File office2pdf(String inputFilePath,String outputFilePath_end) { OfficeManager officeManager = null; try { if (StringUtils.isEmpty(inputFilePath)) { System.out.println("输入文件地址为空,转换终止!"); return null; } File inputFile; String originSourceTxtName = inputFilePath.substring(inputFilePath.lastIndexOf(".")); if (originSourceTxtName.equalsIgnoreCase(".txt")) { //是txt则需要转化为odt,然后再转为pdf outputFilePath_end = outputFilePath_end.replaceAll("." + getPostfix(outputFilePath_end), ".txt.pdf"); System.out.println(outputFilePath_end); } inputFile = new File(inputFilePath); // 用于处理PDF文档 if (!inputFile.exists() && !getPostfix(inputFilePath).equals("txt")) { System.out.println("输入文件不存在,转换终止!"); return null; } // 获取OpenOffice的安装路劲 officeManager = getOfficeManager(); // 连接OpenOffice OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); return converterFile(inputFile, outputFilePath_end, inputFilePath, converter); } catch (Exception e) { System.out.println("转化出错!"); } finally { // 停止openOffice if (officeManager != null) { officeManager.stop(); } } return null; } /** * 根据操作系统的名称,获取OpenOffice.org 3的安装目录<br> * 如我的OpenOffice.org 3安装在:C:/Program Files (x86)/OpenOffice.org 3<br> * * @return OpenOffice.org 3的安装目录 */ public static String getOfficeHome() { String osName = System.getProperty("os.name"); System.out.println("操作系统名称:" + osName); if (Pattern.matches("Linux.*", osName)) { return "/opt/openoffice.org3"; } else if (Pattern.matches("Windows.*", osName)) { return "E:/openoffice"; } else if (Pattern.matches("Mac.*", osName)) { return "/Applications/OpenOffice.org.app/Contents/"; } return null; } /** * 获取输出文件的路径 * * @param inputFilePath * @return */ public static String getOutputFilePath(String inputFilePath) { String outputFilePath = inputFilePath.replaceAll("." + getPostfix(inputFilePath), ".pdf"); return outputFilePath; } /** * 获取inputFilePath的后缀名,如:"e:/test.pptx"的后缀名为:"pptx"<br> * * @param inputFilePath * @return */ public static String getPostfix(String inputFilePath) { return inputFilePath.substring(inputFilePath.lastIndexOf(".") + 1); }
pdf转换成功后,当前端想要预览某个文件时候,传过来的filepath,这时候就可以根据原路径找到pdf路径,然后通过流的方式输出pdf文件到浏览器窗口实现在线预览了。
输出pdf文件流到浏览器:
public void onLineReadFile(String fileName, HttpServletResponse response) { try { //文件夹路径 String mkPath = PropertiesUtil.getValue("fileStorePath"); String originSourcePath = mkPath+"/"+fileName; //获取预览文件的路径 String outputSourcePath = DocConverterController.getOutputFilePath(originSourcePath); File pdfFile = new File(outputSourcePath); while(!pdfFile.exists()){ Thread.sleep(1000); } //输出pdf到浏览器 BufferedInputStream br = new BufferedInputStream(new FileInputStream(pdfFile)); byte[] buf = new byte[1024]; int len = 0; response.reset(); // 非常重要 response.setContentType("application/pdf;charset=utf-8"); response.setHeader("Content-Disposition", "inline; filename=" + java.net.URLEncoder.encode(pdfFile.getName(), "UTF-8")); OutputStream out = response.getOutputStream(); while ((len = br.read(buf)) != -1) out.write(buf, 0, len); br.close(); out.close(); } catch (Exception e) { logger.info(e); } }
前端接收数据后即在线预览成功!
项目没开,直接看下成功后大概样式如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。