码迷,mamicode.com
首页 > Web开发 > 详细

Struts 2 之文件上传

时间:2015-02-09 09:25:10      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:struts 2   文件上传   


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

单个文件上传

public class UploadAction extends ActionSupport{
   
    private File image; //上传的文件
    private String imageFileName; //文件名称
    private String imageContentType; //文件类型
 
    public String execute() throws Exception {
        String realpath =ServletActionContext.getServletContext().getRealPath("/images");
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try {
            // 建立文件输出流
            System.out.println(getSavePath());
            fos = new FileOutputStream(realpath+ "\\" + getImageFileName());
            // 建立文件上传流
            fis = newFileInputStream(getImage());
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer))> 0) {
                fos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            System.out.println("文件上传失败");
            e.printStackTrace();
        } finally {
            close(fos, fis);
        }
        return SUCCESS;   
}
 
    public File getImage() {
        return image;
    }
 
    public void setImage(File image) {
        this.image = image;
    }
 
    public String getImageFileName() {
        return imageFileName;
    }
 
    public void setImageFileName(StringimageFileName) {
        this.imageFileName = imageFileName;
    }
 
    public String getImageContentType() {
        return imageContentType;
    }
 
    public void setImageContentType(StringimageContentType) {
        this.imageContentType = imageContentType;
    }
   
}


 

多个文件上传

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class TagUploadListAction extends ActionSupport {
    private static final long serialVersionUID= 1L;
    private String name;
 
    // 上传多个文件的集合文本
    private List<File> upload;
    // /多个上传文件的类型集合
    private List<String>uploadContextType;
   // 多个上传文件的文件名集合
    private List<String> uploadFileName;
 
    public String getName() {
            return name;
     }
    public void setName(String name) {
       this.name = name;
    }
    public List<File> getUpload() {
       return upload;
    }
    public void setUpload(List<File>upload) {
       this.upload = upload;
    }
    public List<String>getUploadContextType() {
       return uploadContextType;
    }
    public voidsetUploadContextType(List<String> uploadContextType) {
       this.uploadContextType =uploadContextType;
    }
    public List<String>getUploadFileName() {
       return uploadFileName;
    }
    public voidsetUploadFileName(List<String> uploadFileName) {
       this.uploadFileName = uploadFileName;
    }
    public String execute() {
 
       // 把上传的文件放到指定的路径下
       String path =ServletActionContext.getServletContext().getRealPath(
 
              "/WEB-INF/uploadList");
 
       // 写到指定的路径中
       File file = new File(path);
 
       // 如果指定的路径没有就创建
       if(!file.exists()) {
           file.mkdirs();
       }
 
       // 把得到的文件的集合通过循环的方式读取并放在指定的路径下
       for (int i = 0; i < upload.size();i++) {
           try {
              //list集合通过get(i)的方式来获取索引
              FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
           } catch (IOException e) {
              // TODO Auto-generated catchblock
              e.printStackTrace();
           }
       }
       return SUCCESS;
    }
}


Struts 2 之文件上传

标签:struts 2   文件上传   

原文地址:http://blog.csdn.net/u012152619/article/details/43666983

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!