Today’s content:
File upload------commons-fileupload
The essence of file upload and download: copy of file
File upload: copy from local to server disk The client needs to write a file upload form---- >The server needs to write code to accept uploaded files
File download: copy from the server disk to the local disk You need to write server-side code, and the client's download work is solved by the browser kernel
(1) Need to write a file upload form
(2) Write server-side code with For receiving uploaded files
![](https://img.php.cn/upload/article/000/000/001/ed8a049816a4e985d3623c3bc7566296-0.png)
3, the server accepts the form for file upload Data
Principle of file upload and reception
![](https://img.php.cn/upload/article/000/000/001/ed8a049816a4e985d3623c3bc7566296-1.png)
Easy and quick to use File upload tool---Apache Commons-fileupload.jar
(1) Download the jar package for file upload fileuplaod with the help of commons-io.jar
(2) Use the API in the fileupload tool Encoding
4, the three core objects in the FileUpload tool
DiskFileItemFactory: Disk file item factory--some related configuration settings The size of the cache and the location of the temporary directory
ServletFileUplaod: A core class for file upload
FileItem: Represents each form item
6, Detailed explanation of file upload API
Set the cache size: factory.setSizeThreshold() in bytes
Set the directory of temporary files: factory.setRepository(File)
Determine whether it is a file upload form: ServletFileUpload.isMultipartContent(request);
Parse request to obtain a collection of form items: upload.parseRequest(request);
Set the encoding method of the uploaded file name: upload.setHeaderEncoding("UTF- 8");
Determine whether it is a normal form item :item.isFormField();
Get the name attribute value of the form: item.getFieldName();
Get the value value of the form: item.getString("UTF-8") --- UTF-8 represents the encoding when obtaining Chinese
Get the name of the uploaded file: item.getName()
Get uploaded files: item.getInputStream();
Delete temporary files: item.delete();
The above is the detailed content of Detailed explanation of java_file upload example. For more information, please follow other related articles on the PHP Chinese website!