我实现文件预览的方式是:
txt/doc/ppt/excel –>pdf –>swf
将txt/doc/ppt/excel 先转成pdf文件,再转成swf文件,最后展示在前台
需要的工具:
openoffice 、SWFTools、FlexPaper 、JODConverter
1.安装openoffice ,并通过dos窗口打开
openoffice 下载网址:http://www.openoffice.org/zh-cn/download/
下载安装后,通过dos找到安装路径,如:C:\Program Files (x86)\OpenOffice 4\program
执行dos命名:cd C:\Program Files (x86)\OpenOffice 4\program
继续输入 soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard 即可打开
- 下载安装swfTools
网址:http://www.swftools.org/download.html FlexPaper下载
网址:http://flexpaper.devaldi.com/download/
下载 download Web Server Package下载JODConverter 的jar包
网址:http://sourceforge.net/projects/jodconverter/files/JODConverter/2.2.2/
由于文件预览只是项目的一个小功能,所以我们将FlexPaper的js文件和FlexPaperView.swf文件(该文件为文件预览的模板文件,必须要有)放在如下路径下:
4.导入必须的JODConverter jar包
5.文件预览 fileManagerPreview.jsp
<html>
<head>
<c:import url="/pages/include/head.jsp"></c:import>
<link type="text/css" rel="stylesheet"
href="${ctx}/themes/default/css/icon.css" />
<script src="${ctx}/plugins/flexpaper/flexpaper_handlers.js"></script>
<script src="${ctx}/plugins/flexpaper/flexpaper.js"></script>
<script src="${ctx}/plugins/flexpaper/FlexPaperViewer.js"></script>
<style type="text/css" media="screen">
html, body { height:100%; }
body { margin:0; padding:0; overflow:auto; }
#flashContent { display:none; }
</style>
<title>文档在线预览系统</title>
</head>
<body>
<div style="position:absolute;left:50px;top:10px;">
<div id="documentViewer" class="flexpaper_viewer" style="width:770px;height:500px"></div>
<script type="text/javascript">
var startDocument = "Paper";
$('#documentViewer').FlexPaperViewer(
{ config : {
SWFFile : path+'/doc/'+'${file}',
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : true,
FullScreenAsMaxWindow : false,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
RenderingOrder : 'flash',
StartAtPage : '',
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
WMode : 'window',
localeChain: 'zh_CN'
}}
);
</script>
</div>
</body>
</html>
6.文件转换工具类:FileOperatorUtil.java
// 文件类型转换 Txt/Word/Excel/PPT=>PDF
public static void docToPdf(File docFile, File pdfFile) throws Exception{
System.out.println(Config .getConfig("openOffice_connection_port"));
OpenOfficeConnection connection = new SocketOpenOfficeConnection(Integer.parseInt(Config .getConfig("openOffice_connection_port")));
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");
} catch (java.net.ConnectException e) {
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;
}
}
@SuppressWarnings("all")
public static void pdf2swf(File pdfFile , File swfFile ) throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1) {// windows环境处理
try {
Process p = r.exec(Config.getConfig("pdf_to_swf")+" "+ 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已经存在不需要转换****");
}
}
static String loadStream(InputStream in) throws IOException {
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while ((ptr = in.read()) != -1) {
buffer.append((char) ptr);
}
return buffer.toString();
}
七。关键点提示:
到这一步 基本上就可以从界面上展现文件预览,可是部分朋友的界面展现只是一个空白的div。
一般的原因有以下几点:
- swf路径错误;
- FlexPaper控件中路径错误;
①swf路径错误的排除可以通过写死swfFile路径进行测试。
如:SWFFile : ‘http://localhost:8080/ipnet/doc/abcd.swf’
swffile文件必须是放在服务器上,硬盘上是预览不到的,因为flexpaper是需要与服务器通信来获得swf文件流。
②FlexPaper控件中路径错误
按照上文的目录结构,flexpaper的js文件放在WebRoot/plugins/flexpaper下,并且FlexPaperView.swf文件和flexpaper.js文件在同一目录下,
所以我们需要修改flexpaper.js的源码。
修改的js的目的是为确定flexpaper.js的路径和 让flexpaper.js能找到FlexPaperViewer.swf文件。