HttpServletResponse對象實現文件下載

Web服務器收到客戶端的http請求,會針對每一次請求,分別創建一個用於代表請求的request對象、和代表響應的response對象。
request和response對象即然代表請求和響應,那我們要獲取客戶機提交過來的數據,只需要找request對象就行了。要向客戶機輸出數據,只需要找response對象就行了。

一、HttpServletResponse對象介紹
這裏寫圖片描述
HttpServletResponse對象代表服務器的響應。這個對象中封裝了向客戶端發送數據、發送響應頭,發送響應狀態碼的方法。查看HttpServletResponse的API,可以看到這些相關的方法。

1.1、負責向客戶端(瀏覽器)發送數據的相關方法
這裏寫圖片描述
  

1.2、負責向客戶端(瀏覽器)發送響應頭的相關方法

  這裏寫圖片描述

1.3、負責向客戶端(瀏覽器)發送響應狀態碼的相關方法

  這裏寫圖片描述

1.4、響應狀態碼的常量
 這裏寫圖片描述
  HttpServletResponse定義了很多狀態碼的常量(具體可以查看Servlet的API),當需要向客戶端發送響應狀態碼時,可以使用這些常量,避免了直接寫數字,常見的狀態碼對應的常量:

  狀態碼404對應的常量
這裏寫圖片描述
 

  狀態碼200對應的常量
這裏寫圖片描述
  

  狀態碼500對應的常量
這裏寫圖片描述
  

二、HttpServletResponse對象常見應用

2.1、使用OutputStream流向客戶端瀏覽器輸出中文數據

使用OutputStream流輸出中文注意問題:

  在服務器端,數據是以哪個碼錶輸出的,那麼就要控制客戶端瀏覽器以相應的碼錶打開,比如:outputStream.write(“中國”.getBytes(“UTF-8”));使用OutputStream流向客戶端瀏覽器輸出中文,以UTF-8的編碼進行輸出,此時就要控制客戶端瀏覽器以UTF-8的編碼打開,否則顯示的時候就會出現中文亂碼,那麼在服務器端如何控制客戶端瀏覽器以以UTF-8的編碼顯示數據呢?可以通過設置響應頭控制瀏覽器的行爲,例如:response.setHeader(“content-type”, “text/html;charset=UTF-8”);通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數據。

範例:使用OutputStream流向客戶端瀏覽器輸出”中國”這兩個漢字

package gacl.response.study;
 2 
 3 import java.io.IOException;
 4 import java.io.OutputStream;
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class ResponseDemo01 extends HttpServlet {
11 
12     private static final long serialVersionUID = 4312868947607181532L;
13 
14     public void doGet(HttpServletRequest request, HttpServletResponse response)
15             throws ServletException, IOException {
16         outputChineseByOutputStream(response);//使用OutputStream流輸出中文
17     }
18     
19     /**
20      * 使用OutputStream流輸出中文
21      * @param request
22      * @param response
23      * @throws IOException 
24      */
25     public void outputChineseByOutputStream(HttpServletResponse response) throws IOException{
26         /**使用OutputStream輸出中文注意問題:
27          * 在服務器端,數據是以哪個碼錶輸出的,那麼就要控制客戶端瀏覽器以相應的碼錶打開,
28          * 比如:outputStream.write("中國".getBytes("UTF-8"));//使用OutputStream流向客戶端瀏覽器輸出中文,以UTF-8的編碼進行輸出
29          * 此時就要控制客戶端瀏覽器以UTF-8的編碼打開,否則顯示的時候就會出現中文亂碼,那麼在服務器端如何控制客戶端瀏覽器以以UTF-8的編碼顯示數據呢?
30          * 可以通過設置響應頭控制瀏覽器的行爲,例如:
31          * response.setHeader("content-type", "text/html;charset=UTF-8");//通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數據
32          */
33         String data = "中國";
34         OutputStream outputStream = response.getOutputStream();//獲取OutputStream輸出流
35         response.setHeader("content-type", "text/html;charset=UTF-8");//通過設置響應頭控制瀏覽器以UTF-8的編碼顯示數據,如果不加這句話,那麼瀏覽器顯示的將是亂碼
36         /**
37          * data.getBytes()是一個將字符轉換成字節數組的過程,這個過程中一定會去查碼錶,
38          * 如果是中文的操作系統環境,默認就是查找查GB2312的碼錶,
39          * 將字符轉換成字節數組的過程就是將中文字符轉換成GB2312的碼錶上對應的數字
40          * 比如: "中"在GB2312的碼錶上對應的數字是98
41          *         "國"在GB2312的碼錶上對應的數字是99
42          */
43         /**
44          * getBytes()方法如果不帶參數,那麼就會根據操作系統的語言環境來選擇轉換碼錶,如果是中文操作系統,那麼就使用GB2312的碼錶
45          */
46         byte[] dataByteArr = data.getBytes("UTF-8");//將字符轉換成字節數組,指定以UTF-8編碼進行轉換
47         outputStream.write(dataByteArr);//使用OutputStream流向客戶端輸出字節數組
48     }
49 
50     public void doPost(HttpServletRequest request, HttpServletResponse response)
51             throws ServletException, IOException {
52         doGet(request, response);
53     }
54 
55 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

運行結果如下:
這裏寫圖片描述
客戶端瀏覽器接收到數據後,就按照響應頭上設置的字符編碼來解析數據,如下所示:
這裏寫圖片描述

2.2、文件下載

  文件下載功能是web開發中經常使用到的功能,使用HttpServletResponse對象就可以實現文件的下載

文件下載功能的實現思路:

  1.獲取要下載的文件的絕對路徑

  2.獲取要下載的文件名

  3.設置content-disposition響應頭控制瀏覽器以下載的形式打開文件

  4.獲取要下載的文件輸入流

  5.創建數據緩衝區

  6.通過response對象獲取OutputStream流

  7.將FileInputStream流寫入到buffer緩衝區

  8.使用OutputStream將緩衝區的數據輸出到客戶端瀏覽器

範例:使用Response實現文件下載

 package gacl.response.study;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 import java.io.PrintWriter;
 9 import java.net.URLEncoder;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 /**
15  * @author gacl
16  * 文件下載
17  */
18 public class ResponseDemo02 extends HttpServlet {
19 
20     public void doGet(HttpServletRequest request, HttpServletResponse response)
21             throws ServletException, IOException {
22         downloadFileByOutputStream(response);//下載文件,通過OutputStream流
23     }
24 
25     /**
26      * 下載文件,通過OutputStream流
27      * @param response
28      * @throws FileNotFoundException
29      * @throws IOException
30      */
31     private void downloadFileByOutputStream(HttpServletResponse response)
32             throws FileNotFoundException, IOException {
33         //1.獲取要下載的文件的絕對路徑
34         String realPath = this.getServletContext().getRealPath("/download/1.JPG");
35         //2.獲取要下載的文件名
36         String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);
37         //3.設置content-disposition響應頭控制瀏覽器以下載的形式打開文件
38         response.setHeader("content-disposition", "attachment;filename="+fileName);
39         //4.獲取要下載的文件輸入流
40         InputStream in = new FileInputStream(realPath);
41         int len = 0;
42         //5.創建數據緩衝區
43         byte[] buffer = new byte[1024];
44         //6.通過response對象獲取OutputStream流
45         OutputStream out = response.getOutputStream();
46         //7.將FileInputStream流寫入到buffer緩衝區
47         while ((len = in.read(buffer)) > 0) {
48         //8.使用OutputStream將緩衝區的數據輸出到客戶端瀏覽器
49             out.write(buffer,0,len);
50         }
51         in.close();
52     }
53 
54     public void doPost(HttpServletRequest request, HttpServletResponse response)
55             throws ServletException, IOException {
56         doGet(request, response);
57     }
58 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

運行結果如下所示:
這裏寫圖片描述
範例:使用Response實現中文文件下載

  下載中文文件時,需要注意的地方就是中文文件名要使用URLEncoder.encode方法進行編碼(URLEncoder.encode(fileName, “字符編碼”)),否則會出現文件名亂碼。
  

package gacl.response.study;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8 import java.io.PrintWriter;
 9 import java.net.URLEncoder;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 /**
15  * @author gacl
16  * 文件下載
17  */
18 public class ResponseDemo02 extends HttpServlet {
19 
20     public void doGet(HttpServletRequest request, HttpServletResponse response)
21             throws ServletException, IOException {
22         downloadChineseFileByOutputStream(response);//下載中文文件
23     }
24 
25     /**
26      * 下載中文文件,中文文件下載時,文件名要經過URL編碼,否則會出現文件名亂碼
27      * @param response
28      * @throws FileNotFoundException
29      * @throws IOException
30      */
31     private void downloadChineseFileByOutputStream(HttpServletResponse response)
32             throws FileNotFoundException, IOException {
33         String realPath = this.getServletContext().getRealPath("/download/張家界國家森林公園.JPG");//獲取要下載的文件的絕對路徑
34         String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);//獲取要下載的文件名
35         //設置content-disposition響應頭控制瀏覽器以下載的形式打開文件,中文文件名要使用URLEncoder.encode方法進行編碼,否則會出現文件名亂碼
36         response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
37         InputStream in = new FileInputStream(realPath);//獲取文件輸入流
38         int len = 0;
39         byte[] buffer = new byte[1024];
40         OutputStream out = response.getOutputStream();
41         while ((len = in.read(buffer)) > 0) {
42             out.write(buffer,0,len);//將緩衝區的數據輸出到客戶端瀏覽器
43         }
44         in.close();
45     }
46     
47     public void doPost(HttpServletRequest request, HttpServletResponse response)
48             throws ServletException, IOException {
49         doGet(request, response);
50     }
51 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

運行結果如下所示:
這裏寫圖片描述

向客戶端瀏覽器輸出中文數據可以使用OutputStream流
也可以使用PrintWriter流
PrintWriter流詳描述見以下博文

http://www.cnblogs.com/xdp-gacl/p/3789624.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章