Support for file upload annotations in servlet3.0 @multipartconfig found an example at the beginning of the printed information in Content-disposition, a moment of curiosity, so understand a bit. By the way, learn the necessary precautions for file upload.
Content-disposition is an extension of the MIME protocol, and the MIME protocol indicates how the MIME user agent displays additional files. When Internet Explorer receives the header, it activates the file Download dialog box, and its file name box automatically fills in the filename specified in the header. (Note that this is a design cause; You cannot use this feature to save a document to a user's computer without asking the user for the save location.) )
When the server sends a file to the client browser, if it is a file type supported by the browsers, it will usually be opened by default browser, such as txt, JPG, etc., will be displayed directly in the browser, if you need to prompt the user to save, we should use content-disposition to do a bit of processing, The key is to be sure to add attachment:
Response.appendheader ("Content-disposition", "attachment;filename=filename.txt");
Note: This will prompt the browser to save or open, even if you choose to open, will also use the associated program such as Notepad open, instead of IE directly opened.
Content-disposition is to provide a default file name when the user wants to save the requested content as a file. The specific definitions are as follows:
Content-disposition = "Content-disposition" "": "
Disposition-type * (";" Disposition-parm)
Disposition-type = "Attachment" | Disp-extension-token
Disposition-parm = Filename-parm | Disp-extension-parm
Filename-parm = "FileName" "=" quoted-string
Disp-extension-token = Token
Disp-extension-parm = Token "=" (token | quoted-string)
So the concrete examples are:
Content-disposition:attachment; Filename= "Filename.xls"
Of course, the filename parameter can contain path information, but user-agnet ignores the information and only makes the last part of the path information the file name. When you use this header message in response type Application/octet-stream, it means that you don't want to display the content directly, but instead pop up a "File download" dialog box, and then you decide whether to "open" or "save".
Precautions:
1. When the code uses Content-disposition to ensure that the browser pops up the download dialog box. Response.AddHeader ("Content-disposition", "attachment"); Make sure you haven't done anything about prohibiting browser caching. As follows:
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Otherwise you will find the download function in opera and Firefox inside good no problem, under IE is not, just can't find the file.
Example:
protected voidDoPost (httpservletrequest req, httpservletresponse response)throwsservletexception, IOException {req.setcharacterencoding ("UTF-8"); Part part= Req.getpart ("Upload"); //formats such as: Form-data; name= "upload"; filename= "YNote.exe"String disposition = Part.getheader ("Content-disposition"); SYSTEM.OUT.PRINTLN (disposition); String FileName= Disposition.substring (Disposition.lastindexof ("=") + 2, Disposition.length ()-1); String FileType=Part.getcontenttype (); LongFileSize =part.getsize (); System.out.println ("FileName:" +fileName); System.out.println ("FileType:" +FileType); System.out.println ("FileSize:" +fileSize); //the path must first be created under the root directory/uploadString Uploadpath = Req.getservletcontext (). Getrealpath ("/upload"); System.out.println ("Uploadpath" +Uploadpath); Part.write (Uploadpath+ File.separator +fileName); }
of the print section
Form-data; Name= "Upload"; Filename= "Clipboard.png"
FileName:clipboard.png
Filetype:image/png
filesize:39917
Uploadpathd:\installsoft\tomcat\apache-tomcat-7.0.65\wtpwebapps\testweb\upload
The role of content-disposition in the header