1.下載安裝包
(1)安裝openoffice:使用時需要開啟服務(命令開啟:
cd C:\Program Files (x86)\OpenOffice 4\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
)
(2)安裝安裝swfTools
(3)下載flexpaper(只需要用到js文件和FlexPaperViewer.swf:
flexpaper_flash_debug.js
flexpaper_flash.js
jquery.js
)
(4))下載OpenDocument
2.創建上傳documentUpload.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>文檔在線預覽系統</title> 8 <style> 9 body {margin-top:100px;background:#fff;font-family: Verdana, Tahoma;} 10 a {color:#CE4614;} 11 #msg-box {color: #CE4614; font-size:0.9em;text-align:center;} 12 #msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px;} 13 #msg-box .title {font-size:1.4em;font-weight:bold;margin:0 0 30px 0;} 14 #msg-box .nav {margin-top:20px;} 15 </style> 16 17 </head> 18 <body> 19 <div id="msg-box"> 20 <form name="form1" method="post" enctype="multipart/form-data" action="docUploadConvertAction.jsp"> 21 <div class="title"> 22 正在上傳...... 23 </div> 24 <p> 25 <input name="file1" type="file"> 26 </p> 27 <p> 28 <input type="submit" name="Submit" value="上傳"> 29 </p> 30 </form > 31 </div> 32 </body> 33 </html>
3.后台文件轉化類
package com.upload; 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 DocConverter { 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 DocConverter(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(".")); 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); 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("D:/Program Files/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"); } //Linux
//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.創建文檔上傳轉換處理文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.io.*"%> <%@page import="java.util.Enumeration"%> <%@page import="com.oreilly.servlet.MultipartRequest"%> <%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%> <%@page import="com.cectsims.util.DocConverter"%> <% //文件上傳采用cos組件上傳,可更換為commons-fileupload上傳,文件上傳后,保存在upload文件夾 //獲取文件上傳路徑 String saveDirectory =application.getRealPath("/")+"upload"; //打印上傳路徑信息 System.out.println(saveDirectory); //每個文件最大50m int maxPostSize = 50 * 1024 * 1024 ; //采用cos缺省的命名策略,重名后加1,2,3...如果不加dfp重名將覆蓋 DefaultFileRenamePolicy dfp = new DefaultFileRenamePolicy(); //response的編碼為"UTF-8",同時采用缺省的文件名沖突解決策略,實現上傳,如果不加dfp重名將覆蓋 MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,"UTF-8",dfp); //MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,"UTF-8"); //輸出反饋信息 Enumeration files = multi.getFileNames(); while (files.hasMoreElements()) { System.err.println("ccc"); String name = (String)files.nextElement(); File f = multi.getFile(name); if(f!=null){ String fileName = multi.getFilesystemName(name); //獲取上傳文件的擴展名 String extName=fileName.substring(fileName.lastIndexOf(".")+1); //文件全路徑 String lastFileName= saveDirectory+"\\" + fileName; //獲取需要轉換的文件名,將路徑名中的'\'替換為'/' String converfilename = saveDirectory.replaceAll("\\\\", "/")+"/"+fileName; System.out.println(converfilename); //調用轉換類DocConverter,並將需要轉換的文件傳遞給該類的構造方法 DocConverter d = new DocConverter(converfilename); //調用conver方法開始轉換,先執行doc2pdf()將office文件轉換為pdf;再執行pdf2swf()將pdf轉換為swf; d.conver(); //調用getswfPath()方法,打印轉換后的swf文件路徑 System.out.println(d.getswfPath()); //生成swf相對路徑,以便傳遞給flexpaper播放器 String swfpath = "upload"+d.getswfPath().substring(d.getswfPath().lastIndexOf("/")); System.out.println(swfpath); //將相對路徑放入sessio中保存 session.setAttribute("swfpath", swfpath); out.println("上傳的文件:"+lastFileName); out.println("文件類型"+extName); out.println("<hr>"); } } %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style> body {margin-top:100px;background:#fff;font-family: Verdana, Tahoma;} a {color:#CE4614;} #msg-box {color: #CE4614; font-size:0.9em;text-align:center;} #msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px;} #msg-box .title {font-size:1.4em;font-weight:bold;margin:0 0 30px 0;} #msg-box .nav {margin-top:20px;} </style> </head> <body> <div> <form name="viewForm" id="form_swf" action="documnetView.jsp" method="POST"> <input type='submit' value='預覽' class='BUTTON SUBMIT'/> </form> </div> </body> </html>
5.預覽頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String swfFilePath=session.getAttribute("swfpath").toString(); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <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> <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;"> <a id="viewerPlaceHolder" style="width:820px;height:650px;display:block"></a> <script type="text/javascript"> var fp = new FlexPaperViewer( 'FlexPaperViewer', //這里是FlexPaperViewer.swf的路徑,相對根目錄 'viewerPlaceHolder', //這里是要顯示Swf的區域的ID
{ config : { SwfFile : escape('<%=swfFilePath%>'), //這里是要顯示的swf的位置 Scale : 0.6, //縮放比例 ZoomTransition : 'easeOut', //Flexpaper中縮放樣式,它使用和Tweener一樣的樣式,默認參數值為easeOut.其他可選值包括: easenone, easeo //ut, linear, easeoutquad ZoomTime : 0.5, //從一個縮放比例變為另外一個縮放比例需要花費的時間,該參數值應該為0或更大。 ZoomInterval : 0.2, //縮放比例之間間隔,默認值為0.1,該值為正數。 FitPageOnLoad : true, //初始化的時候自適應頁面,與使用工具欄上的適應頁面按鈕同樣的效果。 FitWidthOnLoad : false,//初始化的時候自適應頁面寬度,與工具欄上的適應寬度按鈕同樣的效果。 FullScreenAsMaxWindow : false, //是否支持全屏,當設置為true的時候,單擊全屏按鈕會打開一個flexpaper最大化的新窗口而不是全屏,當由於fla //sh播放器因為安全而禁止全屏,而使用flexpaper作為獨立的flash播放器的時候設置為true是個優先選擇。 ProgressiveLoading : false, //當設置為true的時候,展示文檔時不會加載完整個文檔,而是逐步加載,但是需要將文檔轉化為9以上的flash版本(使 //用pdf2swf的時候使用-T 9 標簽)。 MinZoomSize : 0.2, //最小的縮放比例。 MaxZoomSize : 5, //設置最大的縮放比例。 SearchMatchAll : false, //設置為true的時候,單擊搜索所有符合條件的地方高亮顯示。 InitViewMode : 'SinglePage', //啟動模式 ViewModeToolsVisible : true,//工具欄上是否顯示樣式選擇框(就是顯示縮略圖或分頁顯示的工具) ZoomToolsVisible : true, //工具欄上是否顯示縮放工具 NavToolsVisible : true, //工具欄上是否顯示導航工具(也就是頁碼工具) CursorToolsVisible : true, //工具欄上是否顯示光標工具 SearchToolsVisible : true, //工具欄上是否顯示搜索 localeChain: 'en_CN' //語言 }}); </script> </div> </body> </html>
6.flexpaper所需jar:cos.jar
