Some summary of uploading files on JSP Commons FileUpload components

Source: Internet
Author: User
js| Upload

Maybe the good things need to be discovered and summed up slowly.

I used to write JSP systems are using JSP smartupload components to solve.

A few days ago, the customer complained that when uploading large files, browsers did not respond, or even crash. Asked me to help solve and add upload progress (this is not a list, there is a need to add me msn:info@hkeb.com).

Immediately Google, Baidu, just know that JSP smartupload applicable to the comparison of small files, and if the upload large files are also commons FileUpload components.

I have read countless articles about this stuff on the Internet. Basically, every piece of information is similar. It's no wonder that everything on the internet is turning around.

The upload file is resolved, but other text field parameters on the form do not know how to receive it. I was badly beaten, and I didn't solve it for one or two weeks.

Because all of the articles do not mention how to resolve other text data in the form.

It was not until today that a more workable approach was found.

1: If you want to pass the parameter, the amount of data is not too large, you can use get of the method with the parameter. That is, the upload.jsp behind the form action.

For example: Upload.jsp?id=1&bid=2&uid=somebody

The type of the form does not change. This allows you to obtain parameters using Request.getparameter ("id") before upload.jsp processing the uploaded file data.

This is a more feasible method in the case of a small amount of data being transmitted.

2: If there is no way, the file domain must be submitted to the upload component with the processing. According to the Netizen said. You can use the following code 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 so 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 ("Could 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 a field such as normal text *****/
return new Defaultmultiparthttpservletrequest (request, multipartfiles, parameters);
}
catch (Fileuploadbase.sizelimitexceededexception ex) {
throw new Maxuploadsizeexceededexception (This.fileUpload.getSizeMax (), ex);
}
catch (Fileuploadexception ex) {
throw new Multipartexception ("Could not parse multipart request", ex);
}
}

The use method is:

Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Then you can read the parameters normally:
Multipartrequest.getparameter ("xxx");

OK, can not get to, I did not go to verify that there is a successful certification to inform the younger brother a sound, be grateful.



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.