package com.estone.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 DocConverter {
private String SWFTools_Windows = "F:/sortware/testingsoftware/SWFTools/pdf2swf.exe ";
private String SWFTools_Linux = "F:/sortware/testingsoftware/SWFTools/pdf2swf.exe ";
private static final int environment = 1;// 环境1:windows,2:linux(涉及pdf2swf路径问题)
private String fileString;
private String outputPath = "";// 输入路径,如果不设置就输出在默认位置
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile;
private File odtFile;
public DocConverter(String fileString) {
ini(fileString);
}
/*
* 重新设置 file @param fileString
*/
public void setFile(String fileString) {
ini(fileString);
}
/*
* 初始化 @param fileString
*/
private void ini(String fileString) {
try {
this.fileString = fileString;
fileName = fileString.substring(0, fileString.lastIndexOf("/"));
docFile = new File(fileString);
String s = fileString.substring(fileString.lastIndexOf("/") + 1,fileString.lastIndexOf("."));
fileName = fileName + "/" + s;
// 用于处理TXT文档转化为PDF格式乱码,获取上传文件的名称(不需要后面的格式)
String txtName = fileString.substring(fileString.lastIndexOf("."));
// 判断上传的文件是否是TXT文件
if (txtName.equalsIgnoreCase(".txt")) {
// 定义相应的ODT格式文件名称
odtFile = new File(fileName + ".odt");
// 将上传的文档重新copy一份,并且修改为ODT格式,然后有ODT格式转化为PDF格式
this.copyFile(docFile, odtFile);
pdfFile = new File(fileName + ".pdf"); // 用于处理PDF文档
} else if (txtName.equals(".pdf") || txtName.equals(".PDF")) {
pdfFile = new File(fileName + ".pdf");
this.copyFile(docFile, pdfFile);
} else {
pdfFile = new File(fileName + ".pdf");
}
swfFile = new File(fileName + ".swf");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Title: copyFile
* @Description: TODO
* @param: @param docFile2
* @param: @param odtFile2
* @return: void
* @author: hl
* @time: 2014-4-17 下午9:41:52
* @throws
*/
private void copyFile(File sourceFile,File targetFile)throws Exception{
//新建文件输入流并对它进行缓冲
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff = new BufferedInputStream(input);
// 新建文件输出流并对它进行缓冲
FileOutputStream output = new FileOutputStream(targetFile);
BufferedOutputStream outBuff = new BufferedOutputStream(output);
// 缓冲数组
byte[]b = new byte[1024 * 5];
int len;
while((len = inBuff.read(b)) != -1){
outBuff.write(b,0,len);
}
// 刷新此缓冲的输出流
outBuff.flush();
// 关闭流
inBuff.close();
outBuff.close();
output.close();
input.close();
}
/*
* 转为PDF @param file
*/
private void doc2pdf() throws Exception {
if (docFile.exists()) {
if (!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
// close the connection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
} catch (java.net.ConnectException e) {
// ToDo Auto-generated catch block
e.printStackTrace();
System.out.println("****swf转换异常,openoffice服务未启动!****");
throw e;
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,读取转换文件失败****");
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
} else {
System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
}
}
/*
* 转换成swf
*/
@SuppressWarnings("unused")
private void pdf2swf() throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1){// windows环境处理
try {
// 这里根据SWFTools安装路径需要进行相应更改
Process p = r.exec(SWFTools_Windows + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} else if (environment == 2){// linux环境处理
try {
Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
} else {
System.out.println("****pdf不存在,无法转换****");
}
} else {
System.out.println("****swf已存在不需要转换****");
}
}
static String loadStream(InputStream in) throws IOException {
int ptr = 0;
//把InputStream字节流 替换为BufferedReader字符流 2013-07-17修改
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder buffer = new StringBuilder();
while ((ptr = reader.read()) != -1) {
buffer.append((char) ptr);
}
return buffer.toString();
}
/*
* 转换主方法
*/
public boolean conver() {
if (swfFile.exists()) {
System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
return true;
}
if (environment == 1) {
System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
} else {
System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
}
try {
doc2pdf();
pdf2swf();
} catch (Exception e) {
// TODO: Auto-generated catch block
e.printStackTrace();
return false;
}
if (swfFile.exists()) {
return true;
} else {
return false;
}
}
/*
* 返回文件路径 @param s
*/
public String getswfPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
没有合适的资源?快使用搜索试试~ 我知道了~
Java实现文档在线预览demo(openoffice+swfTools+FlexPaper)
共62个文件
jar:20个
js:9个
jsp:7个
5星 · 超过95%的资源 需积分: 44 887 下载量 45 浏览量
2014-04-21
17:02:54
上传
评论 15
收藏 5.51MB ZIP 举报
温馨提示
Java实现文档在线预览(openoffice+swfTools+FlexPaper)实现的一个简单demo
资源推荐
资源详情
资源评论
收起资源包目录
ctcesims.zip (62个子文件)
ctcesims
.settings
com.genuitec.eclipse.migration.prefs 219B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 414B
org.eclipse.core.resources.prefs 108B
org.eclipse.wst.common.component 486B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 395B
src
com
estone
test
DocConverter.java 8KB
weather
Json.java 3KB
.project 2KB
WebRoot
css
flexpaper.css 29KB
upload
documentView副本.html 2KB
1233.html 2KB
docs
Paper.pdf.swf 532KB
META-INF
MANIFEST.MF 36B
Paper.swf 527KB
WEB-INF
web.xml 404B
classes
com
estone
test
DocConverter.class 7KB
weather
Json.class 3KB
lib
jodconverter-cli-2.2.2.jar 6KB
slf4j-api-1.5.6.jar 22KB
commons-logging-1.0.4.jar 37KB
commons-beanutils.jar 184KB
unoil-3.0.1.jar 1.3MB
ridl-3.0.1.jar 248KB
jurt-3.0.1.jar 106KB
commons-httpclient.jar 298KB
jodconverter-2.2.2.jar 31KB
json-lib-2.2-jdk15.jar 131KB
commons-collections-3.2.jar 558KB
ezmorph-1.0.5.jar 84KB
commons-cli-1.2.jar 40KB
cos.jar 55KB
morph-1.1.1.jar 286KB
commons-lang.jar 237KB
juh-3.0.1.jar 54KB
xstream-1.3.1.jar 421KB
slf4j-jdk14-1.5.6.jar 9KB
commons-io-1.4.jar 106KB
documentUpload.jsp 1KB
documentView.jsp 2KB
FlexPaperViewer.swf 402KB
js
jquery.js 89KB
swfobject.js 10KB
flexpaper_flash_debug.js 11KB
flexpaper_flash.js 10KB
js1
jquery.min.js 90KB
flexpaper.js 17KB
expressInstall.swf 727B
flexpaper_handlers_debug.js 4KB
swfobject.js 10KB
documentUpload.jsp 1KB
documentView.jsp 2KB
FlexPaperViewer.swf 421KB
flexpaper.css 29KB
docUploadConvertAction.jsp 3KB
flexpaper_handlers.js 3KB
index.jsp 922B
docUploadConvertAction.jsp 4KB
.mymetadata 297B
.classpath 3KB
.myeclipse
共 62 条
- 1
毅个混蛋
- 粉丝: 62
- 资源: 39
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于yolov5识别算法实现的DNF自动脚本源码.zip
- 基于Python实现的自动化办公项目.zip
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页