Struts: Implements argument Type Mismatch Error for image uploading.

Source: Internet
Author: User

Partial source code of upload. jsp:

<HTML: Form Action = "Upload. Do" method = "Post">

Select the photo to be uploaded:

<HTML: file property = "photofile"/>

<HTML: Submit value = "Upload"/>

</Html: Form>
Next, declare this attribute in actionform and set the getter and setter methods. The source code is as follows:

Public class uploadform extends actionform {

Protected formfile photofile;

Public formfile getphotofile (){

Return photofile;

}

Public void setphotofile (formfile photofile ){

This. photofile = photofile;

}

}
The photofile attribute of this form is not string or Boolean,
Org. Apache. Struts. Upload. formfile. Because the user uploads a binary file, and the HTTP protocol transmits data in the form of text, this requires
To convert. For example, a car needs to be sent from the ground a to the ground B, but there is only one cableway between the two places, and the car cannot be opened. So I want to find a way to remove the car in the ground, send parts to location B and then group again
Build a car. Formfile is used for disassembly and assembly, but it encapsulates the process of disassembly, transmission, and assembly. What we see is that a car enters from the ground.
Formfile. After a while, it will be opened from location B. All we have to decide is where to stop it. This is the activity of action.

Public class uploadaction extends action {
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception {
Uploadform = (uploadform) form;
If (! Photofile. getfilename (). Equals ("")){
Try {

Inputstream stream = photofile. getinputstream ();
String filepath = request. getrealpath ("/");
Filepath = filepath + photofile. getfilename ();
Outputstream Bos = new fileoutputstream (filepath );
Int bytesread = 0;
Byte [] buffer = new byte [400000];
While (bytesread = stream. Read (buffer, 0, 400000 ))! =-1 ){
Bos. Write (buffer, 0, bytesread );
Bos. Flush ();
Bos. Close ();
Stream. Close ();
}
} Catch (exception e ){
System. Err. Print (E );
}
Photofile. Destroy ();
}

Return (mapping. findforward ("toindex "));
}
}
The following definitions in <action-mappings> In the struts-config.xml:
<Action Path = "/upload" type = "com. WebEx. TMIS. uploadaction" name = "uploadform" Scope = "request" input = "/wrong. jsp"/>
Run: The following exception is thrown:
Struts's argument Type Mismatch Error ..
Java. Lang. illegalargumentexception: cannot invoke com. WebEx. TMIS. uploadform. setphotofile-Argument Type Mismatch,
Debug
It turns out that the string is forcibly converted to formfile, so the argument type will be thrown.
Mismatch: In the form (HTML: form), enctype = "multipart/form-Data" means to set the form
Mime encoding. By default, the encoding format is application/X-WWW-form-urlencoded and cannot be used for file upload.
Multipart/form-data to increase the function of transferring file data, perform the following operations. enctype = "multipart/form-Data" is
Upload binary data;
The input values in form are passed in binary format. When enctype = "multipart/form-Data" is added to the HTML form
The Slice upload problem is solved.
The upload. JSP code is as follows:
<HTML: Form Action = "Upload. Do" enctype = "multipart/form-Data" method = "Post">

Select the photo to be uploaded:

<HTML: file property = "photofile"/>

<HTML: Submit value = "Upload"/>

</Html: Form>

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.