Struts 2 之文件上传

如果要获得上传文件的原始名称,需要定义一个String类型的属性,属性名必须为***FileName,其中***为File属性的名称;同理,如果要获取该文件的MIME类型,需要定义一个***ContentType的String属性

单个文件上传

  1. public class UploadAction extends ActionSupport{  
  2.      
  3.     private File image; //上传的文件  
  4.     private String imageFileName; //文件名称  
  5.     private String imageContentType; //文件类型  
  6.    
  7.     public String execute() throws Exception {  
  8.         String realpath =ServletActionContext.getServletContext().getRealPath("/images");  
  9.         FileOutputStream fos = null;  
  10.         FileInputStream fis = null;  
  11.         try {  
  12.             // 建立文件输出流  
  13.             System.out.println(getSavePath());  
  14.             fos = new FileOutputStream(realpath+ "\\" + getImageFileName());  
  15.             // 建立文件上传流  
  16.             fis = newFileInputStream(getImage());  
  17.             byte[] buffer = new byte[1024];  
  18.             int len = 0;  
  19.             while ((len = fis.read(buffer))> 0) {  
  20.                 fos.write(buffer, 0, len);  
  21.             }  
  22.         } catch (Exception e) {  
  23.             System.out.println("文件上传失败");  
  24.             e.printStackTrace();  
  25.         } finally {  
  26.             close(fos, fis);  
  27.         }  
  28.         return SUCCESS;     
  29. }  
  30.    
  31.     public File getImage() {  
  32.         return image;  
  33.     }  
  34.    
  35.     public void setImage(File image) {  
  36.         this.image = image;  
  37.     }  
  38.    
  39.     public String getImageFileName() {  
  40.         return imageFileName;  
  41.     }  
  42.    
  43.     public void setImageFileName(StringimageFileName) {  
  44.         this.imageFileName = imageFileName;  
  45.     }  
  46.    
  47.     public String getImageContentType() {  
  48.         return imageContentType;  
  49.     }  
  50.    
  51.     public void setImageContentType(StringimageContentType) {  
  52.         this.imageContentType = imageContentType;  
  53.     }  
  54.      
  55. }  


 

多个文件上传

  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.util.List;  
  4. import org.apache.commons.io.FileUtils;  
  5. import org.apache.struts2.ServletActionContext;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7. public class TagUploadListAction extends ActionSupport {  
  8.     private static final long serialVersionUID= 1L;  
  9.     private String name;  
  10.    
  11.     // 上传多个文件的集合文本  
  12.     private List<File> upload;  
  13.     // /多个上传文件的类型集合  
  14.     private List<String>uploadContextType;  
  15.    // 多个上传文件的文件名集合  
  16.     private List<String> uploadFileName;  
  17.    
  18.     public String getName() {  
  19.             return name;  
  20.      }  
  21.     public void setName(String name) {  
  22.        this.name = name;  
  23.     }  
  24.     public List<File> getUpload() {  
  25.        return upload;  
  26.     }  
  27.     public void setUpload(List<File>upload) {  
  28.        this.upload = upload;  
  29.     }  
  30.     public List<String>getUploadContextType() {  
  31.        return uploadContextType;  
  32.     }  
  33.     public voidsetUploadContextType(List<String> uploadContextType) {  
  34.        this.uploadContextType =uploadContextType;  
  35.     }  
  36.     public List<String>getUploadFileName() {  
  37.        return uploadFileName;  
  38.     }  
  39.     public voidsetUploadFileName(List<String> uploadFileName) {  
  40.        this.uploadFileName = uploadFileName;  
  41.     }  
  42.     public String execute() {  
  43.    
  44.        // 把上传的文件放到指定的路径下  
  45.        String path =ServletActionContext.getServletContext().getRealPath(  
  46.    
  47.               "/WEB-INF/uploadList");  
  48.    
  49.        // 写到指定的路径中  
  50.        File file = new File(path);  
  51.    
  52.        // 如果指定的路径没有就创建  
  53.        if(!file.exists()) {  
  54.            file.mkdirs();  
  55.        }  
  56.    
  57.        // 把得到的文件的集合通过循环的方式读取并放在指定的路径下  
  58.        for (int i = 0; i < upload.size();i++) {  
  59.            try {  
  60.               //list集合通过get(i)的方式来获取索引  
  61.               FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));  
  62.            } catch (IOException e) {  
  63.               // TODO Auto-generated catchblock  
  64.               e.printStackTrace();  
  65.            }  
  66.        }  
  67.        return SUCCESS;  
  68.     }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值