1、页面
var filename="<%=request.getParameter("filePath").toString()%>"; document.all.WebOffice1.OptionFlag |= 0x0080;//文件下载进度条 document.all.WebOffice1.LoadOriginalFile("/document/getfile.jsp?filename="+filename,"doc"); document.all.WebOffice1.SetToolBarButton2("Standard",1,0); document.all.WebOffice1.ShowToolBar = 1; function SaveToServer() { var returnValue; // 保存页面的返回值 document.all.WebOffice1.HttpInit(); // 初始化 Http 引擎 document.all.WebOffice1.SetTrackRevisions(0); document.all.WebOffice1.ShowRevisions(0); // 添加相应的 Post 元素 //document.all.WebOffice1.HttpAddPostString("username", "aganar"); var docname = null; var filename=null; if(docname!=null){ filename=""; }else{ filename="<%=request.getParameter("filePath").toString()%>"; } // 添加上传文件 document.all.WebOffice1.HttpAddPostCurrFile("DocContent", ""); // 提交上传文件 document.all.WebOffice1.HttpPost("/document/commonsfileupload.jsp?filename="+ filename.replace(/\/g,'\')); }
2、commonsfileupload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%> <%@ page import="org.apache.commons.fileupload.FileItem"%> <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%> <%@ page import="java.io.File"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'commonsfileupload.jsp' starting page</title> </head> <body> <% String filenameString=new String(request.getParameter("filename").getBytes("ISO-8859-1"),"UTF-8"); String uploadPath = request.getRealPath("/upload"); // 上传文件的目录 try { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 Iterator<FileItem> i = items.iterator(); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { // 获得文件名,这个文件名包括路径: String fileName = fi.getName(); // 在这里可以记录用户和文件信息 // 写入文件可以从fileName中提取文件名: fi.write(new File(filenameString)); } else if (fi.isFormField()) { String name = fi.getFieldName(); String value = fi.getString("UTF-8"); } } out.write("上传服务器成功!"); System.out.print("upload succeed"); } catch (Exception e) { // 可以跳转出错页面 e.printStackTrace(); out.write("上传失败!"); } %> </body> </html>