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
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