java 使用 Spring MVC 和 Html 上传图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13877089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:17:48  来源:igfitidea点击:

Image Upload Using Spring MVC and Html

javahtmlspringspring-mvcillegalargumentexception

提问by ragemusker

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.

当我使用 spring mvc 代码时,我不断收到此错误我正在尝试使用 spring mvc 进行图像上传,我缺少什么参数。

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

我的调度程序 servlet 是

<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

我的 html 页面是

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

采纳答案by Viral Patel

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?

在定义时<input type="file">您已经指定了名称name="UploadFile"。而在您的 UploadItem 命令对象中,文件属性是file(从 item.getFile() 猜测)。您确定正确映射文件名吗?

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

有关 Spring MVC 文件上传的工作教程,请参阅本教程

回答by Amith

Try this:

试试这个:

protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="UploadFile") MultipartFile image, BindException errors,HttpSession session)

回答by phnix

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

本教程http://www.journaldev.com/2573/spring-mvc-file-upload-example-tutorial-single-and-multiple-files也适用于我。这很简单。重要的是当你使用

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

,不要忘记将 commons-fileupload 添加到您的 pom 依赖项中。