Struts 2之文件上传

n672 9年前

如果要获得上传文件的原始名称,需要定义一个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 = new FileInputStream(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(String imageFileName) {                this.imageFileName = imageFileName;            }                     public String getImageContentType() {                return imageContentType;            }                     public void setImageContentType(String imageContentType) {                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 void setUploadContextType(List<String> uploadContextType) {               this.uploadContextType =uploadContextType;            }            public List<String> getUploadFileName() {               return uploadFileName;            }            public void setUploadFileName(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;            }        }