Commons-fileupload User's Guide (document translation)

Source: Internet
Author: User
Tags file upload html form interface
Use FileUpload depending on your application needs, FileUpload can have many different ways to use it. In the simplest case, you can call an easy way to parse the servlet request and then process the form list as they are submitted to your application. On other terminals, you may also decide to customize the FileUpload to fully control how individual tables are stored. For example, you might want to stream content to the data. Over here. We will describe the basic usage of fileupload, and then explain some of the simpler and most commonly used patterns. The personalization of FileUpload is described here. How does it work? A file upload request consists of a list of ordered table items that are encoded according to RFC1867 and are uploaded in HTML based on the form's file. FileUpload can parse such a request and provide a list of individual upload form items to your application. Each of these form items implements the Fileitem interface without having to consider how it is potentially implemented. Each file's form item has a series of properties that may be useful to your application, for example, each item has a name and file type, and you can provide a inputstream to fetch its data. On the other hand, you might want to deal with these options differently, depending on whether the data comes from a normal text box or a simple HTML form field, or a file that can be uploaded, based on a list item that is not a rule for this option. The Fileitem interface provides a variety of ways to determine whether it is an uploaded file, and then you can process the data in the most appropriate way. FileUpload uses Fileitemfactory to create a new file project. That's why it gives fileupload flexibility. The factory ultimately controls the creation of each project. The default factory stores data for the project in memory or on disk, depending on the size of the project (for example, byte data). Of course, this action can be customized to meet the needs of your application. Parsing requests before you deal with the options to upload, it is clear that you need to parse the requests themselves. It's straightforward to make sure that the request is not a file to upload, but FileUpload makes it simpler, and you just provide a static way to do it. Check if it is a file upload request Boolean ismultipart = Fileupload.ismultipartcontent (request); Now we are ready to parse this request to an alternative option. The result of parsing is a list of file options, each of which implements the Fileitem interface, and processing these options will beFace was discussed. In the simplest case, the simplest use scenario can refer to the following: The uploaded option must reside in memory at a modest size; the larger file upload option must be written to the temporary file on the disk; the large file upload request must not be allowed; The default memory-resident option maximum size, the maximum allowable upload file request, And the storage of temporary files is acceptable; it is not easy to handle such a request in this scenario://Create a new file upload handle diskfileupload upload = diskfileupload ()/Resolution request List/* Fileitem/items = upload.parserequest (request); That's all we need to do, really! The result of parsing is a list of file items, each of which implements the Fileitem interface. The processing of these items is discussed below. Practice more control If your use scenario is very close to the simplest way to use, as you can see in the above, but you need more control over the critical size and temporary file hosting address, you can use the Diskfileupload class method to customize these actions, like this:// Create a new file upload handle diskfileupload upload = Update diskfileupload ()//Set upload parameter Upload.setsizethrehold (maximum memory size); Upload.setsizemax ( Maximum request size); Upload.setrepositorypath (temp directory)//Resolution request LIST/* Fileitem/items = upload.parserequest (request); Of course, Each configuration method is separate from the others, but if you want to configure them at once, you can use the optional Parserequest () method, like this://Create a new file upload handle diskfileupload upload = the newer diskfileupload ()//Resolution request LIST/* Fileitem/items = upload.parserequest (request,        memory size, Maximum files, temporary directories that are allowed to upload; If you want to control the parsing of requests more, such as storing the upload options elsewhere, for example, in a database-you can refer to the custom fileupload. Processing upload options Once the parsing process is complete, you can get a list of file options,For further processing. In most cases, you will be able to handle file uploads differently depending on the form field of the rule. So you might be able to deal with://Process upload options iterator iter = Items.iterator () while (Iter.hasnext ()) {    Fileitem item = ( Fileitem) Iter.next ();    if (Item.isformfield ()) {        Processformfield (item);   } else {        ProcessUploadedFile ( Item);   }} for a regular form field, you may be interested in only its name and its string value. You will also think that handling them is simple://processing a Rule form field if (Item.isformfield ()) {    String name = Item.getfieldname ();     String value = item.getstring ();    ...} And for a file to upload, before you deal with its content, there are a lot of different things you want to know, here is a use of some of the methods you may be interested in the example//process a file upload if (!item.isformfield ()) {     String fieldName = Item.getfieldname ();    string fileName = Item.getname ();    String ContentType = Item.getcontenttype ();    Boolean isinmemory = Item.isinmemory ();     Long sizeinbytes = Item.getsize();    ...} For these uploaded files, you generally do not want to access them through memory, unless they are small, or you do not have any other good method, further, you want to treat the content as a file flow, or the entire file to the final address. FileUpload provides a simple way to do these things. Handling a file Upload situation if (writetofile) {    file UploadedFile = new file (...);     Item.write (uploadedfile);} else {    InputStream uploadedstream = Item.getinputstream ();    Uploadedstream.close ();} Note that in the default FileUpload implementation, the Write () method attempts to rename the file to a specific location, if the data is already in the temporary file, and if the rename fails, the actual copy file is completed (?). For other reasons, or the data is already in memory. If you do need to take the uploaded data in memory, you simply call the Get () method to obtain it as an array of characters. Processing an uploaded file in memory byte[] data = Item.get (); Interaction with antivirus software when the Web container is running, while antivirus software runs on the same system at the same time, the use of fileupload in applications can lead to unpredictable things. This section will describe some of the situations you might encounter, and we will provide some ways to deal with them. The default FileUpload implementation will write options that exceed its upload size in memory to disk. When such a file is closed, the antivirus software in any system wakes up and checks it, potentially isolating the file-that is, moving it to a specific place that does not cause problems. This, of course, is an accident for developers, as the files just uploaded will not be processed. On the other hand, those uploaded files that are smaller than the set memory size will be kept in memory, this will not be detected by antivirus software, so it is possible that the virus resides in a system in some way (although, if it is written to disk, antivirus software locates and detects it). A common solution is to set up a directory specifically in the system to store these uploaded files, and then configure the antivirus softThe item ignores this directory. This will ensure that uploaded files are not quarantined in the system, but this gives the application developer the responsibility to scan the virus. The task of scanning these uploaded files can be implemented in external processing. This allows you to move clean files to an "improved" location, or you can integrate antivirus into your application. As for how to scan an external processing or integration virus into an application, this is beyond the scope of this document. What's next is hopefully this page will give you a good idea of how to use FileUpload in your own application. For more on the methods described here, and other available methods, you can refer to the API documentation. The usage described here is already able to meet the needs of most file uploads, and, of course, if you have more complex requirements and the ability to use its flexible custom configuration, FileUpload will certainly be able to help you. Author:bruce chenemail:love.oss@gmail.commsn:cf_asp_master@yahoo.com.cn Welcome to correct me.

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.