Java 實現線上開啟 word 文件 併發處理
阿新 • • 發佈:2018-11-19
1. 需求
防止多個使用者同時開啟一個檔案,出現編輯儲存檔案相互覆蓋的問題。
2. 頁面程式碼
(1)web.xml
<!-- PageOffice Begin --> <servlet> <servlet-name>poserver</servlet-name> <servlet-class>com.zhuozhengsoft.pageoffice.poserver.Server</servlet-class> </servlet> <servlet-mapping> <servlet-name>poserver</servlet-name> <url-pattern>/poserver.zz</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>poserver</servlet-name> <url-pattern>/sealsetup.exe</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>poserver</servlet-name> <url-pattern>/posetup.exe</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>poserver</servlet-name> <url-pattern>/pageoffice.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>poserver</servlet-name> <url-pattern>/jquery.min.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>poserver</servlet-name> <url-pattern>/pobstyle.css</url-pattern> </servlet-mapping> <servlet> <servlet-name>adminseal</servlet-name> <servlet-class>com.zhuozhengsoft.pageoffice.poserver.AdminSeal</servlet-class> </servlet> <servlet-mapping> <servlet-name>adminseal</servlet-name> <url-pattern>/adminseal.zz</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>adminseal</servlet-name> <url-pattern>/loginseal.zz</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>adminseal</servlet-name> <url-pattern>/sealimage.zz</url-pattern> </servlet-mapping> <mime-mapping> <extension>mht</extension> <mime-type>message/rfc822</mime-type> </mime-mapping> <context-param> <param-name>adminseal-password</param-name> <param-value>111111</param-value> </context-param> <!-- PageOffice End -->
(2)Default.jsp 頁面
<%@ page language="java" import="java.util.*,com.zhuozhengsoft.pageoffice.*" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>演示:併發控制</title> <!--PageOffice.js和jquery.min.js檔案一定要引用--> <script type="text/javascript" src="<%=request.getContextPath()%>/jquery.min.js"></script> <script type="text/javascript" src="<%=request.getContextPath()%>/pageoffice.js" id="po_js_main"></script> </head> <body style="font-size: 12px;"> <form id="form1" runat="server"> <div style="border: solid 1px RoyalBlue; width: 500px; text-align: center; height: 200px; margin: 100px auto;"> <div style="margin-top: 50px; height: 170px;"> <span style="color: Red;">操作說明:</span><span>首先點選“張三開啟檔案”會在彈出視窗中開啟Word文件(不要關閉視窗),<br />再點選“李四開啟檔案”,看併發控制效果。</span> <br /> <br /> 1:<a href="javascript:POBrowser.openWindowModeless('Word.jsp?userid=1','width=1200px;height=800px;');">張三開啟檔案</a> <br /> <br /> 2:<a href="javascript:POBrowser.openWindowModeless('Word.jsp?userid=2','width=1200px;height=800px;');">李四開啟檔案</a> </div> </div> </form> </body> </html>
(3)Wod.jsp
<%@ page language="java" import="java.util.*,com.zhuozhengsoft.pageoffice.*" pageEncoding="utf-8"%> <% String userName = "somebody"; String userId = request.getParameter("userid").toString(); if (userId.equals("1")) { userName = "張三"; } else { userName = "李四"; } PageOfficeCtrl poCtrl=new PageOfficeCtrl(request); poCtrl.setServerPage(request.getContextPath()+"/poserver.zz"); poCtrl.addCustomToolButton("儲存","Save",1); poCtrl.setSaveFilePage("SaveFile.jsp"); //設定併發控制時間 poCtrl.setTimeSlice(20); poCtrl.webOpen("doc/test.doc",OpenModeType.docNormalEdit,userName); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>最簡單的開啟儲存Word檔案</title> </head> <body> <script type="text/javascript"> function Save() { document.getElementById("PageOfficeCtrl1").WebSave(); } //文件關閉前先提示使用者是否儲存 function BeforeBrowserClosed(){ if (document.getElementById("PageOfficeCtrl1").IsDirty){ if(confirm("提示:文件已被修改,是否繼續關閉放棄儲存 ?")) { return true; }else{ return false; } } } </script> <form id="form1" > 當前使用者: <%=userName %>。 <div style=" width:auto; height:700px;"> <%=poCtrl.getHtmlCode("PageOfficeCtrl1")%> </div> </form> </body> </html>
(4)SaveFile.jsp
<%@ page language="java" import="java.util.*,com.zhuozhengsoft.pageoffice.*" pageEncoding="utf-8"%>
<%
FileSaver fs=new FileSaver(request,response);
fs.saveToFile(request.getSession().getServletContext().getRealPath("ConcurrencyCtrl/doc/")+"/"+fs.getFileName());
fs.close();
%>
3. 效果演示
4:注意:
(1)本示例為了相容所有的瀏覽器,用pageoffice提供的POBrowser方法. 用 POBrowser 的頁面比許引用 pageoffice.js 和 jquery.min.js 檔案.
pageoffic.js 檔案 和 jquey.min.js 檔案封裝到 jar 裡面了.只需要web.xml 裡面配置路徑就可以. 一般是當前專案的根目錄下. 參考上面的程式碼
<script type="text/javascript" src="<%=request.getContextPath()%>/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/pageoffice.js" id="po_js_main"></script>
引用pageoffice.js 的時候 id 必須寫
(2)開啟文件的時候會提示註冊
PageOffice V4.0 標準版試用序列號:IMTG6-BSXJ-JGZ6-3BIWM
(4)必須是不同的使用者開啟同一個文件.
b