java实现文档的在线阅读功能
来源:互联网 发布:自攻螺丝 钻头 知乎 编辑:程序博客网 时间:2024/04/29 22:08
最近在工作中,遇到对各种类型的文档进行在线阅读,实现的思路是:如果文档类型不是pdf,则需要将文档通过openoffice组件转化为pdf文件,然后将pdf通过swftools转为swf文件,最后通过flexpaper显示swf文件,具体步骤如下:
1. 安装openoffice和swftools两个软件,我装的是openoffice3.2和swftools-0.9.2。
2. 以一个test.doc文件为例,
String fileSuffix = "......."; //文件的前缀,即"test"
String fileType = "........."; //文件类型,即"doc"
String fileRealPath = "..........."; //文件的实际路径
String flashFilePath = "......."; //临时存放生成的swf文件路径
String pdfFilePath = "......" //临时存放生成的pdf文件路径
//创建test.doc的swf文件,即test.swf
File flashFile = new File(flashFilePath + fileSuffix + "swf");
//如果test.swf文件不存在
if(!flashFile.exists()){
//1.如果文件为pdf格式,则只需将文件移动到/pdfTemp路径下,不需要openoffice转换了
//2.如果文件为其他格式,则需先将文件的格式转化为pdf格式
File sourceFile = new File(fileRealPath + fileSuffix +"." + fileType);
if(fileType.equals("pdf")){
//如果为pdf文件,则将其复制到临时存放生成的pdf文件路径
sourceFile.renameTo(new File(pdfFilePath, sourceFile.getName()));
}else{
File pdfFile = new File(pdfFilePath + fileSuffix + "." + "pdf");
//使用openoffice的API接口转化为pdf文件(大家可以网上下载jodconverter-2.2.2.zip以及例子程序,网上挺多的,这里由于空间有限,不能为大家上传,非常抱歉!)
JOD4DocToPDF.docToPdf(sourceFile, pdfFile);
}
try {
//将pdf文件转化为swf文件(第一个参数为swf文件的名字,第二个参数为pdf文件的名字,详细可参见下方的代码)
FlashPaper.convertPDF2SWF(fileSuffix, fileSuffix);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class FlashPaper extends Thread {
public static int convertPDF2SWF(String swfName,String documentName) throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
String pdfPath = request.getSession().getServletContext().getRealPath(pdfFilePath+documentName+".pdf");
String swfPath = request.getSession().getServletContext().getRealPath("swfFilePath+swfName+".swf");
////目标路径不存在则建立目标路径
File dest = new File(request.getSession().getServletContext().getRealPath("/swfTemp"));
if (!dest.exists()) dest.mkdirs();
//源文件不存在则返回
File source = new File(pdfPath);
if (!source.exists()) return 0;
//调用pdf2swf命令进行转换(安装swftools路径为 D盘根目录)
String command= "D:/Program Files/SWFTools/pdf2swf.exe -t \""+pdfPath+"\" -o \""+swfPath+"\" -s flashversion=9 -s languagedir=c://xpdftest//xpdf-chinese-simplified";
System.out.println("cmd:"+command);
// Process pro = Runtime.getRuntime().exec(command);
Process process = Runtime.getRuntime().exec(command); // 调用外部程序
final InputStream is1 = process.getInputStream();
new Thread(new Runnable() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(is1));
try {
while(br.readLine()!= null) ;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区
InputStream is2 = process.getErrorStream();
BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
StringBuilder buf = new StringBuilder(); // 保存输出结果流
String line = null;
while((line = br2.readLine()) != null) buf.append(line); // 循环等待ffmpeg进程结束
System.out.println("输出结果为:" + buf);
while (br2.readLine() != null);
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return process.exitValue();
}
}
3. 代码已完成,然后再通过flexpaper组件在前台页面显示swf文件,即可看到百度文库等网站类似的在线阅读效果。如何使用flexpaper显示swf文件,我会继续写下一篇新的文章。
- java实现文档的在线阅读功能
- Java 实现文档在线阅读功能
- Java学习之道:java实现文档的在线阅读功能
- Java实现文档在线阅读
- 实现类似百度文库的文档在线阅读功能
- Ubuntu 服务器 PHP实现word、excel、ppt、pdf 等文档在线阅读功能的实现
- 在线阅读实现(文档-->swf的转换)
- 20140731 java 《文档在线预览功能》的实现简述
- 百度文库和豆丁网的在线文档阅读功能
- FlexPaper+SwfTools实现的在线文档功能
- 文档在线查看功能的实现
- java零碎要点010---Java实现office文档与pdf文档的在线预览功能
- Java实现仿百度文库文档在线阅读
- Java实现仿百度文库文档在线阅读
- Java实现office文档与pdf文档的在线预览功能
- Java实现office文档与pdf文档的在线预览功能
- Java实现office文档与pdf文档的在线预览功能
- Java实现office文档与pdf文档的在线预览功能
- c#文本文件读取并可控制读取行
- Ping 127.0.0.1 与Ping 本机地址
- oracle复习
- Linux命令--替换 上分隔符 与touch小结合了一下
- python实现自动化远程控制(1)--画UML类图
- java实现文档的在线阅读功能
- android之Fragment(官网资料翻译)
- uva 10014 - Simple calculations
- NSString 转换特殊字符“&”“@”“#”等
- C与C++的细微区别——省略形式参数名
- 来到Android世界,还是要HelloWorld
- 让我们感动落泪的瞬间-- 那些、微小说
- 为已存在数据的表加主键
- linux命令--date