一站式
企业服务平台
400-6789-948

javaopenOffice实现局域网内在线预览(docx转pdf)

序言

目前主要浏览器均支持pdf文件直接开启以达到文件预览目的。在使用该方法时需要把要查看的数据保存到ftp文件名中并将其转存至一个指定目录下;如果是其它格式文件,则必须重新加载;然后再进行读取操作。若为其它格式文件,必须下载,所以使用openOffice将文件转成pdf形式。

一,安装openOffice

下载地址:http://www.openoffice.org/

安装教程供参考:openOffice下载安装

在安装目录中键入cmd

在指令窗口中输入下列指令开始:

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard.

二,检验

导包

com.artofsolving jodconverter 2.2.1

文件工具类

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry; import com.artofsolving.jodconverter.DocumentConverter; import com.artofsolving.jodconverter.DocumentFormat; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; import java.io. *; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class FileConvertUtil { private static final String DEFAULT_SUFFIX = "pdf"; public static final String DEFAULT_HOST = "127.0.0.1"; private static final Integer OPENOFFICE_PORT = 8100; public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception { File inputFile = new File(sourcePath); InputStream inputStream = new FileInputStream(inputFile); return covertCommonByStream(inputStream, suffix); } public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception { URL url = new URL(netFileUrl); URLConnection urlconn = url.openConnection(); urlconn.connect(); HttpURLConnection httpconn = (HttpURLConnection) urlconn; int httpResult = httpconn.getResponseCode(); if (httpResult == HttpURLConnection.HTTP_OK) { InputStream inputStream = urlconn.getInputStream(); return covertCommonByStream(inputStream, suffix); } return null; } public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_PORT); connection.connect(); DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection); DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry(); DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX); DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix); converter.convert(inputStream, sourceFormat, out, targetFormat); connection.disconnect(); return outputStreamConvertInputStream(out); } public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception { ByteArrayOutputStream baos=(ByteArrayOutputStream) out; return new ByteArrayInputStream(baos.toByteArray()); } }

controler层代码

@PostMapping("/onlinePreview") public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception { String suffix = url.substring(url.lastIndexOf('.') + 1); if (!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls") && !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx") && !suffix.equals("sheet") && !suffix.equals("pdf")) { throw new Exception("文件格式不支持预览"); } url=url.replace("192.168.1.125:8765/knowledge/","G:/creo/knowledge/"); InputStream in = FileConvertUtil.convertLocaleFile(url, suffix); OutputStream outputStream = response.getOutputStream(); byte[] buff = new byte[1024]; int n; while ((n = in.read(buff)) != -1) { outputStream.write(buff, 0, n); } outputStream.flush(); outputStream.close(); in.close(); }

常见的异常情况

java.lang.IllegalArgumentException: inputFormat is null at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.ensureNotNull(AbstractOpenOfficeDocumentConverter.java:113)

其原因是转换07版本和高版本(.docx-.xlsx-.pptx)后,上述3种格式并没有出现在支持文件格式上。

***解决方法

改写后

BasicDocumentFormatRegistry (一定得在com.artofsolving.jodconverter包下)

package com.artofsolving.jodconverter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class BasicDocumentFormatRegistry implements DocumentFormatRegistry { private List documentFormats = new ArrayList(); public BasicDocumentFormatRegistry() { } public void addDocumentFormat(DocumentFormat documentFormat) { this.documentFormats.add(documentFormat); } protected List getDocumentFormats() { return this.documentFormats; } public DocumentFormat getFormatByFileExtension(String extension) { if (extension == null) { return null; } else { if (extension.indexOf("doc") >= 0) { extension = "doc"; } if (extension.indexOf("ppt") >= 0) { extension = "ppt"; } if (extension.indexOf("xls") >= 0) { extension = "xls"; } String lowerExtension = extension.toLowerCase(); Iterator it = this.documentFormats.iterator(); DocumentFormat format; do { if (!it.hasNext()) { return null; } format = (DocumentFormat)it.next(); } while(!format.getFileExtension().equals(lowerExtension)); return format; } } public DocumentFormat getFormatByMimeType(String mimeType) { Iterator it = this.documentFormats.iterator(); DocumentFormat format; do { if (!it.hasNext()) { return null; } format = (DocumentFormat)it.next(); } while(!format.getMimeType().equals(mimeType)); return format; } }

参考资料:解决jodconverter2 .2.1版不支持将docx,xlsx,pptx转换为PDF格式的异常情况

原文链接:http://www.sfdkj.com/12808.html

赞(11) 联系作者
未经允许不得转载:【聚禄鼎】一站式企业服务平台 » javaopenOffice实现局域网内在线预览(docx转pdf)
分享到

评论 抢沙发

让我们一起创建更加美好的网络世界

本站部分资源来自于网络收集,若侵犯了你的隐私或版权,请及时联系我们删除有关信息!

扫码添加站长微信

扫码添加小编微信

登录

找回密码

注册