仿豆丁百度文库网页版阅读器完整解决方案
Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用openoffice转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
此过程涉及软件(实测)
openoffice3.2(window)
swftools-2011-01-23-1815.exe(window)
openoffice3.1.1(linux)
swftools-2011-01-23-1815.tar.gz(linux)
jar包jodconverter-2.2.2(里面lib目录下含多个要用到的jar文件)
flexpaper1.4.0 (直接复制过来,项目上运行成功,本地运行还没成功)
FlexPaper_1.4.5_flash(网上下载的最新版本没试过,二选一即可)
附上自己写的java转换Demo DocConverter.java
importjava.io.BufferedInputStream;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importcom.artofsolving.jodconverter.DocumentConverter;
importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
* doc docx格式转换
* @author Administrator
*
*/
public classDocConverter {
private static final int environment = 1;//环境 1:windows2:linux (只涉及pdf2swf路径问题)
privateString fileString;
privateString outputPath = "";//输入路径 ,如果不设置就输出在默认的位置
privateString fileName;
private FilepdfFile;
private FileswfFile;
private FiledocFile;
publicDocConverter(String fileString) {
ini(fileString);
}
/**
* 重新设置file
* @param fileString
*/
public voidsetFile(String fileString){
ini(fileString);
}
/**
* 初始化
* @param fileString
*/
private voidini(String fileString) {
this.fileString =fileString;
fileName =fileString.substring(0, fileString.lastIndexOf("."));
docFile = newFile(fileString);
pdfFile = newFile(fileName + ".pdf");
swfFile = newFile(fileName + ".swf");
}
/**
* 转为PDF
* @param file
*/
private voiddoc2pdf() throws Exception{
if(docFile.exists()){
if(!pdfFile.exists()){
OpenOfficeConnection connection = newSocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = newOpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
// close theconnection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");
} catch(java.net.ConnectException e) {
// TODOAuto-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
*/
private voidpdf2swf() throws Exception{
Runtime r = Runtime.getRuntime();
if(!swfFile.exists()){
if(pdfFile.exists()){
if(environment ==1){//windows环境处理
try {
Process p = r.exec("D:/TOOLS/SWFTools/pdf2swf.exe" +
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(IOException 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 e;
}
}
} else {
System.out.println("****pdf不存在,无法转换****");
}
} else {
System.out.println("****swf已经存在不需要转换****");
}
}
staticString loadStream(InputStream in) throwsIOException {
int ptr= 0;
in = newBufferedInputStream(in);
StringBuffer buffer = newStringBuffer();
while((ptr = in.read()) != -1 ) {
buffer.append((char)ptr);
}
returnbuffer.toString();
}
/**
* 转换主方法
*/
public booleanconver() {
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) {
// TODOAuto-generated catch block
e.printStackTrace();
return false;
}
if (swfFile.exists()){
return true;
} else {
return false;
}
}
/**
* 返回文件路径
* @param s
*/
publicString getswfPath(){
if(swfFile.exists()){
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
returntempString;
} else {
return "";
}
}
/**
* 设置输出路径
*/
public voidsetOutputPath(String outputPath){
this.outputPath =outputPath;
if(!outputPath.equals("")){
String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));
if(outputPath.charAt(outputPath.length())== '/'){
swfFile = newFile(outputPath + realName + ".swf");
} else {
swfFile = newFile(outputPath + realName + ".swf");
}
}
}
public static voidmain(String s[]) {
DocConverter d = newDocConverter("D:/data/FTP/midlongforecast/midweatherforecast/20010202.xml");
d.conver();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------
以上即是所有用到的转换需要用到的文件
windows安装过程省略,linux下安装swftools命令
解压到目录后
./configure
make
make install
无论在哪个平台安装完以上两个软件后
需要在将openoffice以服务方式启动
Window 启动方式
cd C:\ProgramFiles\OpenOffice.org 3\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"–nofirststartwizard
Linux启动
soffice-headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard &
以上是实现两个平台下的转换,但是会有openoffice解析不了的字体乱码问题,或者显示字体变形等
更高级的web文档应用可以参考QQ邮件,邮件实现了在线预览excel doc pdf等格式文件并可以用鼠标点击选取复制,期待高手解决.
转载于:https://blog.51cto.com/2342615/702090
仿豆丁百度文库网页版阅读器完整解决方案相关推荐
- 仿豆丁百度文库网页版阅读器完整解决方案,linux windows
最近实现了双平台下仿豆丁网百度文库阅读doc等文档的例子,分享给大家,也留做自己以后查找 先发张效果图 Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用op ...
- 《从案例中学习JavaScript》之实现网页版阅读器
###序 现在手机上的文本阅读app已经非常丰富,良好的阅读体验与海量的书库常常令我感到无比兴奋. 我想到8年前用一点几寸屏幕的mp3看电子书的情景,顿生一种淡淡的温馨.再久远一些,小的时候,我也经常 ...
- 百度文库中在线阅读器的基本思想
实现原理是这样的首先将这些文档转成PDF,然后使用pdf2swf.exe转成SWF文件,然后使用flexpaper播放 文档转PDF,可以使用jodconverter+openoffice 过程可以参 ...
- 百度高德位置定位服务器,调用百度、高德地图App,百度地图网页版,App定位
1.首先判断是否安装了目标地图App //判断是否安装目标应用 public static boolean isInstallByread(String packageName) { return n ...
- (android高仿系列)今日头条 --新闻阅读器 (一)
在模仿中循序渐进,以程序员角度去看待每一个APP是如何实现的,它有什么优缺点,并从中提升自己. 之前发现很多人在群里面.论坛上求网易新闻客户端的源码,之后我就去下了个网易新闻客户端和今日头条新闻客户端 ...
- pdf在线查看 html代码,PDF网页在线阅读器pdfjs
pdfjs是PDF网页在线阅读器,直接本地或远程加载PDF,设置输出文本字体.自适应页面显示.自定义头部底部显示. var doc = new pdfjs.Document({ font: asset ...
- (android高仿系列)今日头条 --新闻阅读器 (三) 完结 、总结 篇
从写第一篇今日头条高仿系列开始,到现在已经过去了1个多月了,其实大体都做好了,就是迟迟没有放出来,因为我觉得,做这个东西也是有个过程的,我想把这个模仿中一步一步学习的过程,按照自己的思路写下来,在根据 ...
- (android高仿系列)今日头条 --新闻阅读器 (二)
高仿今日头条 --- 第一篇:(android高仿系列)今日头条 --新闻阅读器 (一) 上次,已经完成了头部新闻分类栏目的拖动效果. 这篇文章是继续去完善APP 今日头条 这个新闻阅读器的其他功能 ...
- 百度文库手机版通信协议分析
下面表格中的操作的HTTP请求头中的HOST都必须是appwk.baidu.com.都是以POST的形式请求.POST的数据的格式都是: request={"Bdi_bear":& ...
最新文章
- ASP.NET用户控件的创建和使用
- python朗读网页-早上起床后不想动,让 Python 来帮你朗读网页吧
- python线下培训-Python培训线上和线下有什么区别?
- 利用python 提取log 文件里的关键句子,并进行统计分析
- how can you save more space at home?
- 关系数据库设计理论(函数依赖、异常、范式)、ER图
- 小练习——过滤掉出现次数最多的数据
- 第1章 数据库系统概述
- 查找树的指定层级_阿里面试,问了B+树,这个回答让我通过了
- 快速制作BP神经网络结构示意图
- svg的transform-matrix详解
- Wifi万能钥匙已经被淘汰了!Github这个开源工具太好用了!
- SQL 分页查询 返回总条数
- python中的zen原则到底是什么
- 6款良心本地视频播放器,功能强大还完全免费
- 胶原蛋白市场爆发,德国vikki如何在市场异军突起?
- c#语言求两个数最大公约数,C#趣味程序---求两个数的最大公约数和最小公倍数...
- Sitemesh小记
- [英语语法]词法之动词
- 如何在excel多条件筛选
热门文章
- Jenkins配置Findbugs做源代码安全扫描
- A5-1和DES两个加密算法的学习
- 对于经常需要truncate的表进行固定统计信息
- 软件设计和设计的问题
- iphone-common-codes-ccteam源代码 CCTestMacros.m
- 钻石2 D2 让你的diamond2待机2天,甚至2天以上的方法(绝对不是购买电池)
- Exchange中的数据库文件
- 4.Python的不堪一击到初学乍练(列表,元组)
- asp.net core结合NLog搭建ELK实时日志分析平台
- zbb20180710 maven Failed to read artifact descriptor--maven