package com.openoffice;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
public class Doc2HtmlUtil {
private static Doc2HtmlUtil doc2HtmlUtil;
/**
* 获取Doc2HtmlUtil实例
*/
public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
if (doc2HtmlUtil == null) {
doc2HtmlUtil = new Doc2HtmlUtil();
}
return doc2HtmlUtil;
}
/**
* 转换文件成html
*
* @param fromFileInputStream:
* @throws IOException
*/
public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timesuffix = sdf.format(date);
String docFileName = null;
String htmFileName = null;
if("doc".equals(type)){
docFileName = "doc_" + timesuffix + ".doc";
htmFileName = "doc_" + timesuffix + ".html";
}else if("docx".equals(type)){
docFileName = "docx_" + timesuffix + ".docx";
htmFileName = "docx_" + timesuffix + ".html";
}else if("xls".equals(type)){
docFileName = "xls_" + timesuffix + ".xls";
htmFileName = "xls_" + timesuffix + ".html";
}else if("ppt".equals(type)){
docFileName = "ppt_" + timesuffix + ".ppt";
htmFileName = "ppt_" + timesuffix + ".html";
}else{
return null;
}
File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
if (htmlOutputFile.exists())
htmlOutputFile.delete();
htmlOutputFile.createNewFile();
if (docInputFile.exists())
docInputFile.delete();
docInputFile.createNewFile();
/**
* 由fromFileInputStream构建输入文件
*/
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fromFileInputStream.close();
} catch (IOException e) {
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();
// 转换完之后删除word文件
docInputFile.delete();
return htmFileName;
}
/**
* 转换文件成pdf
*
* @param fromFileInputStream:
* @throws IOException
*/
public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timesuffix = sdf.format(date);
String docFileName = null;
String htmFileName = null;
if("doc".equals(type)){
docFileName = "doc_" + timesuffix + ".doc";
htmFileName = "doc_" + timesuffix + ".pdf";
}else if("docx".equals(type)){
docFileName = "docx_" + timesuffix + ".docx";
htmFileName = "docx_" + timesuffix + ".pdf";
}else if("xls".equals(type)){
docFileName = "xls_" + timesuffix + ".xls";
htmFileName = "xls_" + timesuffix + ".pdf";
}else if("ppt".equals(type)){
docFileName = "ppt_" + timesuffix + ".ppt";
htmFileName = "ppt_" + timesuffix + ".pdf";
}else{
return null;
}
File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
if (htmlOutputFile.exists())
htmlOutputFile.delete();
htmlOutputFile.createNewFile();
if (docInputFile.exists())
docInputFile.delete();
docInputFile.createNewFile();
/**
* 由fromFileInputStream构建输入文件
*/
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
fromFileInputStream.close();
} catch (IOException e) {
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();
// 转换完之后删除word文件
docInputFile.delete();
return htmFileName;
}
public static void main(String[] args) throws IOException {
Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
File file = null;
FileInputStream fileInputStream = null;
file = new File("F:\\临时文件\\openofficetest\\test1.xls");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls");
coc2HtmlUtil.file2pdf(fileInputStream, "F:\\临时文件\\openofficetest\\xls","xls");
file = new File("F:\\临时文件\\openofficetest\\test1.doc");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc","doc");
coc2HtmlUtil.file2pdf(fileInputStream, "F:\\临时文件\\openofficetest\\doc","doc");
file = new File("F:\\临时文件\\openofficetest\\test1.ppt");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
coc2HtmlUtil.file2pdf(fileInputStream, "F:\\临时文件\\openofficetest\\ppt","ppt");
file = new File("F:\\临时文件\\openofficetest\\test1.docx");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
coc2HtmlUtil.file2pdf(fileInputStream, "F:\\临时文件\\openofficetest\\docx","docx");
}
}

yudaonihaha
- 粉丝: 0
- 资源: 13
最新资源
- 基匹配瞬变变换(BMCT)及其在致密砂岩气藏识别中的应用研究(可复现,有问题请联系博主)
- xfdashboard-themes-0.8.1-1.el8.x64-86.rpm.tar.gz
- FiddlerCore.Trial.5.0.2
- FORESEE UFS存储 Datasheet
- 嵌入式开发点阵字模生成工具(OLED/LCD适配版)- PCtoLCD2002完美版
- DeepSeek 超实用提问技巧大揭秘,让你的问题秒变高质量答案!
- xfdashboard-devel-0.8.1-1.el8.x64-86.rpm.tar.gz
- 物联网中UHF RFID系统的编程
- DeepSeek 超实用提问技巧大揭秘,让你的问题秒变高质量答案!
- 基于springboot框架的java毕业设计-民宿管理平台系统源码(高分毕设)
- xfdashboard-0.8.1-1.el8.x64-86.rpm.tar.gz
- 基于Inception模块的双向门控循环单元网络预测缺失声波测井数据(可复现,有问题请联系博主)
- 基于改进蛙跳算法优化BP神经网络的地层孔隙度预测模型研究与应用(可复现,有问题请联系博主)
- java毕业设计基于springboot框架的民宿管理平台系统源码(高分毕设)
- 绿色端口转发工具,端口映射
- xfconf-devel-4.16.0-1.el8.x64-86.rpm.tar.gz
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


