效果:
客户端代码:
<html >
<head id="Head1" >
<title>Word文件浏览</title>
<script language="javascript" type="text/javascript">
function save_word(){
document.all.MyOffice.HttpInit();
document.all.MyOffice.HttpAddPostCurrFile("FileData", "myword.doc");
var err = document.all.MyOffice.HttpPost("http://10.153.195.111:6600/page/test/svcmain.jsp");
if (!err)
alert('保存失败!');
else
alert('保存成功!');
}
function show_word() {
document.all.MyOffice.Open("http://10.153.195.111:6600/page/tools/myword.doc", true, "Word.Document");
//document.all.MyOffice.CreateNew("Word.Document");
//进入不留痕状态
document.all.MyOffice.SetTrackRevisions(0);
//所有菜单都不可使用
document.all.MyOffice.SetMenuDisplay(0);
document.all.MyOffice.ShowView(6);
//接受当前修订
// document.all.MyOffice.SetTrackRevisions(4);
//document.all.MyOffice.close(); //关闭
}
</script>
</head>
<body onload="show_word();">
<form id="form1" >
<div><button onclick="save_word();">保存</button></div>
<object id="MyOffice" style="LEFT: 0px; TOP: 0px; width= 480px ; HEIGHT: 500px; overflow: scroll; overflow-x: hidden;" VIEWASTEXT name="MyOffice" "
classid="clsid:00460182-9E5E-11D5-B7C8-B8269041DD57" codebase=dsoframer.ocx#version=2,3,0,1>
<PARAM NAME="_ExtentX" VALUE="24739">
<PARAM NAME="_ExtentY" VALUE="16166">
<PARAM NAME="BorderColor" VALUE="-2147483632">
<PARAM NAME="BackColor" VALUE="-2147483643">
<PARAM NAME="ForeColor" VALUE="-2147483640">
<PARAM NAME="TitlebarColor" VALUE="-2147483635">
<PARAM NAME="TitlebarTextColor" VALUE="-2147483634">
<PARAM NAME="BorderStyle" VALUE="0">
<PARAM NAME="Titlebar" VALUE="0">
<PARAM NAME="Toolbars" VALUE="0">
<PARAM NAME="Menubar" VALUE="0">
<param name="Statusbar" value="0">
<param name="border" value="0">
<param name="scrollWidth" value="0">
<param name="MenuBarStyle" value="0">
</object>
</form>
</body>
</html>
服务端代码:
<%@ page contentType="text/html; charset=GBK"%>
<%request.setCharacterEncoding("iso-8859-1");%>
<%@ page import="java.io.*"%>
<%
System.out.println("======begin....");
InputStream in = request.getInputStream();
byte inputByte[] = new byte[2048];
File fp = new File("/oboss/test.doc");
FileOutputStream fout = new FileOutputStream(fp);
if ( -1 != in.read(inputByte)){
String str = new String(inputByte,"iso-8859-1");
int i = str.indexOf("Content-Type: application/msword");
if( i != -1)
{
String strleft = str.substring(i+ "Content-Type: application/msword\r\n\r\n".length());
fout.write(strleft.getBytes("iso-8859-1"));
}
}
while ( -1 != in.read(inputByte))
{
fout.write(inputByte);
}
fout.close();
in.close();
%>