Using HttpServletResponse for download functions

Source: Internet
Author: User
Tags gtar

The download function can also be implemented in JS, but this approach exposes the file address to an external surface and is unsafe.

We generally recommend the use of the background method for download. The SPRING-MVC architecture is used in the project, in which the controller is one of the Servlets, and the httpservletresponse can be used to set the response header information like a servlet to achieve the download function.

first, the use of httpservletresponse to achieve the download function
    1. Gets the absolute path of the file to download
    2. Get the file name to download
    3. Set the Content-disposition response header to control the browser download form to open the file
    4. Get the file input stream to download
    5. Creating a data buffer
    6. Getting OutputStream streams from response objects
    7. Writes the FileInputStream stream to buffer buffers
    8. Using OutputStream to output buffer data to the client browser
second, the use of httpservletresponse to realize the download function considerations
    1. It is recommended to use OutputStream stream when writing file download function, avoid using printwriter stream, because OutputStream stream is byte stream, can handle any kind of data, and PrintWriter stream is character stream, can only handle character data, Processing byte data with a character stream can result in data loss.
    2. When downloading the Chinese file, it is important to note that the Chinese file name is encoded using the Urlencoder.encode method (Urlencoder.encode (filename, "character encoding")), otherwise the file name will be garbled.

      Response.setheader ("Content-disposition", "attachment;filename=" +urlencoder.encode (filename, "UTF-8"));

Third, httpservletresponse implementation of the downloaded source code (SPRING-MVC Controller)

1 /**2 * Download images from the server to the client locally according to FilePath3    */4@RequestMapping ("/downloadjpg")5  Public voidDownloadjpg (@RequestParam (value= "FileId") String fileid,httpservletresponse response) {6Jcyappealdatum datum=Jcyappealdatumservice.get (fileId);7     if(datum!=NULL) {8File file=NewFile (Datum.getfilepath ());9         if(File.exists ()) {TenInputStream istream=NULL; One             Try { AIStream =Newfileinputstream (file); -                 byte[] buffer =New byte[Istream.available ()]; - istream.read (buffer); theString fileName = datum.getfilename () + ". jpg"; -Response.setcontenttype ("Image/jpeg");//formatting returned content -                 //set to return the corresponding header content, the Chinese name must be transcoded ( -String exportname = "Attachment;filename=" +NewString ((fileName). GetBytes ("gb2312"), "Iso8859-1"); +Response.setheader ("Content-disposition", exportname); - Response.getoutputstream (). write (buffer); +}Catch(Exception e) { A e.printstacktrace (); at}finally { -                 Try { - istream.close (); -}Catch(Exception E2) { - e2.printstacktrace (); -                 }     in             } -     } to              +     } -}

Iv. several common contenttype

Serial number

Content Type

File name extension

Describe

1

Application/msword

Doc

Microsoft Word

2

Application/octet-stream bin

DMS LHA LZH EXE class

Executable program

3

Application/pdf

Pdf

Adobe Acrobat

4

Application/postscript

AI EPS PS

PostScript

5

Appication/powerpoint

Ppt

Microsoft Powerpoint

6

Appication/rtf

Rtf

RTF format

7

Appication/x-compress

Z

UNIX Compressed Files

8

Application/x-gzip

Gz

Gzip

9

Application/x-gtar

Gtar

Tar document (GNU format)

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 sound files

14

Audio/mpeg

MPEG MP2

Mpeg sound files

15

Audio/x-aiff

Mid MIDI RMF

Midi format

16

Audio/x-pn-realaudio

Ram RA

Real Audio Sound

17

Audio/x-pn-realaudio-plugin

Rpm

Real Audio Plugin

18

Audio/x-wav

Wav

Microsoft Windows Sound

19

Image/cgm

Cgm

Computer graphics meta-file

20

Image/gif

Gif

COMPUSERVE GIF Image

21st

Image/jpeg

JPEG jpg JPE

JPEG image

22

Image/png

Png

PNG Images

  

Using HttpServletResponse for download functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.