5.servlet Uploading Files

Source: Internet
Author: User

one. Maven dependency
<Dependency>            <groupId>Commons-fileupload</groupId>            <Artifactid>Commons-fileupload</Artifactid>            <version>1.3.1</version>        </Dependency>        <Dependency>            <groupId>Commons-io</groupId>            <Artifactid>Commons-io</Artifactid>            <version>2.4</version>        </Dependency>
two.use of the Diskfileitemfactory class

A task that encapsulates each item in the request message entity into a separate diskfileitem (implementation of the Fileitem interface) object
Default implementation by the Org.apache.commons.fileupload.FileItemFactory interface
Org.apache.commons.fileupload.disk.DiskFileItemFactory to finish. When the uploaded file item is compared to the hour, it is saved directly in memory (faster), relatively large, in the form of temporary files, saved in the disk temporary folder (although slower, but memory resources are limited).

Property
1) public static final int default_size_threshold: Saves the file in memory or the disk Temp folder's default threshold value of 10240, which is 10kb.

2) Private file repository: Used to configure the temporary folder that is used when the file item is larger than the critical value when the file project is created, default to the system default temporary file path, which can be obtained through the system Properties Java.io.tmpdir. The following code:

System.getproperty ("Java.io.tmpdir");

3) Private int sizethreshold: The critical value for saving the file in memory or disk Temp folder

Construction method
1) public diskfileitemfactory ()

The file item factory object is constructed with the default thresholds and the system Temp folder.

2) public diskfileitemfactory (int sizethreshold,file repository)

Use parameters to specify the critical value and the system Temp folder to construct the file item factory object.

3) Fileitem CreateItem ()
Each request message entity project is built into an Diskfileitem instance and returned according to the Diskfileitemfactory-related configuration. This method never needs to be called by us personally, and the FileUpload component is used internally when parsing the request.

4) void Setsizethreshold (int sizethreshold)
Apache File Upload Component when parsing the contents of each field in the uploaded data, it is necessary to temporarily save the parsed data for further processing of the data later (either in a disk-specific location or into a database). Because the Java virtual machine can use a limited amount of memory by default, a "Java.lang.OutOfMemoryError" error will be thrown when the limit is exceeded. If the uploaded file is large, such as 800M files, in memory will not be able to temporarily save the contents of the file, the Apache file Upload component to use temporary files to save the data, but if the uploaded file is small, such as 600-byte files, it is obvious that it will be stored directly in memory performance will be better.

The Setsizethreshold method is used to set whether the upload file is saved as a temporary file in the disk's critical value (int value in bytes), and the system default value of 10KB is applied if the method is not called to set this threshold. The corresponding Getsizethreshold () method is used to obtain this critical value.

5) void Setrepository (File repository)
The Setrepositorypath method is used to set the file to be stored as a temporary file in the storage directory on disk when the upload file size is greater than the critical value set by the Setsizethreshold method. There is a corresponding File getrespository () method that obtains a temporary folder.

Note: When you do not call this method to set the temporary file store directory, the default system default temporary file path is used, which can be obtained through the system Properties Java.io.tmpdir. The following code:

System.getproperty ("Java.io.tmpdir");

The Tomcat system default temp directory is "<tomcat installation directory >/temp/".

Three. Considerations for uploading large files using the Commons-fileupload package

Used in projects

Commons-fileupload-1.2.1.jar

Upload a large file.

Test a piece, the effect is very good.

Summarized as follows:

  1. The maximum threshold for uploading files must be set.

    final Long max_size = ten *1024x768 * 1024x768 *//Set upload file up to 10G

  2. You must set a temporary directory on the file upload server

    File Upload parameter configuration
    Create a new file upload handle
    Diskfileitemfactory factory = new Diskfileitemfactory ();
    Set the memory buffer beyond the post-write temp file
    Factory.setsizethreshold (4096);
    Set up a temporary storage directory for files uploaded to the server-very important to prevent the system disk from being stored on the system disk
    Factory.setrepository (New File ("F:\\uploadfiletemp"));
    Servletfileupload upload = new Servletfileupload (factory);
    Upload.setheaderencoding ("Utf-8");

    Set the maximum upload value for a single file
    Upload.setsizemax (max_size); //File upload limit 10G

  3. Be sure to delete temporary files from temporary directory after successful upload

    Fileitem.delete (); Be sure to call the file after the file upload is finished, delete the temp directory ...

  4. It is best to record the time at which the file is uploaded from the beginning to the end of the upload, which is useful for analyzing future file upload times.

In addition, because the STRUTS2 upload function also introduced the package, but did the interceptor limit file maximum upload size of 2M, you can modify its configuration file to dynamically change the size of the uploaded file, and struts will help you delete the temporary files after the file upload is successful.

I use spring MVC here, need to specify the maximum upload file size in the upload class, (this thing is not dynamically set, the maximum upload file size is generally the system standard, is to let users follow the drop), and must be uploaded after the successful deletion of temporary files.

5.servlet Uploading Files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.