1. 程式人生 > >struts2下載檔案點選取消時出現異常的解決辦法

struts2下載檔案點選取消時出現異常的解決辦法

前言

Struts2的檔案下載Action與普通的Action並沒有什麼太大的不同,僅僅是該Action需要提供一個返回InputStream流的方法,該輸入流代表了被下載檔案的入口,同時在配置檔案中配置Action的result型別為stream。

檔案下載

Action類的實現:

public class FileDownloadAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    // 檔案的Mime型別
    private String contentType;

    // 要下載的檔名稱
    private String fileName;

    private File[] files;

    public String list() throws Exception {
        File f = new File(getDownloadFilePath());
        // 獲取目錄下儲存的檔案
        files = f.listFiles();
        return
"list"; } /* * 定義一個返回InputStream的方法 該方法將被作為下載檔案的入口, 且需要配置Stream型別結果時指定inputName引數 * inputName引數的值就是該方法去掉get字首,首字母小寫的字串 */ public InputStream getDownloadFile() throws Exception { String filePath = getDownloadFilePath() + "/" + fileName; InputStream is = new FileInputStream(new File(filePath)); return
is; } //設定下載檔案的名稱 public String getDownloadFileName() { String downFileName = fileName; // get userAgent String userAgent = ServletActionContext.getRequest().getHeader("User-Agent"); try { // Edge Browser or IE Browser if (userAgent.contains("Edge"
) || userAgent.contains("Trident") || userAgent.contains("MSIE")) { downFileName = java.net.URLEncoder.encode(downFileName, "UTF-8").replace("+", "%20"); } else { downFileName = new String(downFileName.getBytes("UTF-8"), "ISO-8859-1"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return downFileName; } public String getDownloadFilePath() throws Exception { String filePath = getClass().getResource("/").toURI().getPath(); filePath = filePath.replace("WEB-INF/classes/", "download"); return filePath; } public File[] getFiles() { return files; } public void setFiles(File[] files) { this.files = files; } public void setFileName(String fileName) throws UnsupportedEncodingException{ //下載檔名如果包含中文需要轉碼,避免出現亂碼 fileName = URLDecoder.decode(fileName,"UTF-8"); this.fileName = fileName; } public String getFileName() { return fileName; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } }

Struts.xml的配置

配置下載檔案的Action時,需要配置一個型別為stream的結果,需要指定如下4個屬性。

  • contentType: 指定被下載檔案的檔案型別
  • inputName: 指定被下載檔案的入口輸入流
  • contentDisposition: 指定下載的檔名
  • bufferSize: 指定下載檔案時的緩衝大小
<action name="download" class="com.learn.FileDownloadAction.FileDownloadAction">
    <result type="stream">
       <!-- 指定下載檔案的MIME型別 -->
       <param name="contentType">${contentType}</param>
       <!-- 下載的檔案資料存放的方法,該方法返回一個InputStream 例如取值為inputStream的屬性需要編寫getInputStream()方法 -->
       <param name="inputName">downloadFile</param>
       <!--下載時,客戶端顯示的下載的檔名 -->
       <param name="contentDisposition">attachment;filename=${downloadFileName}</param>
       <!-- 資料的緩衝大小 -->
       <param name="bufferSize">4096</param>
    </result>
</action>

檔案下載取消時出現異常

在struts2中使用result裡type=”stream”的結果型別時,當在“下載提示視窗”中點選“取消按鈕”時,總是報出“java.lang.IllegalStateException”異常。

異常原因分析:

  1. 配置其中result標籤下的各個引數
  2. 從伺服器中獲取輸入流,並同時與客戶端建立輸出流(伺服器與客戶端連結通過Socket進行連線)
  3. 當點選“儲存”或“開啟”時,開始傳輸資料。如果點選“取消”,關閉所有的流。這裡要注意的是,但是實際發現Socket並沒有斷開!並且流也沒有關閉!這一點非常重要!所以在JSP容器通過Response獲取輸出流之前,前面的流並沒有關閉,所以會造成該異常的報出。

異常解決辦法:

  1. 下載外掛struts2-sunspoter-stream-1.0.jar,並複製在/WEB-INF/lib下。 CSDN下載地址:struts2-sunspoter-stream-1.0.jar

  2. 在Struts.xml 中配置:

<result-types>
    <result-type name="streamx" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX" />
</result-types>

3.在下載的Action中配置result的型別為streamx:

<action name="download" class="com.learn.FileDownloadAction.FileDownloadAction">
    <result type="streamx">
        <!-- 下載的檔案資料存放的方法,該方法返回一個InputStream 例如取值為inputStream的屬性需要編寫getInputStream()方法 -->
        <param name="inputName">downloadFile</param>
        <!--下載時,客戶端顯示的下載的檔名 -->
        <param name="contentDisposition">attachment;fileName=${downloadFileName}</param>
        <!-- 資料的緩衝大小 -->
        <param name="bufferSize">4096</param>
    </result>
</action>