Summary of JSP Commons FileUpload component File Upload

Source: Internet
Author: User

Maybe good things need to be discovered and summarized slowly.

Previously, the JSP system I wrote was solved by using the Jsp SmartUpload component.

A few days ago, the customer complained that when uploading large files, the browser did not respond, and even crashed. ask me to help solve the problem and add the upload progress representation (this temporary and no table, there is a need to add me MSN: info@hkeb.com ).

Google and baidu now, we know that JSp SmartUpload is suitable for small files, and the commons FileUpload component is required when uploading large files.

I read countless pieces of information about this stuff online. Basically, every piece of information is similar. It's no wonder that everything on the Internet is transferred.

The file upload solution is solved, but other text field parameters on the form do not know how to receive them. It takes a week or two to solve the problem.

Because none of the articles mentioned how to solve other text data of the form.

It was not until today that a feasible method was found.

1: If you want to pass the parameter, if the data volume is not too large, you can use the get method with the parameter. That is, the upload. jsp in form action is followed by the parameter.

Example: upload. jsp? Id = 1 & bid = 2 & uid = somebody

The Form Type remains unchanged. In this way, you can use request. getParameter ("id") to obtain parameters before upload. jsp processes uploaded file data.

This is a feasible method when the data size of parameter passing is small.

2: If there is no solution, it must be submitted to the Upload Component together with the file domain for processing. According to netizens, the following code can be used for processing.

Public MultipartHttpServletRequest resolveMultipart (HttpServletRequest request) throws MultipartException {
DiskFileUpload fileUpload = this. fileUpload;
String enc = determineEncoding (request );

// Use prototype FileUpload instance if the request specifies
// Its own encoding that does not match the default encoding
If (! Enc. equals (this. defaultEncoding )){
FileUpload = new DiskFileUpload ();
FileUpload. setSizeMax (this. fileUpload. getSizeMax ());
FileUpload. setSizeThreshold (this. fileUpload. getSizeThreshold ());
FileUpload. setRepositoryPath (this. fileUpload. getRepositoryPath ());
FileUpload. setHeaderEncoding (enc );
}

Try {
List fileItems = fileUpload. parseRequest (request );
Map parameters = new HashMap ();
Map multipartFiles = new HashMap ();
For (Iterator it = fileItems. iterator (); it. hasNext ();){
FileItem fileItem = (FileItem) it. next ();
If (fileItem. isFormField ()){
String value = null;
Try {
Value = fileItem. getString (enc );
}
Catch (UnsupportedEncodingException ex ){
Logger. warn ("cocould not decode multipart item '" + fileItem. getFieldName () +
"'With encoding'" + enc + "': using platform default ");
Value = fileItem. getString ();
}
String [] curParam = (String []) parameters. get (fileItem. getFieldName ());
If (curParam = null ){
// Simple form field
Parameters. put (fileItem. getFieldName (), new String [] {value });
}
Else {
// Array of simple form fields
String [] newParam = StringUtils. addStringToArray (curParam, value );
Parameters. put (fileItem. getFieldName (), newParam );
}
}
Else {
// Multipart file field
CommonsMultipartFile file = new CommonsMultipartFile (fileItem );
MultipartFiles. put (file. getName (), file );
If (logger. isDebugEnabled ()){
Logger. debug ("Found multipart file [" + file. getName () + "] of size" + file. getSize () +
"Bytes with original filename [" + file. getOriginalFilename () + "], stored" +
File. getStorageDescription ());
}
}
}
/***** Note that parameters is the value of fields such as plain text *****/
Return new DefaultMultipartHttpServletRequest (request, multipartFiles, parameters );
}
Catch (FileUploadBase. SizeLimitExceededException ex ){
Throw new MaxUploadSizeExceededException (this. fileUpload. getSizeMax (), ex );
}
Catch (FileUploadException ex ){
Throw new MultipartException ("cocould not parse multipart request", ex );
}
}

The usage is as follows:

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Then you can read the parameters normally:
MultipartRequest. getParameter ("xxx ");

No. Can you get it? I didn't verify it. I 'd be grateful to you for telling me if the verification was successful.

The above code reference web site: http://dev2dev.bea.com.cn/bbs/thread.jspa? ForumID = 121 & threadID = 16067 & messageID = 97803

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.