File Upload principle:
Browser:
1. The form method must be post.
2. to upload information, you must use the <input type = "file" name = "F"> component with a name.
3. You must specify the form's enctype value as multipart/form-data.
Server:
You can use request. getinputstream () to obtain the read information of the input stream.
Specific process:
1. Import the jar package
2. Use commons-fileupload to upload files
1. diskfileitemfactory ---- It is used to set file upload-related attributes
Diskfileitemfactory factory = new diskfileitemfactory ();
2. servletfileupload ----- complete file upload
Servletfileupload upload = new servletfileupload (factory );
3. fileitem ---- It is used to process upload items.
List <fileitem> fileitems = upload. parserequest (request );
API details
1. diskfileitemfactory
Constructor
Diskfileitemfactory () default buffer size: 10 K
Diskfileitemfactory (INT sizethreshold, File Repository) parameter sizethreshold represents the buffer size repository temporary file storage location
If the parameter-free structure is used during creation, you can also set the buffer size and temporary file storage location through the setxxx method provided by the parameter.
Setsizethreshold (INT sizethreashold) is used to set the buffer size
Setrepository (file F) is used to set the temporary file storage location
2. fileservletupload
1. ismultipartcontent
This method returns a Boolean value, which is used to determine whether the current upload operation is performed.
For an upload operation, we use the commons-fileuplad API to obtain all the upload information, instead of using a common web object request.
2. parserequest to get a list <fileitem>, which can be understood as all the upload items.
3. Set the size of the uploaded file
Void setfilesizemax (long filesizemax) sets the upload size of a single file
Void setsizemax (long sizemax) sets the total file upload size
4. Fixed garbled names of uploaded files
Setheaderencoding (string C) is used to solve the problem of garbled names of uploaded files.
3. fileitem
Isformfield () returns true, false, which indicates a common component and uploads a component.
The getname () common component obtains null, and the path of the uploaded file obtained by the Upload Component.
Getfieldname () gets the component name
The getstring () component obtains the value and the Upload Component obtains the file content.
Getinputstream (); it is used to operate the Upload Component to obtain an input stream pointing to the uploaded file.
For getstring (), there is an overloaded method getstring (string C), which is used to solve the garbled problem.
Delete () is used to delete temporary files.
------------------------------------------------------
Garbled Characters During the upload operation:
The request. setcharacterencoding ("UTF-8") cannot be used.
Use the commons-fileupload plug-in:
For common components: item. getstring (string encoding );
If the Upload Component is used: the name of the uploaded file is garbled: servletfileupload. setheaderencoding (string encoding );
File Download
For this download, you only need to set the href of the hyperconnection tag to point to the file to be downloaded.
Disadvantages:
1. If the file to be downloaded can be directly parsed by the browser, It will be opened in the browser when clicked. You must right-click and save.
2. The downloaded file must be accessible directly from the browser.
Server-side stream download:
The file to be downloaded is in the D:/upload directory.
If the content of the file to be downloaded is obtained through response and the output stream is written back to the browser
What it needs to do is to display the information in the browser.
Mimetype: indicates the type of data that the server notifies the browser to respond.
// Response. setcontexttype ("text/html; charset = UTF-8 ");
In this way, two information must be set for the file download operation:
1. respose. setcontenttype (); The mimetype type of the response data must be set.
After setting it, if the current mimetype browser can be parsed directly, it will be displayed in the browser,
If the file cannot be parsed, you will be asked to download the file, but the name of the downloaded file is incorrect.
2. response. setheader ("content-disposition", "attachment; filename = ?? ");
After this setting, the download operation is clicked, And the filename value is the name of the downloaded file.
Garbled downloading:
On the download2.jsp page, modify
<A href = "$ {pagecontext. Request. contextpath}/download1? Filename=1.txt "> download 1.txt </a> <br>
<A href = "$ {pagecontext. Request. contextpath}/download1? Filename .jpg "> download auto.jpg </a> <br>
<A href = "$ {pagecontext. Request. contextpath}/download1? Filenamehtmlhtml#.doc "> download html#.doc </a> <br>
Problem 1: In this case, the hyper-CONNECT request is a GET request. The data obtained from the server is garbled and cannot be found.
Solution: Garbled GET requests
Filename = new string (filename. getbytes ("iso8859-1"), "UTF-8"); // address the garbled GET request
Bytes ---------------------------------------------------------------------------------------------
Question 2: The name of the downloaded file is garbled
When downloading an object, the object name uses different encodings for different browsers.
Ie uses UTF-8 code.
Firefox uses base64 encoding.
Response. setheader ("content-disposition", "attachement; filename = ?? ");
If the filename value contains Chinese characters, the encoding used for parsing non-existing browsers is different.
If (agent. Contains ("MSIE ")){
// IE browser
Filename = urlencoder. encode (filename, "UTF-8 ");
Filename = filename. Replace ("+ ","");
} Else if (agent. Contains ("Firefox ")){
// Firefox
Base64encoder base64encoder = new base64encoder ();
Filename = "=? UTF-8? B? "
+ Base64encoder. encode (filename. getbytes ("UTF-8 "))
+ "? = ";
} Else if (agent. Contains ("Chrome ")){
// Google Browser
Filename = urlencoder. encode (filename, "UTF-8 ");
} Else {
// Other browsers
Filename = urlencoder. encode (filename, "UTF-8 ");
}