servlet配置的整个过程
步骤:
1.页面
if(upForm.getForm().isValid()){ upForm.form.submit({ waitMsg : '正在上传文件,请稍候......',//提示信息 waitTitle : '请稍候',//标题 url : jutil.getRootPath()+'/nurseInfoImport.do', method : 'post', success : function(form, action) { Ext.MessageBox.alert('提交成功', action.result.msg); }, failure:function(form,action){//加载失败的处理函数 Ext.Msg.alert('提示','导入失败!'); }});}
<servlet> <servlet-name>nurseInfoImport</servlet-name> <servlet-class>com.alensic.nursing.sysmgr.NurseInfoImportServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>nurseInfoImport</servlet-name> <url-pattern>/nurseInfoImport.do</url-pattern> </servlet-mapping>
package com.alensic.nursing.sysmgr;import java.io.FileInputStream;import java.io.IOException;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.tomcat.util.http.fileupload.DiskFileUpload;public class NurseInfoImportServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { System.out.println("开始进行文件上传"); DiskFileUpload fu = new DiskFileUpload(); fu.setSizeMax(100*1024*1024); // 设置最大文件尺寸,这里是4MB fu.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb List fileItems = fu.parseRequest(request); // 得到所有的文件: Iterator i = fileItems.iterator(); // 依次处理每一个文件: while (i.hasNext()) { FileItem fi = (FileItem) i.next(); String fileName = fi.getName();// 获得文件名,这个文件名包括路径: ImportExcel importNurse = new ImportExcel(); importNurse.importExcel(new FileInputStream(fileName)); } } catch (Exception e) { e.printStackTrace(); response.getWriter().print("{success:flase,message:'失败'}"); // 可以跳转出错页面 } } }