文件下載的方式
- 超鏈接下載
- 后臺實現下載
案例實操
超鏈接下載
當我們在 HTML 或 JSP 頁面中使用標簽時,原意是希望能夠進行跳轉,但當超鏈接遇到瀏覽器不識別的動態網頁時則會自動下載。如果瀏覽器遇到能夠直接顯示的資源,瀏覽器就會默認顯示出來,比如 txt,png,jpg 等。當然我們也可以通過 download 屬性規定瀏覽器進行下載。但有些瀏覽器并不支持。
默認下載
<a href="upload/abc.zip" rel="external nofollow" >超鏈接下載</a>
指定 download 屬性下載
<a href="upload/abc.txt" rel="external nofollow" download="abcdef.txt">超鏈接下載</a>
這里,download 也可以不寫任何信息,會自動使用默認文件名。這樣當用戶打開瀏覽器點擊鏈接的時候就會直接下載文件。
后臺實現下載
Step1:需要通過 HttpServletResponse.setContentType 方法設置 Content-type 頭字段的值,這樣瀏覽器才能夠使用某種方式或激活某個程序來處理相應 MIME 類型的數據,例 如 ”application/octet-stream” 或 ”application/x-msdownload” 等
Step2:需要通過 HttpServletResponse.setHeader 方法設置 Content-Disposition 頭的值為”attachment;filename=文件名”,filename提供了文件下載時的一個默認文件名
Step3:讀取下載文件,調用 HttpServletResponse.getOutputStream 方法返回的OutputStream對象來向客戶端寫入附件內容。
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
|
public class DownLoadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置請求編碼 request.setCharacterEncoding( "UTF-8" ); // 接受參數,得到需要被下載的文件的名稱 String fileName = request.getParameter( "fileName" ); // 判斷名字名是否為空 if (fileName == null || "" .equals(fileName)){ // 提示 System.out.println( "文件名不能為空" ); return ; } // 獲取文件存放的真實路徑 String path = request.getServletContext().getRealPath( "/" + fileName); // 通過文件路徑和文件名得到file對象 File file = new File(path); // 判斷是否存在,并且是一個標準文件 if (file.exists() && file.isFile()){ // 設置相應類型 application/octet-stream response.setContentType( "application/x-msdownload" ); // 設置頭信息 response.setHeader( "Content-Disposition" , "attachment;filename=" + fileName); // 通過file對象得到輸入流 FileInputStream in = new FileInputStream(file); // 得到輸出流 ServletOutputStream out = response.getOutputStream(); byte [] car = new byte [ 1024 ]; int len = 0 ; while ((len = in.read(car)) != - 1 ){ out.write(car, 0 ,len); } // 關閉流 in.close(); out.close(); } else { System.out.println( "文件路徑不正確!" ); } } } |
擴展~HTML表單編碼
HTML表單編碼
enctype屬性指定瀏覽器如何編碼數據并將其呈現給服務器。
此屬性有三個允許值。
application/x-www-form-urlencoded
默認編碼。
此編碼無法用于將文件上傳到服務器。
multipart/form-data
此編碼用于將文件上傳到服務器。
text/plain
此編碼因瀏覽器而異。
要理解不同編碼的工作原理,我們創建了以下形式。
1
2
3
4
5
6
7
8
9
10
|
<!DOCTYPE HTML> < html > < body > < form method = "post" action = "http://example.com/form" > < input name = "fave" /> < input name = "name" /> < button >Submit Vote</ button > </ form > </ body > </ html > |
application/x-www-form-urlencoded
如果使用application / x-www-form-urlencoded編碼,每個數據項的名稱和值都使用用于編碼URL的相同方案進行編碼。這是編碼應用于示例形式的數據的方式:
fave=Apples&name=FiratName+LastName
特殊字符將替換為其HTML實體對應部分。數據項的名稱和值由等號(=)分隔,數據/值元組由&符號(&)分隔。
multipart/form-data
multipart / form-data編碼往往僅用于上傳文件。下面是示例表單中的數據如何編碼:
------WebKitFormBoundary2desQWER543CDFGF
Content-Disposition: form-data; name="fave" YourName
------WebKitFormBoundary2desQWER543CDFGF Content-Disposition: form-data; name="name" www.lezijie.cn
------WebKitFormBoundary2desQWER543CDFGF-- fave=Apple
name=www.lezijie.cn
multipart/plain
主流瀏覽器以不同的方式對該編碼進行編碼。
Google Chrome以與application / x-www-form-urlencoded方案相同的方式對數據進行編碼,而Firefox對數據進行編碼的方式如下:
fave=xml
name=www.lezijie.cn
每個數據項都放在一行上,不會對特殊字符進行編碼。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.51cto.com/14866389/2522717