最近项目需要用到文件的预览功能,就开始在网上收集资料,学习了几种文件预览的方法。我集成到我项目内测试的有以下三种,最后使用的是第三种:
直接使用别人提供的服务 例如:office web 365使用openoffice+swfTools+flexPaper实现使用openoffice+pdf.js实现
1.使用office web 365第三方服务点击进入官网;
特点:
实现起来简单,简单的不能再简单了,你只需要给它提供一个文件资源的链接就可以了。它支持各种文件类型的预览,而且能保存原有文件的各种动态效果,如PPT的动画效果,而且连压缩包都可以预览。也就是说,你在这上面预览文件和你在计算机上直接打开没有任何区别。能实现文件的缓存,减轻你服务器的压力。有免费版、标准版、高级版等多个版本,不同的版本对应的服务也不一样,免费版预览的文件大小不能超过5MB,每天预览次数(调用接口次数)不能超过500次,对于要求高的系统就必须使用付费版的了。安全问题,虽说office web提供了很高的安全性,但是总感觉将自己的东西放别人那里不会太安全。不适合局域网内的项目。
openoffice示例如下:
不需要部署、不需要编程,不需要做任何事情,只需要做个链接:点我预览,链接指向: http://officeweb365.com/o/?i=您的网站ID&furl=要预览的Office文件下载地址 例:
1 不需要部署、不需要编程,不需要做任何事情,只需要做个链接:点我预览,链接指向:
2 http://officeweb365.com/o/?i=您的网站ID
&furl=要预览的Office文件下载地址
3 例:
<a href="http://officeweb365.com/o/?i=1&furl=http://a.com/downfile/a.doc" target="_blank">点我预览
</a>
2.使用openoffice+swfTools+flexPaper实现
使用openOffice+swfTools+flexPaper实现文件预览功能的主要思想是:通过Java程序调用服务器上安装的软件openoffice,来将其它文件类型转化为pdf,然后调用swfTools将pdf转化为swf文件,在前端通过flexpaper插件来预览swf文件。
特点:
这个实现起来相对复杂一点,需要在服务器上提前安装openOffice和swfTools软件,然后在程序里调用这些软件来完成文件类型的转换。好像不支持中文路径和中文文件名,要想支持中文文件名,需要反编译flexpaper然后再。。。反正很麻烦这个的资料很多,因此网上可以找很多教程来帮助你完成配置。
具体的实现可以参考下这个博客:OpenOffice+SwfTools+flexpaper实现文件预览
3.使用openoffice+pdf.js
在我的项目中采用的就是这种方法,pdf.js的显示效果比较好,而且可以根据自己的项目需要完成相关的配置。下面说一下我的项目中具体使用:
1.首先安装openOffice,安装成功之后在命令行启动该服务,如下图:进入安装目录下的program目录,然后执行命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"
查看运行的进程内有没有soffice.bin,有的话则启动成功。
2.然后在项目内导入如下jar包:
jodconverter-2.2.2.jarridl-3.2.1.jarcommons.io.jarjuh.jarjurt.jarunoil.jarslf4j-api-1.7.13.jarslf4j-jdk14-1.7.13.jarxstream-1.4.1.jar
3.编写Java代码完成其它文件到pdf的转化:直接初始化将源文件路径,以及要转化为的pdf的文件路径传递进去即可,代码如下
1 package com.ams.util.server;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.BufferedReader;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.InputStreamReader;
12
13 import com.artofsolving.jodconverter.DocumentConverter;
14 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
15 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
16 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
17
18 public class DocConverter {
19
20 private String fileName;
21 private File pdfFile;
22 private File docFile;
23 private File odtFile;
24
25
26
27 public DocConverter(String sourceFile,String targetFile)
throws Exception {
28
29 fileName = sourceFile.substring(0, sourceFile.lastIndexOf("/"
));
30 docFile =
new File(sourceFile);
//得到转换前的文件
31 //得到文件的不带后缀的名字s
32 String s = sourceFile.substring(sourceFile.lastIndexOf("/") + 1,sourceFile.lastIndexOf("."
));
33 fileName = fileName + "/" +
s;
34 // 用于处理TXT文档转化为PDF格式乱码,获取上传文件的名称(不需要后面的格式_
35 String txtName = sourceFile.substring(sourceFile.lastIndexOf("."));
//得到文件格式
36 // 判断上传的文件是否是TXT文件
37 if (txtName.equalsIgnoreCase(".txt"
)) {
38 //是txt则需要转化为odt,然后再转为pdf
39 odtFile =
new File(fileName + ".odt"
);
40 // 将上传的文档重新copy丿份,并且修改为ODT格式,然后有ODT格式转化为PDF格式
41 this.copyFile(docFile, odtFile);
42 pdfFile =
new File(targetFile);
// 用于处理PDF文档
43 }
else if (txtName.equals(".pdf") || txtName.equals(".PDF"
)) {
44 pdfFile =
new File(targetFile);
45 this.copyFile(docFile, pdfFile);
46 }
else{
47 pdfFile =
new File(targetFile);
48 }
49 doc2pdf();
//调用函数进行转换
50 }
51 private void copyFile(File sourceFile,File targetFile)
throws Exception{
52 //新建文件输入流并对它进行缓冲
53 FileInputStream input =
new FileInputStream(sourceFile);
54 BufferedInputStream inBuff =
new BufferedInputStream(input);
55 // 新建文件输出流并对它进行缓冲
56 FileOutputStream output =
new FileOutputStream(targetFile);
57 BufferedOutputStream outBuff =
new BufferedOutputStream(output);
58 // 缓冲数组
59 byte[]b =
new byte[1024 * 5
];
60 int len;
61 while((len = inBuff.read(b)) != -1
){
62 outBuff.write(b,0
,len);
63 }
64 // 刷新此缓冲的输出浿
65 outBuff.flush();
66 // 关闭流
67 inBuff.close();
68 outBuff.close();
69 output.close();
70 input.close();
71 }
72 private void doc2pdf()
throws Exception {
73 if (docFile.exists()) {
74 if (!
pdfFile.exists()) {
75 OpenOfficeConnection connection =
new SocketOpenOfficeConnection(8100
);
76 try {
77 connection.connect();
78 DocumentConverter converter =
new OpenOfficeDocumentConverter(connection);
79 converter.convert(docFile, pdfFile);
80 // close the connection
81 connection.disconnect();
82 System.out.println("****pdf转换成功,PDF输出_" + pdfFile.getPath() + "****"
);
83 }
catch (java.net.ConnectException e) {
84 // ToDo Auto-generated catch block
85 e.printStackTrace();
86 System.out.println("****swf转换异常,openoffice服务未启动!****"
);
87 throw e;
88 }
catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
89 e.printStackTrace();
90 System.out.println("****swf转换器异常,读取转换文件失败****"
);
91 throw e;
92 }
catch (Exception e) {
93 e.printStackTrace();
94 throw e;
95 }
96 }
else {
97 System.out.println("****已经转换为pdf,不霿要再进行转化****"
);
98 }
99 }
else {
100 System.out.println("****swf转换器异常,霿要转换的文档不存在,无法转换****"
);
101 }
102 }103 }
4.前端需要导入pdf.js插件,然后将返回的结果动态添加到viewer.js中的DEFAULT_URL中即可,在这里面可以进行各种现实效果的配置。
以上就是我实现文件预览的几种方法,希望能和广大网友共同学习和进步!Fighting!