300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Spring Boot整合OpenOffice实现Word Excel PPT预览

Spring Boot整合OpenOffice实现Word Excel PPT预览

时间:2018-06-05 19:10:01

相关推荐

Spring Boot整合OpenOffice实现Word Excel PPT预览

Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览

1 介绍下OpenOffice

官网:/download/

Apache OpenOffice是一款先进的开源办公软件套件,它包含文本文档电子表格演示文稿绘图数据库等。 它能够支持许多语言并且在所有普通计算机上工作。它将你所有的数据以国际开放标准格式存储下来,并能够读写从其它常用办公软件包来的文件。它可以被完全免费下载并使用于任何用途

2 安装OpenOffice

然后直接下一步安装就可以了,步骤过于简单这里省略,如有问题可以留言哈

3 Spring Boot整合

新建Spring Boot项目

3.1 依赖

<!--jodconverter 核心包 --><dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-core</artifactId><version>4.2.2</version></dependency><!--springboot支持包,里面包括了自动配置类 --><dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-spring-boot-starter</artifactId><version>4.2.2</version></dependency><!--jodconverter 本地支持包 --><dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.2.2</version></dependency><!-- commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency>

3.2 配置文件

server.port=9999#使能jodconverter.local.enabled=true#OpenOffice安装地址jodconverter.local.office-home=C:/Program Files (x86)/OpenOffice 4#同时执行任务的个数jodconverter.local.max-tasks-per-process=10#设置端口号(任意设置)jodconverter.local.port-numbers=8110#controller工具参数online.buffer.path=C:/online_pdf/online.buffer.file.name=hello.pdf

3.3 项目结构

3.4 编码

OnlineDocDao.java

/*** @desc: 模拟Dao层* @author: YanMingXin* @create: /10/4-20:28**/@Repositorypublic class OnlineDocDao {/*** 获取文件** @param fileIndex* @return*/@SuppressWarnings("all")public String getFiles(int fileIndex) {switch (fileIndex) {case 1:return "file1.docx";case 2:return "file2.docx";case 3:return "file3.docx";case 4:return "1.xlsx";default:return "file1.docx";}}}

OnlineDocService.java

/*** @desc: Service接口* @author: YanMingXin* @create: /10/4-20:27**/public interface OnlineDocService {/*** 根据索引获取文件名称** @param fileIndex* @return*/String readDoc(int fileIndex);}

OnlineDocServiceImpl.java

/*** @desc: Service实现类* @author: YanMingXin* @create: /10/4-20:27**/@Servicepublic class OnlineDocServiceImpl implements OnlineDocService {@Autowiredprivate OnlineDocDao onlineDocDao;@Overridepublic String readDoc(int fileIndex) {return onlineDocDao.getFiles(fileIndex);}}

OnlineDocController.java

/*** @desc: 控制类* @author: YanMingXin* @create: /10/5-9:50**/@Controller@SuppressWarnings("all")public class OnlineDocController {/*** 注入DocumentConverter(jodconverter内置)*/@Autowiredprivate DocumentConverter converter;/*** 注入Service*/@Autowiredprivate OnlineDocService onlineDocService;@Value("${online.buffer.path}")private String bufferPath;@Value("${online.buffer.file.name}")private String bufferFileName;/*** 主要业务逻辑,处理文件请求** @param index* @param response*/@RequestMapping("/toPdfFile/{index}")public void toPdfFile(@PathVariable("index") Integer index, HttpServletResponse response) {String fileName = onlineDocService.readDoc(index);File file = new File("src/main/resources/static/" + fileName);ServletOutputStream outputStream = null;InputStream in = null;//转换之后文件生成的地址File newFile = new File(bufferPath);if (!newFile.exists()) newFile.mkdirs();try {//文件转化converter.convert(file).to(new File(bufferPath + bufferFileName)).execute();outputStream = response.getOutputStream();in = new FileInputStream(new File(bufferPath + bufferFileName));IOUtils.copy(in, outputStream);} catch (Exception e) {e.printStackTrace();} finally {try {if (in != null) in.close();if (outputStream != null) outputStream.close();} catch (IOException e) {e.printStackTrace();}}}}

3.5 启动测试

4 结语

4.1 为什么需要缓冲层?

4.2 OpenOffice端口号问题

因为看网上相同的文章的时候,端口号都写的8100,还以为OpenOffice的默认端口号是8100,但是改过了之后才发现,应该是Java连接OpenOffice时需要用到的进程端口号,可以随意设置,所以这个就不用管啦

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。