|
|
| file upload in spring 2.5 |
Expert:ashok
hi,
i facing problem in file upload in spring 2.5
my FileUploadController.java file
package example;
import org.springframework.web.servlet.mvc.SimpleFormController; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor; import org.springframework.util.FileCopyUtils;
import java.io.File; import java.io.IOException; import java.util.Map; import java.util.HashMap; import java.net.BindException;
import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
import service.PriceIncrease;
import example.FileUploadBean;
public class FileUploadController extends SimpleFormController { protected final Log logger = LogFactory.getLog(getClass()); protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException, IOException , Exception {
// cast the bean FileUploadBean bean = (FileUploadBean) command;
// let's see if there's content there byte[] file = bean.getFile(); if (file == null) { // hmm, that's strange, the user did not upload anything }
// well, let's do nothing with the bean for now and return return onSubmit(request, response, command, errors); }
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws ServletException { // to actually be able to convert Multipart instance to byte[] // we have to register a custom editor binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); // now Spring knows how to handle multipart object and convert them } }
package example;
import java.io.Serializable;
/*public class FileUploadBean implements Serializable {
private static final long serialVersionUID = 1L;
private byte[] file;
public FileUploadBean() { super(); }
public FileUploadBean(byte[] file) { super(); setFile(file); }
public void setFile(byte[] file) { this.file = file; }
public byte[] getFile() { return file; }
}*/
My FileUploadBean.java file
import org.springframework.web.multipart.MultipartFile;
public class FileUploadBean {
private byte[] file;
public void setFile(byte[] file) { this.file = file; }
public byte[] getFile() { return file; } }
my fileuploadform.jsp file
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <html> <head> <title>Upload a file please</title> </head> <body> <h1>Please upload a file</h1> <form method="post" action="upload.form" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit"/> </form> <!-- <spring:bind path="fileUpload.file"> <input type="file" name="file" id="file" class="file medium" value="<c:out value="${status.value}"/>"/> </spring:bind> --> </body> </html>
the problem is when confire this using in springapp-servelt.xml like below i am getting the requested resource not found 404 error (when i start the tomcat5.50
the bean configuration is :
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /upload.form=fileUploadController </value> </property> </bean> <bean id="fileUploadController" class="example.FileUploadController"> <property name="commandClass" value="example.FileUploadBean"/> <property name="formView" value="fileuploadform"/> <property name="successView" value="confirmation"/> </bean>
pls help me in resolving the aove problem
thanks in advance |
| Answers |
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|