欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 实现在线打开 word 文档 并发处理

程序员文章站 2022-06-02 22:58:02
...

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. 效果演示

Java 实现在线打开 word 文档 并发处理
            
    
    博客分类: 在线编辑wordjava操作word javajavascript在线office 

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