Method of file upload and download based on Java Web

File upload overview

To realize the file upload function in web development, the following two steps need to be completed:

Add upload entry in web page

Read the data of the uploaded file in the servlet and save it to the local hard disk.

How to add an upload entry to a web page?

The < input type = "file" > tag is used to add the file upload input item in the web page. When setting the file upload input item, you must pay attention to:

1. The name attribute of the input entry must be set, otherwise the browser will not send the data of the uploaded file.

2. The enctype attribute value of the form must be set to multipart / form data After setting this value, the browser will attach the file data to the HTTP request message body when uploading the file, and describe the uploaded file using MIME Protocol to facilitate the receiver to analyze and process the uploaded data.

File upload overview

How to read file upload data in servlet and save it to local hard disk?

The request object provides a getinputstream method, which can read the data submitted by the client. However, because users may upload multiple files at the same time, it is a very troublesome job to program on the servlet side to directly read the uploaded data and parse the corresponding file data respectively, for example.

In order to facilitate users to process file upload data, Apache open source organization provides an open source component (Commons fileUpload) for handling form file upload. This component has excellent performance, and its API is extremely simple to use, which allows developers to easily realize the web file upload function. Therefore, the common fileUpload component is usually used to realize the file upload function in Web Development.

To upload files using the Commons fileUpload component, you need to import the corresponding supporting jar packages of the component: Commons fileUpload and Commons io. Commons IO does not belong to the development jar file of the file upload component, but the Commons fileUpload component starts from version 1.1 and needs the support of the Commons IO package when it works.

FileUpload component workflow

Core API - diskfileitemfactory

Diskfileitemfactory is a factory for creating fileitem objects. The common methods of this factory class are:

Public void setsizethreshold (int sizethreshold): sets the size of the memory buffer. The default value is 10K. When the uploaded file is larger than the buffer size, the fileUpload component will use the temporary file cache to upload the file.

Public void setrepository (Java. Io. File repository): Specifies the temporary file directory. The default value is system getProperty("java.io.tmpdir").

Public diskfileitemfactory (int sizethreshold, java.io.file repository): constructor

Core API -- servletfileupload

Servletfileupload is responsible for processing the uploaded file data and encapsulating each input item in the form into a fileitem object. Common methods include:

Boolean ismultipartcontent (HttpServletRequest request): judge whether the uploaded form is multipart / form data type

List parserequest (HttpServletRequest request): parse the request object, wrap each input item in the form into a fileitem object, and return a list collection containing all fileitems.

Setfilesizemax (long filesizemax): set the maximum value of uploaded files setsizemax (long sizemax): set the maximum value of the total number of uploaded files setheaderencoding (Java. Lang. string encoding): set the encoding format setprogresslistener (progresslistener plistener)

File upload case

Implementation steps

1. Create a diskfileitemfactory object, set the buffer size and temporary file directory. 2. Create a servletfileupload object using the diskfileitemfactory object, and set the size limit of the uploaded file. 3. Call servletfileupload The parserequest method parses the request object to get a list object that holds all uploaded content. 4. Iterate over the list, one fileitem object per iteration, and call its isformfield method to determine whether to upload the file

If it is an ordinary form field, call getfieldname and getString methods to get the field name and field value

To upload a file, call the getinputstream method to get the data input stream to read the uploaded data.

File upload by encoding

Processing details of uploaded files

Garbled Chinese documents

For the problem of Chinese garbled file names, you can call the setheaderencoding method of servletuploader or set the SetCharacterEncoding property of request

Deletion of temporary files

Because the file size exceeds diskfileitemfactory When the size of the memory buffer set by the setsizethreshold method, the Commons fileUpload component will use a temporary file to save the uploaded data. Therefore, be sure to call fileitem at the end of the program The delete method deletes the temporary file.

The call of the delete method must be after the stream is closed, otherwise the file will be occupied and the deletion will fail.

File storage location

In order to ensure the security of the server, the uploaded files should be saved in the WEB-INF directory of the application or a directory not managed by the web server.

In order to prevent multiple users from uploading files with the same file name, resulting in file coverage, the file upload program shall ensure that the uploaded files have a unique file name.

In order to prevent too many files in a single directory from affecting the file reading and writing speed, the program handling uploaded files should select an appropriate directory structure generation algorithm according to the possible total number of files uploaded, and store the uploaded files in a decentralized manner.

File download

Because the files to be downloaded can be various types of files, the corresponding contents of the files to be transferred to the client should be treated as binary. Therefore, the method should be called to return the servertoutputstream object to write the file contents to the client.

Download case

Traverse all files in the upload directory, display them to the user, and allow the user to complete the download.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>