Java upload and download using the common-fileupload component

Source: Internet
Author: User

Use fileupload 

Depending on your application requirements, fileupload can be used in many different ways. In the simplest case, you can call a simple method to parse servlet requests and process the form List when they are submitted to your application. On terminals of other sizes, you may also decide to customize fileupload to fully control the storage of individual form items. For example, you may stream the content to save it to data.
Here. We will describe the basic usage of fileupload, and then explain some simpler and most commonly used usage modes. The personalization of fileupload is described here.

How it works 

A file upload request consists of a list of ordered form items. These forms are encoded according to RFC1867 and are form-based file uploads in html. Fileupload can parse such a request and then provide your application with a separate list of upload form items. Each such form item implements the FileItem interface without considering its potential implementation methods. The form items of each file have a series of attributes that may be useful to your application. For example, if each project has a name and file type, you can provide an InputStream to retrieve its data. On the other hand, you may need to process these options differently. This can be determined based on whether the option is a rule form item, that is, whether the data is from a common text box, a simple html form field, or a file that can be uploaded. The FileItem interface provides multiple methods to determine whether a file can be uploaded. Then you can process the data in the most appropriate way. FileUpload uses FileItemFactory to create a new file project. This is why FileUpload is flexible. The factory eventually controls the creation of each project. The default factory stores project data in the memory or disk, which can be determined by the project size (for example, byte data ). Of course, this action can be customized to meet your application needs.

Resolution request 

Before processing the options to be uploaded, it is clear that you have to parse these requests. It is very straightforward to ensure that this request is not a file to be uploaded. However, FileUpload simplifies this point, and you only need to provide a static method to achieve this.



--------------------------------------------------------------------------------

// Check whether it is a file upload request
Boolean isMultipart = FileUpload. isMultipartContent (request );


--------------------------------------------------------------------------------

Now we can prepare to parse this request to an alternative option. The parsing result is a List of file options. Each of these options implements the FileItem interface. processing these options will be discussed below.

Simplest case

For the simplest use cases, refer to the following:

The uploaded option must reside in the memory at a moderate size;
Large File Upload options must be written to temporary files on the disk;
Large File Upload requests must not be allowed;
The maximum size of the default memory resident option, the maximum allowed Upload File requests, and the storage location of temporary files are acceptable;
In this scenario, it is not very easy to process such a request:


--------------------------------------------------------------------------------

// Create a new file upload handle
DiskFileUpload upload = new DiskFileUpload ();
// Parse the request
List/* FileItem */items = upload. parseRequest (request );


--------------------------------------------------------------------------------

This is all we need to do, really!
The parsing result is a List of file projects, each of which implements the FileItem interface. Processing these items will be discussed below.

Exercise more control 

If your application scenario is very similar to the simplest method of use, you can see it above, but you need to control the critical size and the resident address of the temporary file more, you can use the DiskFileUpload class method to customize these actions, like this:


--------------------------------------------------------------------------------

  DiskFileUpload upload =       List  items = upload.parseRequest(request);

 

--------------------------------------------------------------------------------

Of course, each configuration method is independent of others, but if you want to configure them once, you can use the optional parseRequest () method, like this:


--------------------------------------------------------------------------------

// Create a new file upload handle
DiskFileUpload upload = new DiskFileUpload ();
// Parse the request
List/* FileItem */items = upload. parseRequest (request,
Memory size, maximum file that can be uploaded, temporary directory );


--------------------------------------------------------------------------------

If you want to control Request Parsing more, such as storing the upload options elsewhere, for example, in the database-you can refer to custom FileUpload.

Process upload options

Once the parsing process is complete, you can obtain a List of file options for further processing. In most cases, you will process file uploads differently based on the Rule form fields. So you may handle it in this way:


--------------------------------------------------------------------------------

  Iterator iter =      FileItem item =            }    }

--------------------------------------------------------------------------------

For a rule form field, you may only be interested in its name and its string value. You will also think that processing them is simple:


--------------------------------------------------------------------------------

       String name =     String value =  }

--------------------------------------------------------------------------------

Before you process the content of a file, you can see a lot of different things that you want to know, here is an example using methods you may be interested in.


--------------------------------------------------------------------------------

   (!     String fieldName =     String fileName =     String contentType =      isInMemory =      sizeInBytes =  }

--------------------------------------------------------------------------------

For these uploaded files, you generally do not want to access them through the memory, unless they are very small, or you have no other good method, further, you want to process the content as a file stream, or write the entire file to the final address. FileUpload provides a simple way to complete these operations.


--------------------------------------------------------------------------------

// Process the upload of a file

 

      File uploadedFile =   }      InputStream uploadedStream =   }

--------------------------------------------------------------------------------

Note that in the implementation of the default FileUpload, the write () method will try to rename the file to save it to a specific location. If the data is already in the temporary file, if the rename fails, the actual copy file will be completed (?), For other reasons, the data is already in the memory. If you do need to retrieve the uploaded data in the memory, you only need to call the get () method to obtain it as a character array.


--------------------------------------------------------------------------------


// Process an uploaded file in the memory
Byte [] data = item. get ();
...

Interaction with anti-virus software

When the web container is running and the anti-virus software is running on the same system, using FileUpload in the application may cause some unpredictable problems. this part describes some situations you may encounter and we will provide some methods to deal with them. The default FileUpload implementation will write the upload options that exceed the memory size to the disk. When such a file is closed, any antivirus software in the system will be awakened, and then it will be checked, and the file will be potentially isolated-that is, move it to a specific place without issues. In this way, it is certainly an accident for developers, because the uploaded files cannot be processed. On the other hand, the uploaded files smaller than the specified memory size will be kept in the memory, so that they will not be detected by anti-virus software, in this way, the virus may reside in the system in some way (although anti-virus software will locate and detect the virus once it is written to the disk ). A common solution is to set a directory in the system to store these uploaded files, and then configure anti-virus software to ignore this directory. In this way, the uploaded files are not isolated in the system, but the virus scanning responsibility is handed over to the application developers. The task of scanning these uploaded files can be implemented in external processing. In this way, you can move clean files to a "Improved" place, or integrate anti-virus into the application. As for how to scan external processing or integrated viruses to
This is beyond the scope of this document.
What is next?
I hope this page can provide you with a good opinion so that you can use FileUpload in your own applications.

Related Article

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.