Had objects of type "java.lang.String" but expected signature "Org.apache.struts.upload.FormFile"

Source: Internet
Author: User

If the submission does not add enctype= "Multipart/form-data" will be an error:


Java.lang.IllegalArgumentException:-Argument type Mismatch-had objects of type "java.lang.String" but expected Signatu Re "Org.apache.struts.upload.FormFile"


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 and cannot be used for file uploads, and only the multipart/form-data can be used to complete the transfer of file data and perform the following actions.
Enctype= "Multipart/form-data" is the uploading of binary data; The value of the input in the form is passed in the form of 2.


The experience of enctype= "multipart/form-data" in form form


When you use a form to transfer data, if the form adds the 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 done universally (the API provided by the upload 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 using Multipart/form-data upload, the request sent is not the same as the normal HTTP, which requires conversion before other parameters can be read.

If you use spring, it provides a multirequestresolver that only requires:
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Then you can read the parameters 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 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);
}
}
====================================================================================================
<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 file is anonymously uploaded.
As follows:
<tr>
&LT;TD height= "align=" "right" > Upload 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 and cannot be used for file uploads, and only the multipart/form-data can be used to complete the transfer of file data and perform the following actions.
Enctype=\ "Multipart/form-data\" is the uploading of binary data; The value of the input in the form is passed in the form of 2.
The input value in the form is passed in 2, so the request is not worth it. In other words, if you add this code, the request will not succeed,

When you add a form value to a database, you use the following:
Smartupload su = new smartupload ()//New Smartupload Object
Su.getrequest (). Getparametervalues (); take the array value
Su.getrequest (). GetParameter (); Take a single parameter single value





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.