struts2的文件上傳和下載

 1)action中代碼:
 /** 
 * 處理文件上傳的Action 
 * @author dove 
 */  
public class UploadAction  extends ActionSupport {  
    /** 表明上傳的文件內容的對象 */  
    private File upload;  
      
    /** Struts2約定的表明上傳的文件的名 */  
    private String uploadFileName;  
    /** Struts2約定的表明上傳文件的內容類型(MIME) */  
    private String uploadContentType;  
      
    public String execute() throws Exception{  
        System.out.println("文件的名:" + uploadFileName);  
        System.out.println("不要用upload.getName()來獲取文件名,這個是臨時名:" + upload.getName());  
        System.out.println("文件的內容類型:" + uploadContentType);  
          
        //////////使用IO流來操做upload屬性  
        //File destPath = new File("d:/"); //服務端存放文件的目錄  
          
        //若是要存放到web服務器中本項目的某個目錄下  
        //根據服務器的文件保存地址和原文件名建立目錄文件全路徑  
        String destPath = ServletActionContext.getServletContext().getRealPath("/uploads");  
          
        File dest = new File(destPath, uploadFileName); //服務器的文件  
          
        FileUtils.copyFile(upload, dest);//完成了文件的拷貝工做  
          
        return "succ";  
    }  
      
    public String getSummery() {  
        return summery;  
    }  
    public void setSummery(String summery) {  
        this.summery = summery;  
    }  
    public File getUpload() {  
        return upload;  
    }  
    public void setUpload(File upload) {  
        this.upload = upload;  
    }  
    public String getUploadFileName() {  
        return uploadFileName;  
    }  
    public void setUploadFileName(String uploadFileName) {  
        this.uploadFileName = uploadFileName;  
    }  
    public String getUploadContentType() {  
        return uploadContentType;  
    }  
    public void setUploadContentType(String uploadContentType) {  
        this.uploadContentType = uploadContentType;  
    }  
}  

2)struts.xml中的配置
<action name="upload" class="com.xxyd.web.action.UploadAction">  
            <!-- 能夠更改fileUpload攔截器的屬性值來限定上傳文件的內容類型,上傳文件的大小 -->  
            <interceptor-ref name="defaultStack">  
         <param name="fileUpload.allowedTypes">image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg</param>  
         <param name="fileUpload.maximumSize">102400</param>   
        </interceptor-ref>              
            <result name="succ">/succ.jsp</result>  
            <result name="input">/index.jsp</result>  
        </action>  

3)html代碼:
<html>  
  <head>  
    <title>Struts2文件上傳示例</title>  
  </head>  
    
  <body>  
    <h3>Struts2文件上傳示例</h3><hr/>  
    <s:fielderror/>  
    <form action="upload.action" method="post" enctype="multipart/form-data"><!-- 此處必須爲multipart/form-data,並且必須使用post方法 -->  
        <table border="1" width="500">  
            <tr>  
                <td>選擇文件</td>  
                <td><input type="file" name="upload" /></td>  
            </tr>  
            <tr>  
                <td colspan="2" align="center"><input type="submit" value='提交' /></td>  
            </tr>  
        </table>  
    </form>  
  </body>  
</html>  


2,action中的文件下載功能
 1)action中代碼:
 public class DownLoadAction {  
    private String basePath = ServletActionContext.getServletContext().getRealPath("/uploads");  
    private String fileName;  
      
      
    public String execute(){  
        return "succ";  
    }  
      
    public InputStream getInputStream() throws FileNotFoundException{  
        return new FileInputStream(new File(basePath, fileName));  
    }  
  
    public String getFileName() throws UnsupportedEncodingException {  
        return new String(fileName.getBytes(), "ISO-8859-1");  
    }  
  
    public void setFileName(String fileName) {  
        this.fileName = fileName;  
    }  
}  
2)struts.xml中的配置:
<!-- 文件下載 -->  
        <action name="download" class="com.xxyd.web.action.DownLoadAction">  
            <result name="succ" type="stream">  
                <param name="contentType">application/octet-stream;charset=ISO8859-1</param>  
                <param name="inputName">inputStream</param>  
                <param name="contentDisposition">attachment;filename=${fileName}</param>  
                <param name="bufferSize">8192</param>  
            </result>  
        </action>  

  3)html頁面代碼:
  <html>  
  <head>  
    <title>Struts2文件下載功能實例</title>  
  </head>  
    
  <body>      
    <a href="download.action?fileName=sunset.jpg">下載sunset.jpg</a><br/>  
  </body>  
</html>
相關文章
相關標籤/搜索