Struts 2之文件上傳

n672 9年前發布 | 11K 次閱讀 Struts 2 Struts2 Web框架

如果要獲得上傳文件的原始名稱,需要定義一個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;  
    }  

}  </pre> <br />

多個文件上傳

    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;  
    }  
}  </pre> <br />
 本文由用戶 n672 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!