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;
    }
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。