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

贴以前的错误配置代码 :

web.xml

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

  • <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  • <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>  
  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  • <html>  
  •   <head>  
  •     <title>首页</title>  
  •     <meta http-equiv="pragma" content="no-cache">  
  •     <meta http-equiv="cache-control" content="no-cache">  
  •     <meta http-equiv="expires" content="0">      
  •     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  •     <meta http-equiv="description" content="This is my page">  
  •     <!--  
  •     <link rel="stylesheet" type="text/css" href="styles.css">  
  •     -->  
  •   </head>   
  •   <body>   
  •     <center>  
  •         <a href="<c:url value='/spring/annotation/a'/>">springmvc注解的配置 无参数的测试</a>  <br>  
  •         <a href="<c:url value='/spring/annotation/b?name=tom'/>">springmvc注解的配置 无参数为字符串的配置</a><br>  
  •         <a href="<c:url value='/spring/annotation/JACK/c.info'/>">springmvc注解的配置 基于RESTFULL字符串的配置</a><br>  
  •         <a href="<c:url value='/spring/annotation/24/JACK/d_5.html'/>">springmvc注解的配置 基于RESTFULL多参数的配置</a>  
  •           
  •         <hr>  
  •         <h2>文件上传部分</h2>  
  •           
  •         <h3>单个文件上传.....</h3>  
  •         <form action="<c:url value='/spring/fileupload/uploadOne'/>" method="post" enctype="multipart/form-data">  
  •             <input type="file" name="file"><br>  
  •             <input type="submit" >  
  •         </form>  
  •     </center>  
  •   </body>  
  • </html>  
  •   代码上传完毕!看看能否看出哪里出错了 ,去网上求解, 网上有大量文件上传的例子, 都看和自己的配置差不多,都是大同小异 , 自己实在是没有办法, 自己只有跟源码一步一步的进行调试 跟下去,加上log4j日志。 对应Springmvc的调试跟代码 跟源码那也要有一个入口点, 就像Struts2一样 ,的有一个核心过滤器 StrutsPreparedAndExecuteFilter ,而对应我们的springmvc的核心类  那就是DispatcherServlet 此类就是Springmvc的核心, 学springmvc我们都知道这个servlet是做什么用的,简单的来说它是处理一个请求,并提供映射和异常处理的功能! 我们看看此Servlet的大纲视图  看看 有什么猫咪 ? 晕?? 这怎么有个成员变量 》???

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

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

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

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

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

    一大片都是 resolver  

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

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


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

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

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

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

      输出的日志::

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

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

    正确的是::

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

     
    有关springmvc3.1.3文件上传,出现400 的错误问题
     
    有关springmvc3.1.3文件上传,出现400 的错误问题
     原文参考自站长网http://www.software8.co/wzjs/qtyy/2819.html

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

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

    相关文章:

    • 2021-11-13
    • 2022-12-23
    • 2021-07-15
    • 2021-08-12
    • 2021-10-14
    • 2021-09-12
    猜你喜欢
    • 2021-06-04
    • 2022-12-23
    • 2022-12-23
    • 2022-12-23
    • 2021-08-27
    • 2021-05-10
    • 2022-12-23
    相关资源
    相似解决方案