[JAVAWEB learning notes] 29_file upload ------ commons-fileupload, commonsfileupload
Today's content:
File Upload ------ commons-fileupload
Object uploading and downloading: Object copying
File Upload: to copy a file from a local file to a server disk, the client needs to write a file upload form ----> the server needs to write code to accept the uploaded file
File Download: to copy a file from a server disk to a local disk, you need to write the server code. The download of the client is implemented by the browser kernel.
I. File Upload
1, File upload steps:
(1) You need to write a file upload form.
(2) Write server-side code to receive uploaded files
2Client Encoding
Note:
3The server receives the form data uploaded by the file.
Principles of file upload and receiving
Apache Commons-fileupload.jar-a convenient and quick tool for File Upload
Procedure:
(1) download file upload jar package fileuplaod with commons-io.jar
(2) Use APIs in the fileupload tool for encoding
4, FileUpload tool three core objects
DiskFileItemFactory: disk file item factory -- some related configuration settings cache size temporary directory location
ServletFileUplaod: a core class for File Upload
FileItem: represents each form item
5. Quick Start
6File Upload API details
(1) DiskFileItemFactory
Set the cache size. The unit of factory. setSizeThreshold () is byte.
Set the directory of the temporary File: factory. setRepository (File)
(2) ServletFileUpload
Determine whether the form is a file upload: ServletFileUpload. isMultipartContent (request );
Parse the request to obtain the set of form items: upload. parseRequest (request );
Set the encoding method of the uploaded file name: upload. setHeaderEncoding ("UTF-8 ");
(3) FileItem
Determine whether it is a normal form item: item. isFormField ();
Get the name attribute value of the form: item. getFieldName ();
Get the value of the form: item. getString ("UTF-8") --- UTF-8 represents the encoding when the Chinese character is obtained
Get the name of the uploaded file: item. getName ()
Obtain the uploaded file: item. getInputStream ();
Delete temporary files: item. delete ();