Announcement Announcement Module
Collapse
No announcement yet.
Problem MultipartResolver, Help me please :( Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem MultipartResolver, Help me please :(




    Hi..

    I'm using the CommonsMultipartResolver to provide file uploading in my Server. But happen mistake , :-( , my code is , be using springframework for controller, and jstl for view.

    My file upload.jsp



    <%@ include file="/WEB-INF/jsp/include/include.jsp" %>

    <h3>File Upload</h3>
    <c:if test="${not empty model.filename}">
    <p style="font-weight: bold">Uploaded file (click to view): <a href="${model.url}">${model.filename}</a>
    </p>
    </c:if>
    <p>Select a file to upload:</p>
    <form method="post" enctype="multipart/formdata">
    <input id="proyecto" name="codigo" type="hidden" value="2">
    <input type="file" name="file" ><br/>
    <input type="submit" value="Upload" class="button" style="margin-top: 5px"/>
    </form>




    code my controller

    public class FileUploadController extends SimpleFormController {
    private CommonsMultipartResolver multipartResolver;
    MultipartHttpServletRequest multipartRequest;

    private static Log log = LogFactory.getLog(FileUploadController.class);

    public void setMultipartResolver(CommonsMultipartResolver multipartResolver){
    this.multipartResolver = multipartResolver;
    }
    protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder)throws ServletException {
    binder.registerCustomEditor(byte[].class,new ByteArrayMultipartFileEditor());

    }

    @SuppressWarnings("unchecked")
    public ModelAndView processFormSubmission(HttpServletRequest request,
    HttpServletResponse response, Object command,
    BindException errors)
    throws Exception {
    FileUpload bean = (FileUpload) command;
    byte[] bytes = bean.getFile();

    multipartRequest = multipartResolver.resolveMultipart(request); /// mistake happen here .......
    CommonsMultipartFile file = (CommonsMultipartFile)multipartRequest.getFile("fi le");

    String uploadDir = getServletContext().getRealPath("/upload/");
    // Create the directory if it doesn't exist

    File dirPath = new File(uploadDir);

    if (!dirPath.exists()) {
    dirPath.mkdirs();
    }

    String sep = System.getProperty("file.separator");

    if (log.isDebugEnabled()) {
    log.debug("uploading to: " + uploadDir + sep +
    file.getOriginalFilename());
    }

    File uploadedFile = new File(uploadDir + sep +
    file.getOriginalFilename());


    FileCopyUtils.copy(bytes, uploadedFile);
    // set success message
    request.getSession().setAttribute("message", "Upload completed.");
    String url = request.getContextPath() + "/upload/" +
    file.getOriginalFilename();
    Map model = new HashMap();


    model.put("filename", file.getOriginalFilename());
    model.put("url", url);

    return new ModelAndView("upload1View","model",model);
    }

    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException ec) throws Exception {

    return new ModelAndView("upload1View", "model", new HashMap());
    }
    }


    code my bean
    public class FileUpload {
    private byte[] file;

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

    configuration spring servelet.xml


    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.C ommonsMultipartResolver">
    <property name="maxUploadSize"> <value>100000</value> </property>
    </bean>

    <bean id="fileUploadController" class="memi.adminproyect.web.controlador.upload.Fi leUploadController">
    <property name="sessionForm"><value>false</value></property>
    <property name="formView"><value>upload1View</value></property>
    <property name="commandName"><value>fileUpload</value></property>
    <property name="commandClass"><value>memi.adminproyect.web.c ontrolador.upload.FileUpload</value></property>

    <property name="multipartResolver"><ref bean="multipartResolver"/></property>

    </bean>

    MISTAKE HAPPEN,

    org.springframework.web.multipart.MultipartExcepti on: Could not parse multipart request
    org.springframework.web.multipart.commons.CommonsM ultipartResolver.resolveMultipart(CommonsMultipart Resolver.java:240)
    memi.adminproyect.web.controlador.upload.FileUploa dController.processFormSubmission(FileUploadContro ller.java:51)
    org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:237)
    org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:128)
    memi.adminproyect.web.nucleo.ToolAdmintradorProyec to.handleRequest(ToolAdmintradorProyecto.java:74)
    org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
    org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:522)
    org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:321)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:802)

    Please , anybody know, because happen that error

    Thanks,for you help.

  • #2
    Re: Problem MultipartResolver

    The way you are using the MultipartResolver seems odd. What's the point of the code?

    Code:
    multipartRequest = multipartResolver.resolveMultipart(request); 
    CommonsMultipartFile file = (CommonsMultipartFile)multipartRequest.getFile("fi le");
    Spring will do this for you when it detects that the request is multipart. You can cast the HttpServletRequest object to a MultipartRequest. Of course if you have bound the file input parameter to your command object, this is done for you. It looks like that's what you've done in your code. If the bean.getFile() is giving you the byte[] you expect, then that's what's happened.

    Comment


    • #3
      other Problem , screen white

      thanks, your response fupeg.

      But, hapen other mistake . make to modify, now Im implement how reference-springframework.pdf. Im using library commons-fileupload.jar and commons-oi.jar since happens errors without the jar commons-io.jar search web find, that response previous problem.

      um... ,but now, when press onSubmit return null, not
      to recover data, return screen white. Thinks shoulds return errors o other thing

      my code modify is.



      protected ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException err) throws Exception {

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

      if(bean.getFile()==null){

      return new ModelAndView("error500View","model",""); //never it executes that instruction
      }

      return super.onSubmit(request, response, command, err); //never it executes that instruction

      }


      Thanks,for you help

      Comment


      • #4
        use multipart/form-data instead multipart/formdata

        Comment

        Working...
        X