Struts2 實現文件上傳下載 (下載支持中文文件名)代碼

jopen 9年前發布 | 2K 次閱讀 Java Struts2

struts2 實現文件上傳:


Action 代碼:

    package com.action;

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.InputStream;  
import java.io.OutputStream;  

import org.apache.struts2.ServletActionContext;  

import com.opensymphony.xwork2.ActionSupport;  

public class FileUploadAction extends BaseAction{  


    /** 
     *  
     */  
    private static final long serialVersionUID = 1L;  



    /** 
     * 上傳后存放在臨時文件夾里的文件 
     */  
    private File <span style="color:#ff0000;">file;</span>  

    /** 
     * 文件名稱 
     */  
    private String <span style="color:#ff0000;">fileFileName</span>;  

    /** 
     * 文件的MIME類型 
     */  
    private String<span style="color:#ff0000;"> fileContentType</span>;  


    /** 
     * 保存的路徑 
     */  
    private String savePath;  


    public String getSavePath() {  
        return savePath;  
    }  

    public void setSavePath(String savePath) {  
        this.savePath = savePath;  
    }  

    public File getFile() {  
        return file;  
    }  

    public void setFile(File file) {  
        this.file = file;  
    }  

    public String getFileFileName() {  
        return fileFileName;  
    }  

    public void setFileFileName(String fileFileName) {  
        this.fileFileName = fileFileName;  
    }  

    public String getFileContentType() {  
        return fileContentType;  
    }  

    public void setFileContentType(String fileContentType) {  
        this.fileContentType = fileContentType;  
    }  


    public String execute() throws Exception  
    {  

         String root = ServletActionContext.getServletContext().getRealPath(savePath);  

            InputStream is = new FileInputStream(file);  

            OutputStream os = new FileOutputStream(new File(root, fileFileName));  


            byte[] buffer = new byte[500];  
            @SuppressWarnings("unused")  
            int length = 0;  

            while(-1 != (length = is.read(buffer, 0, buffer.length)))  
            {  
                os.write(buffer);  
            }  

            os.close();  
            is.close();  


            return SUCCESS;  

    }  


}  </pre> 


struts2.xml配置信息:

    <?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC     
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     
     "http://struts.apache.org/dtds/struts-2.0.dtd">  


<struts>  

    <package name="fileUpload" extends="struts-default" namespace="/">  

        <action name="fileUpload_*" class="com.action.FileUploadAction" method="{1}">  

          <param name="savePath">/upload</param> //注意在當前工程里建一個 upload文件夾  

        <result>  
            /login.jsp  
        </result>  

         </action>  


    </package>  
</struts>  </pre> 


文件上傳的 jsp 頁面:

    <form action="fileUpload.action" method="post" enctype="multipart/form-data">  
           file: <input type="file" name="file"/><br>  
           <input type="submit" value="submit"/>  
       </form>  

struts2實現文件下載源碼:支持中文文件名

注意:下載文件的大小設置問題,默認是2M. 可以在struts.properties 設置  struts.multipart.maxSize=10485760 (10M 大小根據自己情況)

action 的代碼:

    package com.action;

import java.io.IOException;  
import java.io.InputStream;  
import org.apache.struts2.ServletActionContext;  


public class FileDownloadAction extends BaseAction{  

    /** 
     *  
     */  
    private static final long serialVersionUID = 1L;  
    /** 
     * 文件名稱 
     */  
    private String fileName;  


    public String getFileName() throws IOException {  


        <span style="color:#ff0000;">return  new String(fileName.getBytes(), "ISO8859-1"); //轉碼 使其支持中文,不然無法顯示文件名的中文字符</span>  
    }  

        public void setFileName(String fileName) {  

                this.fileName = fileName;;  

        }  

         public InputStream getInputStream() throws Exception{  

             return ServletActionContext.getServletContext().getResourceAsStream( fileName);  

               }  

                public String execute()throws Exception{  
                    return SUCCESS;  
                }  
}  </pre> 


下載的 struts2.xml配置信息:

    <action name="download" class="com.action.FileDownloadAction">

            <result type="stream" name="success">       
                <param name="contentDisposition">attachment;fileName="${fileName}"</param>  
                <param name="contentType">application/octet-stream;charset=ISO8859-1</param>  
                <param name="inputName"><span style="color:#ff0000;">InputStream</span></param> //InputStream 是有Action中的<span style="font-family: Arial; font-size: 14px; line-height: 26px;">getInputStream()方法 去掉get而定的,要一致</span>  
                <param name="bufferSize">10485760</param>//文件大小(10M)  
            </result>  


    </action>  </pre> 


jsp頁面:

    <a href="download.action?fileName=upload/銘記11er.mp3">點擊下載</a>  

來自:http://blog.csdn.net/u013147600/article/details/44919719

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