jsp頁面下載檔案

來源:互聯網
上載者:User

標籤:book   images   中文   leo   二次   src   url   tty   清除   

.將建立的excel文檔轉換成需要輸出的流:可以是檔案流放在硬碟中,也可以是輸出資料流輸出到瀏覽器供下載。  ◆  檔案流:FileOutputStream

1         FileOutputStream fos = new FileOutputStream("F://workbook.xls");2         workBook.write(fos);3         fos.close();

  ◆  輸出資料流 :response.getOutputStream()

1       response.reset();2         response.setContentType("application/vnd.ms-excel;charset=gbk");3         response.setHeader("Content-Disposition", "attachment;filename=account.xls");4         OutputStream out = response.getOutputStream();5         workBook.write(out);6         out.close();  

  ◆ tip:

    *response.reset();清除首部的空白行
         * getResponse的getWriter()方法連續兩次輸出資料流到頁面的時候,第二次的流會包括第一次的流,
         * 所以可以使用response.reset或者resetBuffer的方法。
         * resetBuffer方法與reset方法的區別是,頭和狀態代碼沒有清除。

         如果發現這樣設定後瀏覽器端並沒有彈出【檔案另存新檔】選擇路徑視窗,請檢查下前面代碼是否出現:

  1.response.setContentType("text/html;charset=UTF-8");//設定編碼格式
      2.PrintWriter out = response.getWriter();導致無法確定輸出資料流

3、excel檔案名稱為中文時亂碼或者出現未知檔案類型錯誤時,考慮用URLEncoder對檔案名稱進行轉碼

   

  

1         String name = java.net.URLEncoder.encode(fileName, "utf-8");2         response.setContentType("application/vnd.ms-excel;charset=utf-8");3         response.setHeader("Content-Disposition", "attachment;filename="+name.toString()+".xls"); 4         OutputStream out = response.getOutputStream();5         workBook.write(out);6         out.close();     

 

  

jsp頁面下載檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.