官网:https://www.openoffice.org/download/
Apache OpenOffice是一款先进的开源 办公软件套件,它包含文本文档、电子表格、演示文稿、绘图、数据库等。 它能够支持许多语言并且在所有普通计算机上工作。它将你所有的数据以国际开放标准格式存储下来,并能够读写从其它常用办公软件包来的文件。它可以被完全免费下载并使用于任何用途。
2 安装OpenOffice然后直接下一步安装就可以了,步骤过于简单这里省略,如有问题可以留言哈
3 Spring Boot整合新建Spring Boot项目
3.1 依赖3.2 配置文件org.jodconverter jodconverter-core 4.2.2 org.jodconverter jodconverter-spring-boot-starter 4.2.2 org.jodconverter jodconverter-local 4.2.2 commons-io commons-io 2.6
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.pdf3.3 项目结构 3.4 编码
OnlineDocDao.java
@Repository
public class OnlineDocDao {
@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
public interface OnlineDocService {
String readDoc(int fileIndex);
}
OnlineDocServiceImpl.java
@Service
public class OnlineDocServiceImpl implements OnlineDocService {
@Autowired
private OnlineDocDao onlineDocDao;
@Override
public String readDoc(int fileIndex) {
return onlineDocDao.getFiles(fileIndex);
}
}
OnlineDocController.java
@Controller
@SuppressWarnings("all")
public class OnlineDocController {
@Autowired
private documentConverter converter;
@Autowired
private OnlineDocService onlineDocService;
@Value("${online.buffer.path}")
private String bufferPath;
@Value("${online.buffer.file.name}")
private String bufferFileName;
@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时需要用到的进程端口号,可以随意设置,所以这个就不用管啦



