package com.sun.util;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.ConnectException;

import com.artofsolving.jodconverter.DocumentConverter;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

/**

* @description 文件转换工具类

* @author sunshanjin

*

*/

public class Doc2HtmlUtils {

private static Doc2HtmlUtils doc2HtmlUtils;

/**

* 获取Doc2HtmlUtil实例

*/

public static synchronized Doc2HtmlUtils getDoc2HtmlUtilsInstance() {

if (doc2HtmlUtils == null) {

doc2HtmlUtils = new Doc2HtmlUtils();

}

return doc2HtmlUtils;

}

/**

* @description dox,docx,xls,ppt转html

* @param file

* @return 生成的html文件名称 xxx.html

* @throws IOException

*/

public String file2html(File file) throws IOException {

if (!file.exists()) {

throw new FileNotFoundException("没有找到该文件:" + file.getPath());

}

String filename = file.getName();

File htmlFile = null;

if (filename.endsWith("docx")) {

htmlFile = new File(file.getParent() + File.separator + filename.replace(".docx", ".html"));

}else if (filename.endsWith("doc")) {

htmlFile = new File(file.getParent() + File.separator + filename.replace(".doc", ".html"));

}else if (filename.endsWith("xls")) {

htmlFile = new File(file.getParent() + File.separator + filename.replace(".xls", ".html"));

}else if (filename.endsWith("ppt")) {

htmlFile = new File(file.getParent() + File.separator + filename.replace(".ppt", ".html"));

}

if (!htmlFile.exists()) {

htmlFile.createNewFile();

}else{

return htmlFile.getName();

}

this.convent(file, htmlFile);

//返回生成的html文件名

return htmlFile.getName();

}

/**

* @description dox,docx,xls,ppt转pdf

* @param file

* @return 生成的pdf文件名称xxx.pdf

* @throws IOException

*/

public String file2pdf(File file) throws IOException {

if (!file.exists()) {

throw new FileNotFoundException("没有找到该文件:" + file.getPath());

}

String filename = file.getName();

File pdfFile = null;

if (filename.endsWith("docx")) {

pdfFile = new File(file.getParent() + File.separator + filename.replace(".docx", ".pdf"));

}else if (filename.endsWith("doc")) {

pdfFile = new File(file.getParent() + File.separator + filename.replace(".doc", ".pdf"));

}else if (filename.endsWith("xls")) {

pdfFile = new File(file.getParent() + File.separator + filename.replace(".xls", ".pdf"));

}else if(filename.endsWith("xlsx")){

pdfFile = new File(file.getParent() + File.separator + filename.replace(".xlsx", ".pdf"));

}else if (filename.endsWith("ppt")) {

pdfFile = new File(file.getParent() + File.separator + filename.replace(".ppt", ".pdf"));

}

if (!pdfFile.exists()) {

pdfFile.createNewFile();

}else{

return pdfFile.getName();

}

this.convent(file, pdfFile);

//返回生成的html文件名

return pdfFile.getName();

}

/**

* 转换文件

* @param sourceFile 源文件

* @param distinctFile 目标文件

*/

public void convent(File sourceFile, File distinctFile){

OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);

try {

connection.connect();

} catch (ConnectException e) {

System.err.println("无法完成文件转换,请检查OpenOffice服务是否启动!");

}

DocumentConverter converter = new OpenOfficeDocumentConverter(connection);

converter.convert(sourceFile, distinctFile);

connection.disconnect();

}

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐