PDA

View Full Version : java.lang.ClassCastException in File Upload


Sharath
Sep 10th, 2007, 06:33 AM
Hello All,
in my onSubmit of FileUploadController:
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; throwing an java.lang.ClassCastException.

code snippet
serverlet.xml
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsM ultipartResolver" />

<bean id="fileUploadController"
class="com..ui.FileUploadController">
<property name="formView" value="fileUpload" />
<property name="successView" value="fileUpload" />
</bean>
jsp
<p>Select a file to upload:</p>
<form method="post" action="fileUpload.html"
enctype="multipart/form-data"><input type="file" name="file" /><br />
<input type="submit" value="Upload" class="button"
style="margin-top: 5px" /></form>
</div>
controller
public FileUploadController() {
super();
setCommandClass(FileUpload.class);
}

/*
* (non-Javadoc)
*
* @see org.springframework.web.servlet.mvc.BaseCommandCon troller#initBinder(javax.servlet.http.HttpServletR equest,
* org.springframework.web.bind.ServletRequestDataBin der)
*/
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}

/*
* (non-Javadoc)
*
* @see org.springframework.web.servlet.mvc.SimpleFormCont roller#onSubmit(javax.servlet.http.HttpServletRequ est,
* javax.servlet.http.HttpServletResponse, java.lang.Object,
* org.springframework.validation.BindException)
*/
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws ServletException, IOException {

FileUpload bean = (FileUpload) command;
byte[] bytes = bean.getFile();

// cast to multipart file so we can get additional information
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
.getFile("file");
Map model = new HashMap();
model.put("filename", file.getOriginalFilename());
model.put("url", url);
return new ModelAndView(getSuccessView(), "model", model);
}
command class:

public class FileUpload {
private byte[] file;

public void setFile(byte[] file) {
this.file = file;
}

public byte[] getFile() {
return file;
}
}


did i miss any thing, plz pore some light on this.

thnaks in advance
sharath

lumpynose
Sep 10th, 2007, 08:12 PM
I don't know if this still works but here's a zip file of a sample upload web app I made some time ago. I've uploaded it to two different places; hopefully one of them will work.

http://rapidshare.com/files/54815064/webapp09.zip.html

http://www.mediafire.com/?cqcgz9jccyd

lumpynose
Sep 10th, 2007, 08:21 PM
I stripped out some files from the directory when I made that zip file. Here are the names of the jars that were in the lib directory.

spring-2.0-rc3.jar
commons-fileupload-1.1.1.jar
commons-io-1.2.jar
commons-logging-1.1.jar
jstl-1.1.2.jar
log4j-1.2.13.jar
standard-1.1.2.jar

Sharath
Sep 11th, 2007, 02:34 AM
hello, thanks for the reply. your links are very helpful.
The problem was duplicate form "POST" in jsp
After deleting the one form tag it is working fine :).

liying
Oct 24th, 2007, 04:18 PM
Hi Sharath,

I got the exact same problem as yours. Actually, how did you fix that? You mentioned "The problem was duplicate form "POST" in jsp
After deleting the one form tag it is working fine". Did you mean removing "method="post" from "<form method="post" action="fileUpload.html" enctype="multipart/form-data">"? I didn't get it.

Could you provide more info? Thanks a lot.

Liying

Sharath
Oct 25th, 2007, 08:45 AM
Hi Liying,
in my jsp there was <form method ="POST" > and inside it one more <form method ="POST" >, So i deleted one then it wrked fine.
Check if u have any nested <form method ="POST">
cheers
sharath