Java 實如今線打開 word 文檔 併發處理

1. 需求javascript

防止多個用戶同時打開一個文件,出現編輯保存文件相互覆蓋的問題。css

2. 頁面代碼html

(1)web.xmljava

<!-- 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 頁面jquery

<%@ 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.jspweb

<%@ 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:注意:app

(1)本示例爲了兼容全部的瀏覽器,用pageoffice提供的POBrowser方法.   用 POBrowser 的頁面比許引用 pageoffice.js 和 jquery.min.js 文件.less

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

相關文章
相關標籤/搜索