使用HttpServletResponse實現下載功能

來源:互聯網
上載者:User

標籤:gif   用戶端   soft   擴充   ima   eth   圖片下載   ext   size   

  下載功能也可以在js中進行實現,但是這種做法會將檔案地址暴露在外面,不安全。

  我們一般推薦使用背景方法實現下載。項目中使用spring-mvc架構,這種架構下controller即為servlet的一種,使用HttpServletResponse即可像servlet一樣設定response頭資訊,實現下載功能。

一、使用HttpServletResponse實現下載功能
  1. 擷取要下載的檔案的絕對路徑
  2. 擷取要下載的檔案名稱
  3. 設定content-disposition回應標頭控制瀏覽器以下載的形式開啟檔案
  4. 擷取要下載的檔案輸入資料流
  5. 建立資料緩衝區
  6. 通過response對象擷取outputstream流
  7. 將FileInputStream流寫入buffer緩衝區
  8. 使用outputstream將緩衝區的資料輸出到用戶端瀏覽器
二、使用HttpServletResponse實現下載功能的注意事項
  1. 編寫檔案下載功能時推薦使用outputstream流,避免使用PrintWriter流,因為OutputStream流是位元組流,可以處理任意類型的資料,而PrintWriter流是字元流,只能處理字元資料,如果用字元流處理位元組資料,會導致資料丟失。
  2. 下載中文檔案時,,需要注意的地方就是中文檔案名稱要使用URLEncoder.encode方法進行編碼(URLEncoder.encode(fileName, "字元編碼")),否則會出現檔案名稱亂碼。

    response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));

三、HttpServletResponse實現下載的原始碼(spring-mvc controller)

 

 1 /** 2 * 根據filePath將伺服器上的圖片下載到用戶端本地 3    */ 4 @RequestMapping("/downloadjpg") 5 public void downloadjpg(@RequestParam(value="fileId")String fileId,HttpServletResponse response){ 6     JcyAppealDatum datum=jcyAppealDatumService.get(fileId); 7     if (datum!=null) { 8         File file= new File(datum.getFilePath()); 9         if (file.exists()) {10             InputStream iStream=null;11             try {12                 iStream = new FileInputStream(file);13                 byte[] buffer = new byte[iStream.available()];14                 iStream.read(buffer);15                 String fileName = datum.getFileName()+".jpg";16                 response.setContentType("image/jpeg"); // 設定返回內容格式17                 // 設定返回相應的頭內容,中文名需轉碼(18                 String exportName = "attachment;filename=" + new String((fileName).getBytes("gb2312"), "ISO8859-1");19                 response.setHeader("Content-disposition", exportName);20                 response.getOutputStream().write(buffer);21             } catch (Exception e) {22                 e.printStackTrace();23             }finally {24                 try {25                     iStream.close();26                 } catch (Exception e2) {27                     e2.printStackTrace();28                 }    29             }30     }31             32     }33 }

 

四、幾種常見的contentType

序號

內容類型

副檔名

描述

1

application/msword

doc

Microsoft Word

2

application/octet-stream bin

dms lha lzh exe class

可執行程式

3

application/pdf

pdf

Adobe Acrobat

4

application/postscript

ai eps ps

PostScript

5

appication/powerpoint

ppt

Microsoft Powerpoint

6

appication/rtf

rtf

rtf 格式

7

appication/x-compress

z

unix 壓縮檔

8

application/x-gzip

gz

gzip

9

application/x-gtar

gtar

tar 文檔 (gnu 格式 )

10

application/x-shockwave-flash

swf

MacroMedia Flash

11

application/x-tar

tar

tar(4.3BSD)

12

application/zip

zip

winzip

13

audio/basic

au snd

sun/next 音效檔

14

audio/mpeg

mpeg mp2

Mpeg 音效檔

15

audio/x-aiff

mid midi rmf

Midi 格式

16

audio/x-pn-realaudio

ram ra

Real Audio 聲音

17

audio/x-pn-realaudio-plugin

rpm

Real Audio 外掛程式

18

audio/x-wav

wav

Microsoft Windows 聲音

19

image/cgm

cgm

電腦圖形元檔案

20

image/gif

gif

COMPUSERVE GIF 映像

21

image/jpeg

jpeg jpg jpe

JPEG 映像

22

image/png

png

PNG 映像

 

  

使用HttpServletResponse實現下載功能

聯繫我們

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