使用 Spring MVC 和 Html 上传图片

Posted

技术标签:

【中文标题】使用 Spring MVC 和 Html 上传图片【英文标题】:Image Upload Using Spring MVC and Html 【发布时间】:2012-12-02 08:39:22 【问题描述】:

当我使用我的 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
... 

我的调度器 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>

控制器类是

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());


而我的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> 

【问题讨论】:

【参考方案1】:

试试这个:

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

【讨论】:

【参考方案2】:

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

Spring MVC文件上传工作教程请参考this tutorial

【讨论】:

我尝试在 UplodItem 命令对象中更改名称,但问题仍然存在,还有什么我需要查看的【参考方案3】:

本教程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>

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

【讨论】:

以上是关于使用 Spring MVC 和 Html 上传图片的主要内容,如果未能解决你的问题,请参考以下文章

在spring mvc中上传图片

Spring MVC 图片的上传和下载

如何实现Spring MVC 图片上传时,图片如何代替进度条?

将spring mvc中的图像永久上传到其他位置

Spring MVC文件上传

ueditor采用Spring MVC的方式整合七牛云实现上传图片视频附件等