struts2 檔案下載 異常的解決方案

來源:互聯網
上載者:User

Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.

如果出現以上異常 通常有兩種情況

 

一種是你在action中的擷取檔案流的方法和後面配置的不匹配,

public InputStream getDownloadFile

() {},

(get後面的第一個字母小寫是否和struts裡面的配置參數一致)

<param name="inputName">downloadFile
</param>

 

這種情況好排查

 

第二種是中文亂碼引起的

 

前台發送過來的請求中,下載檔案名稱是中文,在後台擷取的時候得到的是亂碼,所有在擷取輸入資料流的時候找不到檔案,

getDownloadFile()返回的是null,它照樣提示你

Can not find a java.io.InputStream with the name [downloadFile] in the
invocation stack.

解決辦法是擷取請求參數中的檔案名稱 並將其解碼

public void setFileName(String fileName) {
         String fname = ServletActionContext.getRequest().getParameter("fileName");
                 try {
                   /*
                      * 對fname參數進行UTF-8解碼,注意:實際進行UTF-8解碼時會使用本地編碼,本機為GBK。
                      * 這裡使用 request.setCharacterEncoding解碼無效.
                      * 只有解碼了 getDownloadFile()方法才能在下載目錄下正確找到請求的檔案
                      * */
                    fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
                 this.fileName = fname;
    }

 

可以正常下載了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.