-
Oct 1st, 2008, 10:54 AM
#1
problem with file upload
I am getting ClassCastException while trying to cast HttpServletRequest object into DefaultMultipartHttpServletRequest in my controller that extends SimpleFormController.
I have debugged and determined that the request object is type org.apache.catalina.connector.RequestFacade class.
The following is my "resolver" my config xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver"/>
This is the part from jsp :
<td><input type="file" size="100" name="fileData" id="fileData"/></td>
<td align=center><input class="exec-button" type=button value="Upload" onclick="fnUploadFile();"></td>
<script>
function fnUploadFile(){
document.uploadForm.enctype = "multipart/form-data";
document.uploadForm.submit();
}
</script>
This is my "command" bean:
public class FileData implements Serializable {
private byte[] fileData;
}
I also set custom binder to bind array class.
This is from my controller :
protected ModelAndView onSubmit( HttpServletRequest req,
HttpServletResponse res,
Object o,
BindException errors) throws Exception {
UploadData uploadData = (UploadData )o;
byte[] fileByteArray = uploadData.getFileData();
Class reqClass = req.getClass();
DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest) req;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("fileData");
I am getting byte array with content of uploaded file,but then the "cast" exception is thrown.
I need that cast because I need to get the name of uploaded file.
Please help to find out what I did incorrect.
Thanks.
-
Oct 1st, 2008, 11:12 AM
#2
I just determined that byte array i receive contain actually the file path name displayed in the "text" box located next to "browse" button on jsp, and not the content of the uploaded file.
It means that I definitely do something wrong.
Please help.
Thank you.
-
Oct 1st, 2008, 12:23 PM
#3
I bet you forgot to add
enctype="multipart/form-data"
to the <form> tag in your JSP.
-
Oct 1st, 2008, 08:27 PM
#4
No, i did not. The problem was that "enctype" attribute of form for IE browser should be set via "encoding" and not "enctype".
Anyway thanks for response.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules