word文件在頁面上瀏覽,類似百度文庫形式
1、伺服器上已經上傳有需要的word文件,目前需要做的是將word文件按百度文庫的樣子顯示在頁面上,直接顯示不可以,需要兩個軟體,openoffice和swftools工具,其中openoffice安裝在C:/Program File/OpenOffice 4/下,啟動openoffice服務是在cmd視窗中
進入OpenOffice 4/program 目錄下:
soffice –headless –accept=”socket,host=127.0.0.1,port=8100;urp;”–nofirststartwizard&
回車,雖然沒有反饋,但是服務啟動了。
Swftools工具安裝目錄:C:/Program Files (x86)/swftools ------------程式中需要使用到路徑。
上面是使用的控制元件,準備完成。
2、jar包:需要的jar包:slf4j-api-1.5.6.jar 和 slf4j-log4j12-1.5.6.jar.
3、下面是FileConverterService.java類 :
作用:專門轉換word到pdf,再轉pdf到swf格式,在jsp頁面呼叫。
檔名中如果有空格要去掉,有空格不可以完成轉換。
package com.sjq.dwjs.service.fileConverter; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; 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; /** * doc docx格式轉換 */ public class FileConverterService { private static final int environment = 1;// 環境 1:windows 2:linux private String fileString;// (只涉及pdf2swf路徑問題) private String outputPath = "";// 輸入路徑 ,如果不設定就輸出在預設的位置 private String fileName; private File pdfFile; private File swfFile; private File docFile; public FileConverterService(String fileString) { ini(fileString); } /** * 重新設定file * * @param fileString */ public void setFile(String fileString) { ini(fileString); } /** * 初始化 * * @param fileString */ private void ini(String fileString) { this.fileString = fileString; //此處檔名中有空格的要去除空格,有空格時不可以完成轉換。 fileName = fileString.substring(0, fileString.lastIndexOf(".")).trim(); docFile = new File(fileString); pdfFile = new File(fileName + ".pdf"); swfFile = new File(fileName + ".swf"); } /** * 轉為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) { 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 { Process p = r.exec("C:/Program Files (x86)/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已經存在不需要轉換****"); } } 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(); } /** * 轉換主方法 */ @SuppressWarnings("unused") 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) { 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; } else { return ""; } } /** * 設定輸出路徑 */ public void setOutputPath(String outputPath) { this.outputPath = outputPath; if (!outputPath.equals("")) { String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf(".")); if (outputPath.charAt(outputPath.length()) == '/') { swfFile = new File(outputPath + realName + ".swf"); } else { swfFile = new File(outputPath + realName + ".swf"); } } } }
4、tomcat伺服器上的word文件存放在WebRoot/upload下
Jbxx.jsp儲存路徑WebRoot/jsp/wlxy/Jbxx.jsp
Display.jsp 儲存路徑 WebRoot/jsp/wlxy/Display.jsp
5、Jbxx.jsp如下:<% try{ String sql = "select id,wjmc from 表名 where glid = '"+id+"' "; rs1 = stmt1.executeQuery(sql); while(rs1.next()){ t_fjid = blanknull(rs1.getString("id")); wjmc = blanknull(rs1.getString("wjmc")); out.println("<tr><td align='left' style='font-size:10pt;padding-bottom:10px;' >"); out.println("<a href=\"javascript:displayFj('"+wjcclj+"')\">"+wjmc+"</a> "); } rs1.close(); }catch(Exception e){ e.printStackTrace(); } wjcclj = application.getRealPath("/")+"upload\\"+wjcclj.replace("+","%20"); //這樣得到的路徑是word的實際路徑:D:\tomcat_dwjs\webapps\upload\word文件名稱.docx FileConverterService d = new FileConverterService(wjcclj); //呼叫FileConverterService.java類中的conver方法開始轉換,先執行doc2pdf()將office檔案轉換為pdf;再執行pdf2swf()將pdf轉換為swf; d.conver(); //呼叫getswfPath()方法,列印轉換後的swf檔案路徑 System.out.println(d.getswfPath()); //生成swf相對路徑,以便傳遞給flexpaper播放器 swfpath = d.getswfPath(); //將相對路徑放入sessio中儲存 %>
6、Display.jsp頁面如下:
要注意的是路徑中文亂碼。<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.net.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String swfPath="";
if(request.getParameter("swfPath")!=null&& !"".equals(request.getParameter("swfPath"))){
swfPath=request.getParameter("swfPath");
swfPath=URLDecoder.decode(swfPath,"UTF-8");
}
swfPath = basePath+"upload/"+swfPath;//重要,這樣的路徑是URL訪問路徑:http://10.23.23.43:88/dwjs/upload/word文件名稱.docx
System.out.println(swfPath+"!!!!!!!!!!!!!!!!");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'knowledge_displayFj.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript' src='../../js/swfobject.js'></script>
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript" src="../../js/flexpaper_flash.js"></script>
<script type="text/javascript" src="../../js/flexpaper_flash_debug.js"></script>
</head>
<body>
<div align="center">
<div class="contentshow" style="margin-top:40px;">
<div id='flashContent' style='text-align:center; padding-top:10px;'>
檔案查看出!
</div>
<script type='text/javascript'>
var swfVersionStr = '10.0.0';
var xiSwfUrlStr = '../../js/expressInstall.swf';
var swfFile ='<%=swfPath%>';
var flashvars = {
ObjectID : 'FlexPaperViewer',
SwfFile : encodeURI(swfFile),
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : true,
PrintEnabled : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : true,
localeChain: 'zh_CN'
};
var params = {
}
params.quality = 'high';
params.bgcolor = '#ffffff';
params.allowscriptaccess = 'sameDomain';
params.allowfullscreen = 'true';
params.wmode = 'opaque';
var attributes = {};
attributes.id = 'FlexPaperViewer';
attributes.name = 'FlexPaperViewer';
swfobject.embedSWF('../../js/FlexPaperViewer.swf',
'flashContent','732', '550',
swfVersionStr, xiSwfUrlStr,flashvars, params, attributes);
swfobject.createCSS('#flashContent', 'display:block;text-align:left;');
</script>
</div>
</div>
</body>
</html>
其中expressInstall.swf,FlexPaperViewer.swf檔案可以在網上下載,存放路徑:WebRoot/js/下。
需要的js包swfobject.js。