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
- Gets the absolute path of the file to download
- Get the file name to download
- Set the Content-disposition response header to control the browser download form to open the file
- Get the file input stream to download
- Creating a data buffer
- Getting OutputStream streams from response objects
- Writes the FileInputStream stream to buffer buffers
- Using OutputStream to output buffer data to the client browser
second, the use of httpservletresponse to realize the download function considerations
- 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.
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