分享一个超牛逼的 Java 文件在线预览项目
程序员自修室 文章源自JAVA秀-https://www.javaxiu.com/9409.html
文章源自JAVA秀-https://www.javaxiu.com/9409.html
简介文章源自JAVA秀-https://www.javaxiu.com/9409.html
基于 OpenOffice 服务将文件 (.doc、.docx、.xls、.ppt) 转化为pdf、html格式,然后直接在浏览器渲染输出。文章源自JAVA秀-https://www.javaxiu.com/9409.html
内置功能
文件上传功能文章源自JAVA秀-https://www.javaxiu.com/9409.html
文件转换功能文章源自JAVA秀-https://www.javaxiu.com/9409.html
文件预览功能文章源自JAVA秀-https://www.javaxiu.com/9409.html
远程文件预览功能文章源自JAVA秀-https://www.javaxiu.com/9409.html
代码集成
基于 SpringBoot + OpenOffice 实现的项目案例,配置文件 application.properties 引入:文章源自JAVA秀-https://www.javaxiu.com/9409.html
# 默认不开启 需要开启的设置成truejodconverter.local.enabled=true# 开启多个openOffice进程,每个端口对应一个进程jodconverter.local.portNumbers=8100,8200# openOffice进程重启前的最大进程数jodconverter.local.maxTasksPerProcess=100
项目启动会自动生成两个转换进程,下面是伪代码实现:文章源自JAVA秀-https://www.javaxiu.com/9409.html
@RestController@RequestMapping("document")publicclass ConverterController {@Autowiredprivate DocumentConverter documentConverter;/** * 文件上传 */@RequestMapping("/upload")public Result upload(MultipartFile file){try {if (file != null) { File parentFile = createParentFile(); String fileName = file.getOriginalFilename(); String suffix = fileName.substring(fileName.lastIndexOf(".")); String uuid = IdUtil.simpleUUID(); fileName = uuid + suffix; File docFile = new File(parentFile, fileName); FileUtil.writeFromStream(file.getInputStream(), docFile);/** * 年月日目录 */ String fileDay = DateUtil.thisYear() + "/" + (DateUtil.thisMonth() + 1) + "/" + DateUtil.thisDayOfMonth(); String imagePath = SystemConstant.FILE + "/document/" + fileDay + "/" + fileName;/** * 实时转换 */ logger.info("开始转换pdf......"); File toFile = new File(parentFile, uuid + ".pdf"); documentConverter.convert(docFile).to(toFile).execute(); logger.info("开始转换html......"); toFile = new File(parentFile, uuid + ".html"); converter( docFile,toFile);return Result.ok(imagePath); } else {return Result.error(); } } catch (Exception e) { logger.error("转换异常{}",e);return Result.error(); } }}
软件截图
文章源自JAVA秀-https://www.javaxiu.com/9409.html
文章源自JAVA秀-https://www.javaxiu.com/9409.html
文章源自JAVA秀-https://www.javaxiu.com/9409.html
文章源自JAVA秀-https://www.javaxiu.com/9409.html
文章源自JAVA秀-https://www.javaxiu.com/9409.html
文章源自JAVA秀-https://www.javaxiu.com/9409.html
文章源自JAVA秀-https://www.javaxiu.com/9409.html
小结
网上很多文章,Java 集成 OpenOffice 已经老得不能再老了,在 Linux 下还有各种启动问题,本案例只需要在系统中安装好相关服务,程序启动会自动生成 OpenOffice 进程。微信搜索公众号 逆锋起笔,关注后回复 编程资源,领取各种经典学习资料。文章源自JAVA秀-https://www.javaxiu.com/9409.html
源码
老规矩了,放在隔壁小号了,感兴趣的劳烦大驾。文章源自JAVA秀-https://www.javaxiu.com/9409.html
【源码获取方式】文章源自JAVA秀-https://www.javaxiu.com/9409.html
识别二维码,回复:在线预览文章源自JAVA秀-https://www.javaxiu.com/9409.html
文章源自JAVA秀-https://www.javaxiu.com/9409.html

评论