javaweb學習總結二十六(response對象的用法二 下載文件)


一:瀏覽器打開服務器上的文件

1:讀取服務器上面的資源,如果在web層,可以直接使用servletContext,如果在非web層

可以使用類加載器讀取文件

2:向瀏覽器寫數據,實際上是把數據封裝到response對象上,然后服務器發現response中響應

體中有數據綁定,然后寫給瀏覽器

3:設置響應頭,控制瀏覽器的讀取或者解析方式

如下:打開服務器上的圖片

 1 /**在頁面上查看圖片*/
 2     private void viewImage(HttpServletResponse response) throws IOException {
 3         // 設置響應頭   在網頁上查看圖片
 4         response.setHeader("content-type", "image/jpeg");
 5         InputStream in = this.getServletContext().getResourceAsStream(
 6                 "/download/1.jpg");
 7         OutputStream out = response.getOutputStream();
 8         int length = 0;
 9         byte[] buf = new byte[1024];
10         while ((length = in.read(buf)) > 0) {
11             out.write(buf, 0, length);
12         }
13         out.flush();
14         out.close();
15     }

 

二:下載服務器上面的文件

1:下載文件與打開文件類似,都是先讀取服務器上面的文件,然后再想瀏覽器寫文件,

只是響應頭不同而已。

 response.setHeader("content-disposition", "attachment;filename=1.jpg");

 1 /**下載圖片*/
 2     private void downloadImage(HttpServletResponse response) throws IOException {
 3         // 設置響應頭   在網頁上查看圖片
 4         response.setHeader("content-disposition", "attachment;filename=1.jpg");
 5         InputStream in = this.getServletContext().getResourceAsStream(
 6                 "/download/1.jpg");
 7         OutputStream out = response.getOutputStream();
 8         int length = 0;
 9         byte[] buf = new byte[1024];
10         while ((length = in.read(buf)) > 0) {
11             out.write(buf, 0, length);
12         }
13         out.flush();
14         out.close();
15     }


2:如果需要獲取文件的名稱,最好先獲取服務器上文件的絕對路徑,然后在讀取,寫內容到瀏覽器。

String path = this.getServletContext().getRealPath("/download/高圓圓.jpg");

 1 private void downloadImage2(HttpServletResponse response){
 2         String path = this.getServletContext().getRealPath("/download/高圓圓.jpg");
 3         String filename = path.substring(path.lastIndexOf("\\")+1);
 4                  //設置下載文件響應頭
 5         response.setHeader("content-disposition", "attachment;filename="+filename);
 6         InputStream in = null;
 7         OutputStream out = null;
 8         try {
 9             in = new FileInputStream(path);
10             out = response.getOutputStream();
11             int len = 0;
12             byte[] buf = new byte[1024];
13             while((len = in.read(buf)) > 0){
14                 out.write(buf, 0, len);
15             }
16         } catch (Exception e) {
17             e.printStackTrace();
18         } finally{
19             if(null != in){
20                 try {
21                     in.close();
22                 } catch (IOException e) {
23                     e.printStackTrace();
24                 }
25             }
26             if(null != out){
27                 try {
28                     out.close();
29                 } catch (IOException e) {
30                     e.printStackTrace();
31                 }
32             }
33         }
34     }

如果文件名為中文時,下載會出現亂碼問題,導致無法下載,

這時我們可以先對文件名稱進行編碼,如下:

1 String filename = path.substring(path.lastIndexOf("\\")+1);
2         try {
3             filename = URLEncoder.encode(filename,"utf-8");
4         } catch (UnsupportedEncodingException e1) {
5             e1.printStackTrace();
6         }

這樣亂碼問題就解決了!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM