第一步:引入jar包
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.0</version>
<exclusions>
<exclusion>
<artifactId>juh</artifactId>
<groupId>org.openoffice</groupId>
</exclusion>
<exclusion>
<artifactId>jurt</artifactId>
<groupId>org.openoffice</groupId>
</exclusion>
<exclusion>
<artifactId>ridl</artifactId>
<groupId>org.openoffice</groupId>
</exclusion>
<exclusion>
<artifactId>unoil</artifactId>
<groupId>org.openoffice</groupId>
</exclusion>
</exclusions>
</dependency>
第二步:安装openoffice(windows和linux需要哪个装哪个)
linux 安装参考这篇文章 安装参考 其中rpm安装出错的话加上这个后缀 --force --nodeps ,强制去除多余依赖。 安装参考里的启动命令打错了,用这个启动
/opt/openoffice4/program/soffice "-accept=socket,host=127.0.0.1,port=8100;urp;" -headless -nofirststartwizard &
卸载之前的openoffice版本的话,进入到program文件执行 rpm -e rpm -qa |grep openofficerpm -qa |grep ooobasis
第三步:linux安装中文字体
安装中文字体参考
第四步代码
@Component
public class OpenOfficeConverter {
private static final Logger log = LoggerFactory.getLogger(OpenOfficeConverter.class);
public boolean isRun() {
return officeManager != null && officeManager.isRunning();
}
public void startService(String officeHome, String ports) {
try {
Builder builder = LocalOfficeManager.builder();
if (officeManager != null && officeManager.isRunning()) {
return;
}
builder.officeHome(officeHome);
builder.portNumbers(StringUtils.isNotBlank(ports) ? Integer.valueOf(ports) : port);
builder.killExistingProcess(true);
builder.processTimeout(120000);
builder.processRetryInterval(250);
builder.taskExecutionTimeout(1000 * 60 * 5L);
builder.maxTasksPerProcess(200);
builder.taskQueueTimeout(30000);
officeManager = builder.build();
officeManager.start();
log.info("openoffice启动成功!");
} catch (Exception ce) {
log.error("openoffic 启动失败: " + ce.getMessage());
}
}
@PreDestroy
public void stopService() {
if (officeManager != null) {
try {
officeManager.stop();
log.info("openoffice关闭成功!");
} catch (OfficeException e) {
log.info("openoffice关闭失败!" + e.getMessage());
}
}
}
public void destroy() {
this.stopService();
}
public File convert(String inputFile, String outPath, String fileType) throws GlobalException {
String outputFile = UploadUtils.generateFilename(outPath, fileType);
if (inputFile.endsWith(".txt")) {
String odtFile = FileUtils.getFilePrefix(inputFile) + ".odt";
if (new File(odtFile).exists()) {
inputFile = odtFile;
} else {
try {
FileUtils.copyFile(inputFile, odtFile);
inputFile = odtFile;
} catch (FileNotFoundException e) {
log.error("openoffic convert fail " + e.getMessage());
throw new GlobalException(SysOtherErrorCodeEnum.FILE_NOT_FIND);
}
}
}
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File output = new File(outputFile);
try {
converter.convert(new File(inputFile), output);
} catch (OfficeException e) {
log.error("openoffic convert fail " + e.getMessage());
throw new GlobalException(ContentErrorCodeEnum.OPEN_OFFICE_CONVERSION_ERROR);
}
return output;
}
public File convert(String inputFile, String fileType, boolean delOldFile) {
File tempFile = null;
String outputFile = UploadUtils.generateFilename(getFilePath(), fileType);
if (inputFile.endsWith(".txt")) {
String filecode = "";
filecode = ConverEncoding.getFilecharset(inputFile);
if (SystemUtil.isOSLinux()) {
if (!filecode.equals("UTF-8")) {
try {
filecode = java.text.Normalizer
.normalize(filecode, java.text.Normalizer.Form.NFKD);
inputFile = java.text.Normalizer
.normalize(inputFile, java.text.Normalizer.Form.NFKD);
Runtime.getRuntime()
.exec("iconv -f " + filecode + " -t utf-8 "
+ inputFile + " -c -s -o " + inputFile);
} catch (IOException e) {
e.printStackTrace();
log.error("openoffic convert fail " + e.getMessage());
}
}
} else {
if (!filecode.equals("UTF-8")) {
String odtFile = FileUtils.getFilePrefix(inputFile) + ".odt";
tempFile = new File(odtFile);
if (tempFile.exists()) {
inputFile = odtFile;
} else {
try {
FileUtils.copyFile(inputFile, odtFile);
tempFile = new File(odtFile);
inputFile = odtFile;
} catch (FileNotFoundException e) {
log.error("openoffic convert fail " + e.getMessage());
e.printStackTrace();
}
}
}
}
}
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File output = new File(outputFile);
File oldFile = new File(inputFile);
try {
converter.convert(oldFile, output);
} catch (Exception e) {
e.printStackTrace();
}
if (delOldFile && oldFile.exists()) {
oldFile.delete();
}
if (tempFile != null && tempFile.exists()) {
tempFile.delete();
}
return output;
}
public File convertToPdf(String inputFile, String outPath, String fileName)
throws GlobalException {
String outputFile = UploadUtils.generateByFilename(outPath, fileName, PDF);
File tempFile = null;
if (inputFile.endsWith(".txt")) {
String filecode = "";
filecode = ConverEncoding.getFilecharset(inputFile);
if (SystemUtil.isOSLinux()) {
if (!filecode.equals("UTF-8")) {
try {
Runtime.getRuntime()
.exec("iconv -f " + filecode + " -t utf-8 "
+ inputFile + " -c -s -o " + inputFile);
} catch (IOException e) {
log.error("openoffic convert fail " + e.getMessage());
e.printStackTrace();
}
}
} else {
if (!filecode.equals("UTF-8")) {
String odtFile = FileUtils.getFilePrefix(inputFile) + ".odt";
tempFile = new File(odtFile);
if (tempFile.exists()) {
inputFile = odtFile;
} else {
try {
FileUtils.copyFile(inputFile, odtFile);
inputFile = odtFile;
} catch (FileNotFoundException e) {
log.error("openoffic convert fail " + e.getMessage());
e.printStackTrace();
}
}
}
}
}
if (officeManager == null) {
throw new GlobalException(ContentErrorCodeEnum.OPEN_OFFICE_SERVER_UNINSTALL);
}
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
File output = new File(outputFile);
try {
converter.convert(new File(inputFile), output);
} catch (Exception e) {
log.error("openoffic convert fail " + e.getMessage());
}
if (tempFile != null && tempFile.exists()) {
try {
tempFile.delete();
} catch (Exception e) {
log.error("删除odt文件失败:" + e.getMessage());
}
}
return output;
}
public File convertToHtml(File docFile, File htmlFile) throws GlobalException {
if(officeManager == null) {
throw new GlobalException(ContentErrorCodeEnum.OPEN_OFFICE_SERVER_ERROR);
}
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
try {
converter.convert(docFile, htmlFile);
} catch (OfficeException e) {
log.error("openoffic convert fail " + e.getMessage());
}
return htmlFile;
}
public static String getCode(File file) {
byte[] head = new byte[3];
InputStream inputStream = null;
String code = "gb2312";
if (SystemUtil.isOSLinux()) {
code = "utf-8";
}
try {
inputStream = new FileInputStream(file);
inputStream.read(head);
if (head[0] == -1 && head[1] == -2) {
code = "UTF-16";
}
else if (head[0] == -2 && head[1] == -1) {
code = "Unicode";
}
else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
code = "UTF-8";
}
} catch (IOException e) {
return code;
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return code;
}
public String convertToHtmlString(File docFile, File htmlFile) throws GlobalException {
htmlFile = convertToHtml(docFile, htmlFile);
StringBuilder htmlSb = new StringBuilder();
try {
FileInputStream inputStream = new FileInputStream(htmlFile);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,
OpenOfficeConverter.getCode(htmlFile)));
while (br.ready()) {
String line = br.readLine();
htmlSb.append(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
String htmlStr = htmlSb.toString();
htmlStr = htmlStr.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>");
return htmlStr.toLowerCase();
}
private static OfficeManager officeManager;
private String officeHome;
private Integer port = 8100;
private String filePath;
public OpenOfficeConverter(String officeHome, int port, String filePath) {
super();
this.officeHome = officeHome;
this.port = port;
this.filePath = filePath;
}
public OpenOfficeConverter(String officeHome, int port) {
super();
this.officeHome = officeHome;
this.port = port;
}
public OpenOfficeConverter() {
super();
}
public String getOfficeHome() {
return officeHome;
}
public void setOfficeHome(String officeHome) {
this.officeHome = officeHome;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public static final String HTML = "html";
public static final String PDF = "pdf";
public static final String TXT = "txt";
public static final String DOC = "doc";
public static final String DOCX = "docx";
public static final String XLS = "xls";
public static final String XLSX = "xlsx";
public static final String PPT = "ppt";
public static final String PPTX = "pptx";
public static final String WPS = "wps";
}
|