springboot整合openoffice
mcu项目需要对office文件上传并且在线编辑,于是找到和springboot整合的openoffice并且在线预览操作
一,下载openoffice并安装
1. 下载与安装Oppenoffice SDK
官方下载地址:https://www.openoffice.org/download/
二,运行openoffice服务
创建一个openoffice.bat文件,加入下面命令,保存双击执行
@echo off
cd C:\Program Files (x86)\OpenOffice 4\program
soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
三,添加pom依赖包
<dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-core</artifactId> <version>4.2.2</version> </dependency> <!--springboot支持包,里面包括了自动配置类 --> <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-spring-boot-starter --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.2</version> </dependency> <!--jodconverter 本地支持包 --> <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local --> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.2.2</version> </dependency>
四,添加openoffice yml配置
jodconverter: local: enabled: true #office-home: C:\Program Files (x86)\OpenOffice 4 #安装路径 max-tasks-per-process: 10 port-numbers: 8100 #端口号
五,office文件转为pdf文件
注入: @Autowired private DocumentConverter converter; @GetMapping("/filetopdf") public void filetopdf(HttpServletResponse response) { /*需要转换的文件*/ File file = new File("E:\\test\\1234.docx"); try { // 转换之后文件生成的地址 File newFile = new File("E:/test/pdf/"); if (!newFile.exists()) { newFile.mkdirs(); } // pdf文件生成保存的路径 String savePath="E:/test/pdf/"; String fileName="JCccc"+ UUID.randomUUID().toString().replaceAll("-","").substring(0,6); // pdf文件后缀 String fileType=".pdf"; // 将这三个拼接起来,就是我们最后生成文件保存的完整访问路径了 String newFileMix=savePath+fileName+fileType; // 文件转化 converter.convert(file).to(new File(newFileMix)).execute(); // 使用response,将pdf文件以流的方式发送的前端浏览器上 ServletOutputStream outputStream = response.getOutputStream(); // 读取文件 InputStream in = new FileInputStream(new File(newFileMix)); // copy流数据,i为字节数 int i = IOUtils.copy(in, outputStream); in.close(); outputStream.close(); System.out.println("流已关闭,可预览,该文件字节大小:"+i); } catch (Exception e) { e.printStackTrace(); } }
六,pdf在线预览
/** * 预览 * @param response */ @GetMapping("/filetopreview") public void filetopreview(HttpServletResponse response) { String pdfpath ="E:\\test\pdf\JCccc7a9291.pdf"; try { // 使用response,将pdf文件以流的方式发送的前端浏览器上 ServletOutputStream outputStream = response.getOutputStream(); // 读取文件 InputStream in = new FileInputStream(new File(pdfpath )); // copy流数据,i为字节数 int i = IOUtils.copy(in, outputStream); in.close(); outputStream.close(); System.out.println("流已关闭,可预览,该文件字节大小:"+i); } catch (Exception e) { e.printStackTrace(); } }
七,效果
参考网址:
https://blog.csdn.net/weixin_38977885/article/details/119649743