一个多文件上传的例子,大家可依据自己的需要改变自己的上传目录,有错误的话,希望大家多多批评。
package com.Jskyben.test; /*author:skyact 多文件上传功能 */ import com.opensymphony.xwork.ActionSupport; import java.io.*; import javax.servlet.ServletContext; import com.opensymphony.xwork.ValidationAware; import javax.servlet.http.HttpServletRequest; import com.opensymphony.webwork.dispatcher.multipart.*; import com.opensymphony.webwork.ServletActionContext; import com.opensymphony.webwork.interceptor.ServletRequestAware; public class UploadDoc1 extends ActionSupport implements ValidationAware,ServletRequestAware{ private File[] doc; private String[] docContentType; private String[] docFileName; private String message=""; ServletContext d; HttpServletRequest req; public String execute() throws Exception{ MultiPartRequestWrapper wrapper=(MultiPartRequestWrapper)req; message=wrapper.getParameter("message");//取得表单中其他字段值 d=ServletActionContext.getServletContext(); String path=d.getRealPath("/");//取得根目录 path+="pic/"; for(int i=0;i<doc.length;i++){//多文件上传 File target = new File(path+docFileName[i]); if(target.exists()){ target.delete(); } doc[i].renameTo(target); } return SUCCESS; } public void setServletRequest(HttpServletRequest req){ this.req=req; } public void validate(){ if(doc==null||doc.equals("")){ addFieldError("doc", getText("file not found,please upload a file"));} } public void setDoc(File[] doc) { this.doc = doc; } public void setDocContentType(String[] docContentType) { this.docContentType = docContentType; } public void setDocFileName(String[] docFileName) { this.docFileName = docFileName; } public String getMessage(){ return message; } }