有关springmvc3.1.3文件上传,出现400 的错误问题

使用SptingMVC3.1.3 对于文件上传提交的表单我们都会设置:enctype="multipart/form-data" 的一个设置, 那么值得注意的问题出现了, 对于表单post提交的数据无法绑定到后台的参数中, 紧接着服务器会向浏览器客户端抛HTTP STATUS 400 的错误, 或者出现 类型转换的的异常, 这个问题从开始一直困扰着我好些天,一直都不能解决这个问题,这个问题一直困扰自己好些天。 先贴出自己的错误的 服务器端会向Broswer返回 400 的bad request 的错误! 看看大家能否看出错误的地方?大家一起帮忙找找错误在哪里? 

贴以前的错误配置代码 :

web.xml

Web.xml代码:  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  7.   <display-name>springmvc</display-name>       
  8.   <filter>  
  9.     <filter-name>characterEncoding</filter-name>  
  10.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  11.   </filter>  
  12.   <filter-mapping>  
  13.     <filter-name>characterEncoding</filter-name>  
  14.     <url-pattern>/*</url-pattern>  
  15.   </filter-mapping>  
  16.   <servlet>  
  17.     <servlet-name>spring</servlet-name>  
  18.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  19.     <load-on-startup>1</load-on-startup>  
  20.   </servlet>  
  21.     
  22.   <servlet-mapping>  
  23.     <servlet-name>spring</servlet-name>  
  24.     <url-pattern>/spring/*</url-pattern>  
  25.   </servlet-mapping>  
  26.   <welcome-file-list>  
  27.     <welcome-file>index.jsp</welcome-file>  
  28.   </welcome-file-list>  
  29. </web-app>  
Spring-servlet.xml代码:  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xmlns:aop="http://www.springframework.org/schema/aop"  
  5.         xmlns:context="http://www.springframework.org/schema/context"  
  6.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  7.                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
  8.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  9.                 <!-- 启动配置包的扫描注解 -->  
  10.                 <context:component-scan base-package="com.springmvc.simples"/>  
  11.                 <!-- 配置springmvc的注解的配置的支持 -->  
  12.                 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>  
  13.                 <!-- 配置 文件上传的支持 -->  
  14.                 <bean  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  15.                     <property name="maxUploadSize" value="1024000000"/>  
  16.                     <property name="resolveLazily" value="true"/>  
  17.                     <property name="maxInMemorySize" value="4096"/>  
  18.                     <property name="defaultEncoding" value="UTF-8"/>  
  19.                 </bean>  
  20.                 <!-- 配置视图解析器 -->  
  21.                 <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">  
  22.                     <property name="basenames">  
  23.                         <list>  
  24.                             <value>views</value>  
  25.                         </list>  
  26.                     </property>  
  27.                     <property name="defaultParentView" value="default"/>  
  28.                 </bean>   
  29.                 </beans>  
Veiws.properties代码:  
  1. default.(class)=org.springframework.web.servlet.view.JstlView  
  2. default.url=/jsps/index.jsp  
Fileuploadactioncontrolller代码: 
  1. package com.springmvc.simples.controller;    
  2. import org.springframework.context.annotation.Scope;  
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestMethod;  
  6. import org.springframework.web.bind.annotation.RequestParam;  
  7. import org.springframework.web.multipart.MultipartFile;  
  8. /**  
  9.  * 文件上传: 单个文件上传  与多个文件的上传  
  10.  * @version 1.0  @author 高元东  
  11.  *@mailto 466862016@qq.com  
  12.  * 2013-1-21  
  13.  */  
  14. @Controller  
  15. @Scope("prototype")  
  16. @RequestMapping("/fileupload")  
  17. public class FileUploadActionController {    
  18.     @RequestMapping(value ="/uploadOne",method=RequestMethod.POST)  
  19.     public String fileUploadOne(  
  20.         @RequestParam("file")   MultipartFile file) {  
  21.     /*  if(!file.isEmpty()) {    
  22.             System.err.println("file name :" +file.getOriginalFilename());  
  23.         }*/  
  24.         System.err.println("okokoko");  
  25.         return "default";  
  26.     }     
  27. }  

Index.jsp代码: 

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.     <title>首页</title>  
  7.     <meta http-equiv="pragma" content="no-cache">  
  8.     <meta http-equiv="cache-control" content="no-cache">  
  9.     <meta http-equiv="expires" content="0">      
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11.     <meta http-equiv="description" content="This is my page">  
  12.     <!--  
  13.     <link rel="stylesheet" type="text/css" href="styles.css">  
  14.     -->  
  15.   </head>   
  16.   <body>   
  17.     <center>  
  18.         <a href="<c:url value='/spring/annotation/a'/>">springmvc注解的配置 无参数的测试</a>  <br>  
  19.         <a href="<c:url value='/spring/annotation/b?name=tom'/>">springmvc注解的配置 无参数为字符串的配置</a><br>  
  20.         <a href="<c:url value='/spring/annotation/JACK/c.info'/>">springmvc注解的配置 基于RESTFULL字符串的配置</a><br>  
  21.         <a href="<c:url value='/spring/annotation/24/JACK/d_5.html'/>">springmvc注解的配置 基于RESTFULL多参数的配置</a>  
  22.           
  23.         <hr>  
  24.         <h2>文件上传部分</h2>  
  25.           
  26.         <h3>单个文件上传.....</h3>  
  27.         <form action="<c:url value='/spring/fileupload/uploadOne'/>" method="post" enctype="multipart/form-data">  
  28.             <input type="file" name="file"><br>  
  29.             <input type="submit" >  
  30.         </form>  
  31.     </center>  
  32.   </body>  
  33. </html>  

  代码上传完毕!看看能否看出哪里出错了 ,去网上求解, 网上有大量文件上传的例子, 都看和自己的配置差不多,都是大同小异 , 自己实在是没有办法, 自己只有跟源码一步一步的进行调试 跟下去,加上log4j日志。 对应Springmvc的调试跟代码 跟源码那也要有一个入口点, 就像Struts2一样 ,的有一个核心过滤器 StrutsPreparedAndExecuteFilter ,而对应我们的springmvc的核心类  那就是DispatcherServlet 此类就是Springmvc的核心, 学springmvc我们都知道这个servlet是做什么用的,简单的来说它是处理一个请求,并提供映射和异常处理的功能! 我们看看此Servlet的大纲视图  看看 有什么猫咪 ? 晕?? 这怎么有个成员变量 》???


 

哦  它的是从 这个命名空间中的bean的工厂中获取的bean的名称: ??  不敢确定  ? 

看着像我们在xml中配置的那个bean  ????  难道  ??? 真是? 看看能否有什么set  方法吗???

 我们继续往下找, 单可以肯定在启动项目的时候会进行一系列的初始化我们在 xml配置的那些处理器  也就是我们的handler  Resolver 


 

一大片都是 resolver  


 

直接把它卡在源代码的456行   :: 



 

不看什么 看logger日志我们都知道 , 要查找名称为:multipartResolver 的bean  再看我们的代码:  娘的, 真的少加id=“multipartResolver ”了。


 

那肯定是我们配置的org.springframework.web.multipart.commons.CommonsMultipartResolver 根本没有起到作用, 一定要加上id=“multipartResolver ” 不然, 在你的项目进行启动的时候会找不到这样的bean的。 小小的错误,小小的失误 真的很烦人。

  输出的日志::


 

很明显  我们配置的org.springframework.web.multipart.commons.CommonsMultipartResolver  根本没有起到作用!! 对应别人的代码我们只能参考吗, 不能一味的去copy  不跟自己有空自己去看看源码,

正确的是::


 

 

 

 原文参考自站长网http://www.software8.co/wzjs/qtyy/2819.html

最后   要注意的是  : 在我们配置的bean中一定要加上 id ="multipartResolver"  不然你配置的也没有用的  。 不然你的表单设置成  multipart/form-data  是无法绑定参数的  会出现类型转换的异常!

在写的地方哪里有不清楚的地方,请见谅,我也是项目的需要 ,刚刚碰Springmvc 。把正确简单的demo 上传上去。

原文地址:https://www.cnblogs.com/wwwroot/p/2871560.html