论坛首页 Java企业应用论坛

利用webwork进行上传

浏览 2133 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-17  
一个多文件上传的例子,大家可依据自己的需要改变自己的上传目录,有错误的话,希望大家多多批评。
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;
    }
}
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics