Java Web Office文档在线编辑1、创建页面
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=utf-8"> <title>Office在线编辑</title>
</head>
<body>
<script language="javascript">
function CreateDoc()
{
document.all.OA1.CreateNew( "Word.Document");
}
function CreateExcel()
{
document.all.OA1.CreateNew( "Excel.Sheet");
}
function CreatePPT()
{
document.all.OA1.CreateNew( "PowerPoint.Slide");
}
function OpenFromLocal()
{
document.all.OA1.OpenLocalDialog();
}
function ShowToolbars()
{
var x = document.all.OA1.GetToolbarsIsShow();
document.all.OA1.ShowToolbars( !x);
}
function PrintFile()
{
if(document.all.OA1.IsOpen())
{
document.all.OA1.Print();
java创建文件
}
}
function DocIsDirty()
{
if(document.all.OA1.IsOpen())
{
if(document.all.OA1.IsDirty())
{
window.alert("The file has been modified!");
}
else
{
window.alert("The file hasn't been modified!");
}
}
}
function SaveAs()
{
document.all.OA1.SaveLocalDialog();
}
function CloseFile()
{
if(document.all.OA1.IsOpen())
{
document.all.OA1.Close();
}
}
function OpenFromServer()
{
var sPath= window.prompt("服务器文件路径:",
"192.168.1.216:6888/msbb/tmp/office/test.doc");
document.all.OA1.OpenWebFile(sPath)
}
function SavetoServer()
{
if(document.all.OA1.IsOpen())
{
window.alert("保存文件到服务器!");
document.all.OA1.SaveWebFile("192.168.1.216:6888/msbb/UpLoadServlet");
}
}
</script>
<object classid="clsid:18A295DA-088E-42D1-BE31-5028D7F9B9B5"id="OA1"width="100%" height="700"
codebase="192.168.1.216:6888/msbb/tmp/office/OA.CAB#3,1,0,4"> <param name="_Version"value="65536">
<param name="_ExtentX"value="17859">
<param name="_ExtentY"value="10848">
<param name="_StockProps"value="0">
<param name="BorderStyle"value="1">
<param name="BorderColor"value="16777215">
<param name="CaptionColor"value="12632256">
<param name="CaptionLabel"value="等待打开一个 Office 文档!">
</object>
<input type="button"onClick="CreateDoc()"value="新建 Word">
<input type="button"onClick="CreateExcel()"value="新建 Excel">
<input type="button"onClick="CreatePPT()"value="新建 PPT">
<input type="button"onClick="OpenFromLocal()"value="打开">
<input type="button"onClick="ShowToolbars()"value="显示/隐藏工具栏">
<input type="button"onClick="DocIsDirty()"value="是否修改">
<input type="button"onClick="PrintFile()"value="打印">
<input type="button"onClick="SaveAs()"value="保存">
<input type="button"onClick="CloseFile()"value="关闭">
<br>
<input type="button"onClick="OpenFromServer()"value="从服务器打开">
<form>
<input type="button"onClick="SavetoServer()"value="保存到服务器">
</form>
</body>
</html>
2、创建文件保存到服务器Servlet
package tmp;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import login.Constants;
import apachemons.fileupload.servlet.ServletFileUpload;
import apachemons.fileupload.util.Streams;
public class UpLoadServlet extends HttpServlet
{
public void init() throws ServletException
{
}
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// processFileUpload(req, resp);
processFileUpload(req);
}
public void processFileUpload(HttpServletRequest request) throws IOException
{
// 读取控件输入流
InputStream stream = InputStream();
// 将输入流保存到文件
FileOutputStream savefile = new FileOutputStream(Constants.FILEPA TH + UUID.randomUUID() + ".doc");
byte[] bytes = new byte[1024];
int len;
while ((len = ad(bytes)) != -1)
{
savefile.write(bytes, 0, len);
savefile.flush();
}
// 关闭文件
stream.close();
// 关闭输入流
savefile.close();
}
public void processFileUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletFileUpload upload = new ServletFileUpload();
InputStream stream = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
stream = InputStream();
String filename = "ddd.doc";
String fileId = UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));// 取扩展名bis = new BufferedInputStream(stream);
bos = new BufferedOutputStream(new FileOutputStream(new File(Constants.FILEPATH + fileId)));
} catch (Exception e)
{
} finally
{
if (stream != null)
{
try
{
stream.close();
} catch (Exception e)
{
}
}
if (bis != null)
{
try
{
bis.close();
} catch (Exception e)
{
}
}
if (bos != null)
{
try
{
bos.close();
} catch (Exception e)
{
}
}
}
}
public void destroy()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论