17. File Upload, download

Source: Internet
Author: User
Tags uuid


/* File Upload */(Common-fileupload) (Common-fileupload is dependent on Common-io for this package)
Use the Apache file Upload component to process file uploads/* steps */:
1. Create a diskfilefactory factory = new Diskfilefactory ();
2. Create a File Upload parser

1 New diskfileitemfactory (); 2 // The Setsizethreshold 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 method.  3// there is a corresponding File getrespository () method that gets the Temp folder.  4 factory.setrepository (thenew File(this. Getservletcontext (). Getrealpath ("/ Web-inf/temp "))); 5 6 New Servletfileupload (Factory);


3. Determine if the submitted data is the data of the upload form

1 if (! upload.ismultipartcontent (Request)) {2     // get Data 3 return in the traditional way      ; 4 }

4. Using the Servletfileupload parser to parse the upload data, the result of the parse is a list<fileitem> collection, each fileitem corresponding to the input of a form form

1 list<fileitem> List = upload.parserequest (request); 2  for (Fileitem item:list) {3     if (Item.isformfield ()) {


    

The file upload function has a lot to pay attention to */* small Details * * problem, the following points out the need to pay special attention to

Add Enctype= "Multipart/form-data" to the 0.form form and declare upload binary data

1. In order to ensure the security of the server, upload files should be placed in the outside world directly accessible directory tables, such as in the Web-inf directory

2. To prevent file coverage, create a unique file name for the upload file

3. To prevent too many files appearing under a directory, use the hash algorithm to break the storage

4. To limit the maximum number of uploaded files

5. To return to the type of upload file first, when you receive the upload file name, determine whether the suffix name is legitimate

6. To ensure that temporary files (when the upload file size exceeds the factory set buffer size (not set the default 10KB)) Delete, be sure to after processing the upload file, call the Item.delete method

7. Monitor File Upload Progress

8. Transfer entries on a Web page in a dynamically added file



/* File Download */

1. Provide the downloaded file resources on the JSP page

2. Implement file download

/* How to find the corresponding file after the hash is scattered */

/* After the hash is broken, the file name does not change and the UUID + filename is still in the servlet that handles the download.

The same hash break method, through the unique file name to download (uuid+ file name), and save the file to download the root directory, to download the file storage directory */

17. File Upload, download

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.