Commons-FileUpload User Guide

xiaoxiao2021-03-06  21

Original link: http://iiu.cn/more.asp? Name = dingl & id = 4906

Using FileUPload Depending on your application requirements, FileUpload can have many different ways. In the simplest case, you can call a simple method to resolve the Servlet request and then process a list of forms when they are submitted to your application. On the other size terminal, you may also decide to customize the fileUpload to fully control the method of individual forms stored. For example, you may fluidize content to save the content. it's here. We will describe the basic use of FileUpload, then explain some simpler, and most common usage modes. FileUpload's Personalization is described here. How do it works a file upload request consists of a list of ordered forms, which is encoded according to RFC 1867, and is uploaded in the File of the form in HTML. FILEUPLOAD can parse such a list of requests and provide a list of individual top-page sheets to your app. Each such form item implements the FileItem interface without considering it. The form items for each file have a series of properties that may be useful to your app, for example, each project has a name and file type, you can provide an InputStream to take its data. From another aspect, you may need to do different processing on these options. This can be judged according to a form item that is not a rule, that is, whether it is derived from a normal text box or a simple HTML. A single field, or a file that can be uploaded. The FileItem interface provides a variety of ways to determine if it is an uploaded file, and then you can use the most appropriate way to handle this data. FILEUPLOAD uses FileItemFactory to create a new file item. This is why FileUpload brings flexibility. This factory finally controls the creation of each project. The default factory saves the data of the project on the memory or disk, which can be determined according to the size of the project (for example, byte data). Of course, this action can be customized to meet your application needs. The resolution request is clearly parsing these requests themselves before you have processing the options to upload. That is directly to make sure this request is a file to upload, but FileUpload makes this simple, you only need to provide a static method to do this.

/ / Check if it is a file upload request boolean ismultipart = fileupload.ismultipartContent (request);

Now we can prepare to resolve this request to an alternative option. The result of the resolution is a list of file options. Each such option implements the FileItem interface, and these options will be discussed below. The simplest use scenario can be referred to:

The upload option must reside in memory with moderate size; the relatively large file upload option must be written to the temporary file of the disk; the big file upload request must not be allowed; the default resident memory option is the largest Size, maximum allowable upload file request, and the storage place of temporary files is acceptable;

It is not very easy to deal with such a request under this scenario:

// Create a new file upload handle diskfileupload upload = new diskfileupload (); // parsing request list / * fileItem * / items = upload.parsequest (request);

This is all what we need to do, really! The result of the resolution is the List of a file project, each of which implements the FileItem interface. Handling these items will be discussed below. Exercise more Control If your use is very close to the easiest way of use, you can see it above, but you need more control of the critical size and temporary file residence address, you can use the method of the DiskfileUpload class From the definition of these actions, just like this: // Create a new file upload handle DISKFILEUPLOAD UPLOAD = new diskfileupload (); // Set upload parameter UPLOAD.SETSIETHREHOLD (maximum memory size); Upload.setsizeMax; Upload .setRepositoryPath (Temporary Catalog); // Resolution Request List / * FileItem * / items = UPLOAD.PARSEREQUEST (Request);

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

/ / Create a new file to upload the handle DISKFILEUPLOAD UPLOAD = New DiskfileUpload (); // Resolution Request List / * fileItem * / items = UPLOAD.PARSEREQUEST (Request, Memory Size, Allow Upload Maximum File, Temporary Catalog);

If you want to control the resolution of the request, such as store the upload option to other places, for example, to the database - you can refer to custom fileupload. Processing Upload Options Once the resolution process is completed, you can get a list of file options for further processing. In most cases, you will process the upload of the file different from the rule's form field. So you may handle this way:

// Test the upload option iterator it = items.ITerator (); while (ore.hasnext ()) {fileItem item = (fileItem) iter.next (); if (item.isformfield ()) {ProcessFormfield (item); } else {processuploadedfile (item);}}

For a rule's form field, you may only have its name and its string value for it. You will also think that it is simple:

// Process a rule form single field IF (item.isformfield ()) {string name = item.getfieldName (); string value = item.getstring (); ...}

For a file, you can have a lot of things you want to know before you handle it. There is an example of how you use some of you may be interested.

Processing 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 usually don't want to access them through memory, unless they are small, or there is no other good way, let's go further, or write content as a file flow, or write the entire file to the end the address of. FileUpload provides a simple way to complete these operations. // Handling a file uploading if (WritetOfile) {file uploadedfile = new file (...); item.write (UPLoadedFile);} else {INPUTSTREAM UPLOADSTREAM = item.getInputStream (); ... UPLOADSTREAM.CLOSE }

Note that in the implementation of the default fileupload, the Write () method will attempt to rename the file to a specific location, if the data is already in the temporary file, if the rename fails, the actual copy file is completed. (?), In other reasons, or data is already in memory. If you need to get uploaded data in memory, you only need to simply call the get () method to get it as a character array.

// Process an uploaded file byte [] Data = item.get (); ...

And the interaction of anti-virus software When the web container is running, the anti-virus software is running on the same system at the same time, in which case FileUpload is easy to cause some difficult things. This section will describe some you may In case, we will provide some methods to handle them. The default fileupLoad implementation will make the option exceeded to be uploaded to the disk to the disk. When such files are closed, anti-virus software in any system will be awakened, then check it, then it will potentially isolate this file - that it is said to move it to a specific place that does not have problems. In this way, it is of course an accident for developers because the file just uploaded will not be handled. On the other hand, those uploaded by less than the set memory size will be kept in memory, so that it will not be detected by the anti-virus software so that the virus may be resident in some way. Stayed in the system (although if it is written to disk, anti-virus software will position and detect it). A general solution is to set a directory specifically in the system to store these uploaded files, and then configure the anti-virus software to ignore this directory. This will ensure that the uploaded file is not isolated in the system, but this will give the scanned virus's responsibility to the developer of the application. Scanning these uploaded files can be implemented in external processing. This can move a clean file to a "improvement", or you can integrate anti-virus into your application. As for how to scan external processing or integrated viruses to an application, this has exceeded the discussion scope of this document. The next step is what I hope this page can provide you with a good comment, let you use FileUpload in your own app. More about the methods described here, as well as other available methods, you can refer to the API documentation. The usage introduced here is already possible to meet the needs of most files, of course, if you have more complex needs, use its flexible custom configuration capabilities, FileUpload can help you. Author: Bruce CHENEMAIL: love.oss@gmail.commsn: cf_asp_master@yahoo.com.cn

转载请注明原文地址:https://www.9cbs.com/read-43281.html

New Post(0)