When you use a form to transfer data, if the form adds a enctype= "Multipart/form-data" attribute, the form request is passed to another JSP or servlet
It is not possible to use Request.getparameter () to get the values of individual form elements.
This can be common (upload the API provided by the component):
Boolean ismultipart = servletfileupload.ismultipartcontent (request);
if (Ismultipart) {
Create a factory for disk-based file items
Org.apache.commons.fileupload.FileItemFactory factory = new Diskfileitemfactory ();
Create a new file upload handler
Servletfileupload upload = new Servletfileupload (factory);
Parse the request
List/* Fileitem */items = upload.parserequest (request);
Process the uploaded items
Iterator iter = Items.iterator ();
while (Iter.hasnext ()) {
Org.apache.commons.fileupload.FileItem item = (Org.apache.commons.fileupload.FileItem) ITER
. Next ();
if (Item.isformfield ()) {
String name = Item.getfieldname ();
String value = item.getstring ("GBK");
OUT.PRINTLN (name + "=" + value);
Params.put (Name.touppercase (), Value.trim ());
} ......
**************************************************************************************
When uploading using Multipart/form-data, the request sent is different from the normal HTTP, which requires conversion before other parameters can be read.
If you use spring, it provides a multirequestresolver that only needs to:
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
The parameters can then be read normally:
Multipartrequest.getparameter ("xxx");
Here's how spring is handled, and you must first install the Commons-fileupload component:
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 this 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 normal value of a field such as 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);
}
}
**************************************************************************************
<form name= "UserInfo" method= "post" action= "first_submit.jsp" enctype= "Multipart/form-data" >
Set enctype= "Multipart/form-data" in the Form tab to ensure the correct encoding of the anonymous upload file.
As follows:
<tr>
<TD height= "" "align=" right "> Upload Enterprise business license Picture:</td>
<td><input type= "FILE" name= "UploadFile" size= "onchange=" "Checkimage ()" ></td>
</tr>
You have to add Enctype= "Multipart/form-data".
The meaning of enctype= "Multipart/form-data" in the form is to set the MIME encoding of the form. By default, this encoding format is application/x-www-form-urlencoded, cannot be used for file upload, only use the Multipart/form-data to complete the transfer of file data, do the following.
Enctype=\ "Multipart/form-data\" is to upload binary data; The value of input in the form is passed in 2 binary ways.
The value of input in the form is passed through in 2, so the request is not valued. In other words, this code is added, and the request will not be successful.
When you add a form value to a database, use the following:
Smartupload su = new smartupload ();//Create a new Smartupload object
Su.getrequest (). Getparametervalues (); Take array values
Su.getrequest (). GetParameter (); Take a single parameter single value
Form form enctype= "Multipart/form-data" use doubt