使用JAVA实现word、excel、ppt、txt在线预览功能
思路说明
通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf。获取pdf路径生成url从而实现在线预览功能。
使用office转换PDF
public static int officeToPDF(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
// 找不到源文件, 则返回-1
if (!inputFile.exists()) {
return -1;
}
// 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
// 如果该文件已经存在,则不再进行转换
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
// OpenOffice的安装目录
String OpenOffice_HOME ="C:\\OpenOffice\\program\\soffice.exe ";
// 启动OpenOffice的服务
Process pro = Runtime.getRuntime().exec(OpenOffice_HOME);
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
connection.connect();
// convert
DocumentConverter