4

I keep getting this error when i do with my spring mvc code i am trying to do image upload using spring mvc what is the arguments I am missing.

org.springframework.web.util.NestedServletException: Request processing failed; 
    nested exception is java.lang.IllegalArgumentException: argument type mismatch
java.lang.IllegalArgumentException: argument type mismatch
... 

My dispatcher servlet is

<context:component-scan base-package="com.ImageUploadSpring.Controller" />

<!-- <bean id="simpleHandler" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> -->
     <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />  


    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
           <props>              
                <prop key="/Upload.html">FileUpload</prop>                  
            </props>
        </property>
    </bean> 
      <bean id="FileUpload" class="com.ImageUploadSpring.Controller.FileUpload">
        <property name="commandName" value="ImageUpload"/>  
         <property name="commandClass" value="com.ImageUploadSpring.Bean.UploadItem"/>
         <property name="formView" value="ImageUpload"/>
        <property name="successView" value="message"/> 
    </bean>
 <bean id="FileUpload" class="com.ImageUploadSpring.Controller.FileUpload"></bean>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".html" />
    </bean>

controller class is

public class FileUpload extends SimpleFormController{

 @RequestMapping(value = "/Upload.html", method = RequestMethod.POST)
protected ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors,HttpSession session)  {
    System.out.println("inside submit method");
    try{

    UploadItem item=(UploadItem)command;



    MultipartFile file = item.getFile();

    InputStream inputStream = null;
    OutputStream outputStream = null;
    if (file.getSize() > 0) {
        inputStream = file.getInputStream();

        outputStream = new FileOutputStream("D:/UploadedFiles/Images/"
                + file.getOriginalFilename());

        System.out.println(file.getOriginalFilename());

        int readBytes = 0;
        byte[] buffer = new byte[8192];
        while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {

            outputStream.write(buffer, 0, readBytes);
        }
        outputStream.close();
        inputStream.close();
        session.setAttribute("uploadFile", "D:/UploadedFiles/Images/"
                + file.getOriginalFilename());
    }       


    }catch (Exception e) {
        e.printStackTrace();
    }   

    return new ModelAndView("message");

}
@Override
 protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
 throws ServletException {

     binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());

}

And my html page is

<form name="ImageUpload" action="/ImageUploadSpring/service/Upload.html" method="POST" enctype="multipart/form-data">
<div>
Select images:  
<input type="text" id="box"/> 
<input type="file" id="UploadFile" name="UploadFile"  onchange="CopyMe(this,'box');" accept="image/*" size="40" style="width: 91px;" multiple />
<br><br>  
<input type="submit" value="Upload" /><br><br>
</div>
</form> 

3 Answers 3

1

Try this:

protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="UploadFile") MultipartFile image, BindException errors,HttpSession session)
Sign up to request clarification or add additional context in comments.

Comments

0

While defining <input type="file"> you have specified the name name="UploadFile". Whereas in your UploadItem command object the file attribute is file (guessing from item.getFile()). Are you sure you are correctly mapping the filename?

Please refer to this tutorial for working tutorial on Spring MVC File Upload

1 Comment

i tried changing the name in the UplodItem Command Object ,but still the problem persists ,is there anything else i need to look to
0

this tutorial http://www.journaldev.com/2573/spring-mvc-file-upload-example-tutorial-single-and-multiple-files also works well for me. it is quite simple. the important is when you use

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

, don't forget to add commons-fileupload to your pom dependencies.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.