struts配置:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd"> 
  5. <struts> 
  6.    <!-- 上传文件的总开关,配置上传文件的大小,默认值是2M --> 
  7.    <constant name="struts.multipart.maxSize" value="7097152"></constant> 
  8.    <package name="upload" namespace="/upload" extends="struts-default"> 
  9.        <action name="uploadAction_*" class="cn.itcast.upload.UploadAction" method="{1}"> 
  10.           <interceptor-ref name="defaultStack"> 
  11.                 <!-- 设置上传文件的参数 这里属于第二开关--> 
  12.                 <!-- 设置上传文件的大小,struts2运行时,会调用FileUploadInterceptor中的setMaximumSize(6097152)方法--> 
  13.                 <param name="fileUpload.maximumSize">1097152</param> 
  14.                 <!-- 设置上传文件的允许类型,struts2运行时,会调用FileUploadInterceptor中的setAllowedTypes(...)方法--> 
  15.                 <param name="fileUpload.allowedTypes">application/vnd.ms-excel,application/vnd.ms-powerpoint</param> 
  16.                 <!-- 设置上传文件的允许扩展名,struts2运行时,会调用FileUploadInterceptor中的setAllowedExtensions(,,,)方法--> 
  17.                 <param name="fileUpload.allowedExtensions">txt,ppt</param> 
  18.           </interceptor-ref> 
  19.             
  20.           <result name="success">/upload/success.jsp</result> 
  21.             
  22.           <!-- input:固定写法,当上传出错时,要转向input所指向的页面 --> 
  23.           <result name="input">/upload/error.jsp</result> 
  24.       </action> 
  25.         
  26.        <action name="uploadsAction_*" class="cn.itcast.upload.UploadsAction" method="{1}"> 
  27.           <result name="success">/upload/success.jsp</result> 
  28.           <!-- input:固定写法,当上传出错时,要转向input所指向的页面 --> 
  29.           <result name="input">/upload/error.jsp</result> 
  30.       </action> 
  31.    </package>   
  32. </struts> 

 单文件上传action:

  1. package cn.itcast.upload;  
  2.  
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.  
  6. import javax.servlet.ServletContext;  
  7.  
  8. import org.apache.commons.io.FileUtils;  
  9. import org.apache.struts2.ServletActionContext;  
  10.  
  11. import com.opensymphony.xwork2.ActionSupport;  
  12.  
  13. @SuppressWarnings("serial")  
  14. public class UploadAction extends ActionSupport {  
  15.       
  16.     //<input type="file" name="uploadImage">,保存上传文件  
  17.     private File uploadImage;  
  18.       
  19.     //文件的类型,定义格式:jsp页面上传的文件名+ContentType,这里是uploadImage+ContentType=uploadImageContentType 
  20.     private String uploadImageContentType;  
  21.       
  22.     //文件的名字:定义格式jsp页面上传的文件名+FileName,这里是uploadImage+FileName=uploadImageFileName 
  23.     private String uploadImageFileName;  
  24.       
  25.     public String saveFile(){  
  26.         System.out.println("xxxxxxxxxx");  
  27.           
  28.         ServletContext sc=ServletActionContext.getServletContext();  
  29.         String realpath=sc.getRealPath("/pic");  
  30.           
  31.         File file=new File(realpath,uploadImageFileName);  
  32.           
  33.         try {  
  34.             FileUtils.copyFile(uploadImage, file);  
  35.         } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.           
  39.         return "success";  
  40.     }  
  41.  
  42.  
  43.  
  44.     public File getUploadImage() {  
  45.         return uploadImage;  
  46.     }  
  47.  
  48.  
  49.  
  50.     public void setUploadImage(File uploadImage) {  
  51.         this.uploadImage = uploadImage;  
  52.     }  
  53.  
  54.  
  55.  
  56.     public String getUploadImageContentType() {  
  57.         return uploadImageContentType;  
  58.     }  
  59.  
  60.  
  61.  
  62.     public void setUploadImageContentType(String uploadImageContentType) {  
  63.         this.uploadImageContentType = uploadImageContentType;  
  64.     }  
  65.  
  66.  
  67.  
  68.     public String getUploadImageFileName() {  
  69.         return uploadImageFileName;  
  70.     }  
  71.  
  72.  
  73.  
  74.     public void setUploadImageFileName(String uploadImageFileName) {  
  75.         this.uploadImageFileName = uploadImageFileName;  
  76.     }  
  77.  
  78. }  

多文件上传action:

  1. package cn.itcast.upload;  
  2.  
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.  
  6. import javax.servlet.ServletContext;  
  7.  
  8. import org.apache.commons.io.FileUtils;  
  9. import org.apache.struts2.ServletActionContext;  
  10.  
  11. import com.opensymphony.xwork2.ActionSupport;  
  12.  
  13. @SuppressWarnings("serial")  
  14. public class UploadsAction extends ActionSupport {  
  15.       
  16.     //<input type="file" name="uploadImage">,保存上传文件  
  17.     private File[] uploadImages;  
  18.       
  19.     //文件的类型,定义格式:jsp页面上传的文件名+ContentType,这里是uploadImage+ContentType=uploadImageContentType 
  20.     private String[] uploadImagesContentType;  
  21.       
  22.     //文件的名字:定义格式jsp页面上传的文件名+FileName,这里是uploadImage+FileName=uploadImageFileName 
  23.     private String[] uploadImagesFileName;  
  24.       
  25.     public String saveFiles(){  
  26.         ServletContext sc=ServletActionContext.getServletContext();  
  27.         String realpath=sc.getRealPath("/pic");  
  28.         try {  
  29.             if(uploadImagesContentType!=null&&uploadImagesContentType.length>0){  
  30.                 for(int i=0;i<uploadImagesContentType.length;i++){  
  31.                     File file=new File(realpath,uploadImagesFileName[i]);  
  32.                     FileUtils.copyFile(uploadImages[i], file);  
  33.                  }  
  34.             }  
  35.        } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.        }  
  38.         return "success";  
  39.     }  
  40.  
  41.     public File[] getUploadImages() {  
  42.         return uploadImages;  
  43.     }  
  44.  
  45.     public void setUploadImages(File[] uploadImages) {  
  46.         this.uploadImages = uploadImages;  
  47.     }  
  48.  
  49.     public String[] getUploadImagesContentType() {  
  50.         return uploadImagesContentType;  
  51.     }  
  52.  
  53.     public void setUploadImagesContentType(String[] uploadImagesContentType) {  
  54.         this.uploadImagesContentType = uploadImagesContentType;  
  55.     }  
  56.  
  57.     public String[] getUploadImagesFileName() {  
  58.         return uploadImagesFileName;  
  59.     }  
  60.  
  61.     public void setUploadImagesFileName(String[] uploadImagesFileName) {  
  62.         this.uploadImagesFileName = uploadImagesFileName;  
  63.     }  
  64.  
  65.       
  66.  
  67.  
  68. }