java-ee
  • struts2
  • download
  • 2013-04-12 138 views 0 likes 
    0

    在普通的HTML標籤,可以通過使用錨標記等生成下載鏈接:文件下載

    <a href="www.example.com/somefile.zip">DOWNLOAD</a> 
    

    在Struts2,我保存的文件名和數據庫文件的URL。 然後,在JSP文件中,填充像下載鏈接:

    <s:iterator value="fileList"> 
    <a href='<s:property value="fileURL"/>'> <s:property value="fileName"/> </a> 
    </s:iterator> 
    

    通過這種方式,文件名和被填充自己的鏈接。當我將鼠標懸停在鏈接上時,我可以在瀏覽器的狀態欄中看到正確的文件URL。但是當我點擊鏈接時,現在顯示下載對話框。我搜索了互聯網,他們告訴使用FileInputStream。 我的問題是,是否有可能生成下載鏈接而不是使用FileInputStream作爲上述代碼?

    回答

    3

    使用Struts2你有Action s和Result s。

    所以,你需要一個Action映射到你的鏈接,讓我們把它叫做download_file.do

    您創建的鏈接列表,在一個參數傳遞給告訴struts2的要下載的文件(這是很危險的,允許任意文件,所以也許文件名會很好)。現在

    <s:iterator value="fileList"> 
        <s:a action="download_file"> 
         <s:property value="fileName"/> 
         <s:text name="my.link"/> 
        </a> 
    </s:iterator> 
    

    ,在你Action需要引領者fileName如常。

    一旦在execute方法中擁有fileName,請打開InputStreamFile併爲其提供getter。您可能還想要獲取文件的大小以及要下載的名稱。

    讓我們假設InputStream的吸氣劑爲getFileToDownload,吸氣劑的尺寸爲getFileSize

    您需要提供內容部署一個getter,這將設置下載的文件的名稱,像:

    public String getContentDisposition() { 
        return "attachment;filename=\"" + fileName + "\""; 
    } 
    

    ,也爲MIME類型的吸氣劑,像

    public String getContentType() { 
        return "text/plain"; 
    } 
    

    顯然將MIME設置爲正確的類型。

    所以你的基本Action會是這個樣子

    public class MyAction extends ActionSupport { 
    
        private final File baseDownloadDir = new File("somewhere"); 
        private String fileName; 
        private InputStream inputStream; 
        private long fileSize; 
    
        @Override 
        public String execute() throws Exception { 
         /* 
         *This is a security hole begging to be exploited. 
         *A user can submit "../../../../someImportantFile" 
         *and potentially download arbitrary files from the server. 
         *You really need to do some validation on the input! 
         */ 
         final File fileToDownload = new File(baseDownloadDir, fileName); 
         fileSize = fileToDownload.length(); 
         inputStream = new FileInputStream(fileToDownload); 
         return "downloadFile"; 
        } 
    
        public String getFileName() { 
         return fileName; 
        } 
    
        public void setFileName(String fileName) { 
         this.fileName = fileName; 
        } 
    
        public long getFileSize() { 
         return fileSize; 
        } 
    
        public InputStream getFileToDownload() { 
         return inputStream; 
        } 
    
        public String getContentDisposition() { 
         return "attachment;filename=\"" + fileName + "\""; 
        } 
    
        public String getContentType() { 
         return "text/plain"; 
        } 
    } 
    

    你再返回結果的名字,讓我們把它叫做downloadFile

    在你的動作映射,你需要的是結果映射到StreamResult,這裏是一個XML例子

    <result name="downloadFile" type="stream"> 
        <param name="inputName">fileToDownload</param> 
        <param name="contentType">${contentType}</param> 
        <param name="contentLength">${fileSize}</param> 
        <param name="contentDisposition">${contentDisposition}</param> 
        <param name="contentCharSet">UTF-8</param> 
        <param name="allowCaching">true</param> 
    </result> 
    

    您可能要更改的字符集。

    +0

    非常感謝您的時間。 請讓我再問一個問題。在struts.xml中,是否有足夠的「inputName」和「contentDisposition」參數來下載文件? –

    +2

    像其他參數一樣嗎?唯一的_required_參數是'inputName' - 請參閱[documentation](http://struts.apache.org/release/2.3.x/docs/stream-result.html)。其他人只是讓瀏覽器像預期的那樣行事。 –

    相關問題