1,启动本地OpenOffice文件
Linux启动:nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & 后台启动
windows启动(进入program目录中):soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard
2,maven依赖
<!--openoffice引lib包下的 -->
<dependency>
<groupId>org.artofsolving.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath>
</dependency>
<dependency>
<groupId>org.artofsolving.jodconverter</groupId>
<artifactId>jodconverter-cli-2.2.2</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/jodconverter-cli-2.2.2.jar</systemPath>
</dependency>
3,工具类:
import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.sx.conf.SxAppConfig;
import org.apache.commons.lang.StringUtils;
import java.io.*;
import java.net.ConnectException;
import javax.servlet.http.HttpServletResponse;
/**
* @author josnow
* @version 1.0.0
* @date 2017年5月9日 下午12:38:39
* @desc openoffice转换工具
*/
public class OpenOfficeUtils {
public static final String LOCAL_HOST = "localhost";
public static final int LOCAL_PORT = 8100;
// Format
public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry();
/**
* @param inputFilePath 待转换的文件路径
* @param outputFilePath 输出文件路径
* @return void
* @author old six
* @Description: TODO(本地调用openoffice转换)
* @Date 16:03 2020/11/2
*/
public static void convert(String inputFilePath, String outputFilePath) throws ConnectException {
convert(inputFilePath, outputFilePath, LOCAL_HOST, LOCAL_PORT);
}
/**
* @param inputFilePath 待转换的文件路径
* @param outputFilePath 输出文件路径
* @param connectIp 远程调用ip
* @param connectPort 远程调用端口
* @return void
* @author old six
* @Description: TODO(远程调用openoffice转换)
* @Date 16:02 2020/11/2
*/
public static void convert(String inputFilePath, String outputFilePath, String connectIp, int connectPort)
throws ConnectException {
if (StringUtils.isEmpty(inputFilePath) || StringUtils.isEmpty(outputFilePath)
|| StringUtils.isEmpty(connectIp)) {
throw new IllegalArgumentException("参数异常!!");
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
connection.connect();
DocumentConverter converter = getConverter(connectIp, connection);
converter.convert(new File(inputFilePath), new File(outputFilePath));
connection.disconnect();
}
/**
* @param inputStream 输入流
* @param inputFileExtension 待转换文件的扩展名,例如: xls,doc
* @param outputStream 输出流
* @param outputFileExtension 输出文件扩展名,例如:pdf
* @return void
* @author old six
* @Description: TODO(任何格式转换 - 本地调用openoffice)
* @Date 16:05 2020/11/2
*/
public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
String outputFileExtension) throws ConnectException {
convert(inputStream, inputFileExtension, outputStream, outputFileExtension, LOCAL_HOST, LOCAL_PORT);
}
/**
* @param inputStream 带转换输入流
* @param inputFileExtension 待转换文件的扩展名,例如: xls,doc
* @param outputStream 转换后输出流
* @param outputFileExtension 输出文件扩展名,例如:pdf
* @param connectIp 远程调用ip
* @param connectPort 远程调用端口
* @return void
* @author old six
* @Description: TODO(任何格式转换 - 远程调用openoffice)
* @Date 16:06 2020/11/2
*/
public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
String outputFileExtension, String connectIp, int connectPort) throws ConnectException {
if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null
|| StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) {
throw new IllegalArgumentException("参数异常!!");
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
connection.connect();
DocumentConverter converter = getConverter(connectIp, connection);
converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream,
formatFactory.getFormatByFileExtension(outputFileExtension));
connection.disconnect();
}
/**
* @param connectIp
* @param connection
* @return com.artofsolving.jodconverter.DocumentConverter
* @author old six
* @Description: TODO(创建远程连接)
* @Date 16:09 2020/11/2
*/
private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
|| "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
: new StreamOpenOfficeDocumentConverter(connection);
return converter;
}
/***
* 测试方法
*
* @param args
*/
public static void main(String[] args) {
// OpenOfficeUtils.convert("E:\\Desktop\\改造分布式session要点 - 副本.docx",
// "E:\\Desktop\\aaawww.pdf", "10.100.100.2", 8100);
try {
// OpenOfficeUtils.convert("E:\\Desktop\\本地服务器.xlsx", "E:\\Desktop\\笔试准考证.pdf",
// "10.100.100.2", 8100);
// OpenOfficeUtils.convert("E:\\Desktop\\本地服务器.xlsx", "E:\\Desktop\\笔试准考证.pdf",
// "10.100.100.2", 8100);
// OpenOfficeUtils.convert(new FileInputStream("E:\\Desktop\\本地服务器.xlsx"),
// "XLSX", new FileOutputStream("E:\\Desktop\\111.pdf"), "PDF",
// "192.168.43.102", 8100);
try {
OpenOfficeUtils.convert(new FileInputStream("C:\\Users\\Administrator\\Desktop\\外阜登记失业人员分析报告.docx"), "docx",
new FileOutputStream("C:\\Users\\Administrator\\Desktop\\23\\0000000011.pdf"), "PDF");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// OpenOfficeUtils.convert("C:\\Users\\admin\\Desktop\\2020十一月的考试-准考证 (5).doc", "C:\\Users\\admin\\Desktop\\2020十一月的考试-准考证 (5).html");
} catch (ConnectException e) {
e.printStackTrace();
}
}
/**
* 将pdf下载到本地
*/
public static void pdfdownload(HttpServletResponse response, String exam_id) throws IOException {
FileInputStream in = null;
OutputStream out = null;
try {
// 获取文件名
String fileNa = new String("体测准考证.pdf".getBytes("utf-8"), "ISO-8859-1");
// 设置响应头,控制浏览器下载该文件
response.setHeader("Content-disposition", "attachment; filename=" + fileNa);
// 读取要下载的文件,保存到文件输入流
in = new FileInputStream(System.getProperty("user.dir") + SxAppConfig.getEXAMPDFURI() + exam_id + ".pdf");
// 创建输出流
out = response.getOutputStream();
// 缓存区
byte buffer[] = new byte[1024];
int len = 0;
// 循环将输入流中的内容读取到缓冲区中
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} finally {
// 关闭
in.close();
out.close();
}
}
}