java实现办公文件在线预览性能是一个大家在工作中兴许会遇到的需要,网上些公司专门提供这样的服务,不过须要免费 如果想要收费的,能够用openoffice,实现原理就是:
通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件流;
当然如果装了Adobe Reader XI,那把pdf间接拖到浏览器页面就能够间接关上预览,前提就是浏览器反对pdf文件浏览。
我这里介绍通过poi实现word、excel、ppt转pdf流,这样就能够在浏览器上实现预览了。
1.到官网下载Apache OpenOffice 安装包,装置运行。
不同零碎的装置办法,自行百度,这里不做过多阐明。
2.再我的项目的pom文件中引入依赖
<!--openoffice--> <dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version>2.2.1</version> </dependency>
3.将word、excel、ppt转换为pdf流的工具类代码
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; /** * 文件格式转换工具类 * * @author tarzan * @version 1.0 * @since JDK1.8 */ public class FileConvertUtil { /** 默认转换后文件后缀 */ private static final String DEFAULT_SUFFIX = "pdf"; /** openoffice_port */ private static final Integer OPENOFFICE_PORT = 8100; /** * 办法形容 office文档转换为PDF(解决本地文件) * * @param sourcePath 源文件门路 * @param suffix 源文件后缀 * @return InputStream 转换后文件输出流 * @author tarzan */ public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception { File inputFile = new File(sourcePath); InputStream inputStream = new FileInputStream(inputFile); return covertCommonByStream(inputStream, suffix); } /** * 办法形容 office文档转换为PDF(解决网络文件) * * @param netFileUrl 网络文件门路 * @param suffix 文件后缀 * @return InputStream 转换后文件输出流 * @author tarzan */ public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception { // 创立URL 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; } /** * 办法形容 将文件以流的模式转换 * * @param inputStream 源文件输出流 * @param suffix 源文件后缀 * @return InputStream 转换后文件输出流 * @author tarzan */ 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); } /** * 办法形容 outputStream转inputStream * * @author tarzan */ public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception { ByteArrayOutputStream baos=(ByteArrayOutputStream) out; return new ByteArrayInputStream(baos.toByteArray()); } public static void main(String[] args) throws IOException { //convertNetFile("http://172.16.10.21/files/home/upload/department/base/201912090541573c6abdf2394d4ae3b7049dcee456d4f7.doc", ".pdf"); //convert("c:/Users/admin/Desktop/2.pdf", "c:/Users/admin/Desktop/3.pdf"); } }
4.serve层在线预览办法代码
/** * @Description:系统文件在线预览接口 * @Author: tarzan */ public void onlinePreview(String url, HttpServletResponse response) throws Exception { //获取文件类型 String[] str = SmartStringUtil.split(url,"\\."); if(str.length==0){ throw new Exception("文件格式不正确"); } String suffix = str[str.length-1]; if(!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls") && !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx")){ throw new Exception("文件格式不反对预览"); } InputStream in=FileConvertUtil.convertNetFile(url,suffix); OutputStream outputStream = response.getOutputStream(); //创立寄存文件内容的数组 byte[] buff =new byte[1024]; //所读取的内容应用n来接管 int n; //当没有读取完时,持续读取,循环 while((n=in.read(buff))!=-1){ //将字节数组的数据全副写入到输入流中 outputStream.write(buff,0,n); } //强制将缓存区的数据进行输入 outputStream.flush(); //关流 outputStream.close(); in.close(); }
5.controler层代码
@ApiOperation(value = "系统文件在线预览接口 by tarzan") @PostMapping("/api/file/onlinePreview") public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception{ fileService.onlinePreview(url,response); }
原文链接:https://www.gaodaima.com/weixin_…
版权申明:本文为CSDN博主「洛阳泰山」的原创文章,遵循CC 4.0 BY-SA版权协定,转载请附上原文出处链接及本申明。
近期热文举荐:
1.1,000+ 道 Java面试题及答案整顿(2021最新版)
2.别在再满屏的 if/ else 了,试试策略模式,真香!!
3.卧槽!Java 中的 xx ≠ null 是什么新语法?
4.Spring Boot 2.6 正式公布,一大波新个性。。
5.《Java开发手册(嵩山版)》最新公布,速速下载!
感觉不错,别忘了顺手点赞+转发哦!