Spring MVC3.2 通过Servlet3.0实现文件上传
Servlet3.0规范增加了对文件上传的原生支持,这里记录一下Spring MVC3通过Servlet3上传文件的实现。
配置文件:
applicationContext.xml
<!-- spring mvc3+servlet3上传文件需要配置 --><beanid="multipartResolver"class="org.springframework.web.multipart.support.StandardServletMultipartResolver"></bean>
web.xml中需要配置multipart-config片段
<!-- spring mvc --><servlet><servlet-name>SpringMvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/spring/appContext.xml,/WEB-INF/config/spring/appInterceptor.xml</param-value></init-param><multipart-config><max-file-size>52428800</max-file-size><max-request-size>52428800</max-request-size><file-size-threshold>0</file-size-threshold></multipart-config><load-on-startup>1</load-on-startup></servlet>
update.jsp上传界面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=utf-8"><title>twovs.com测试servlet3+springmvc3.2上传文件</title></head><body><h1>Please upload a file</h1><formmethod="post"action="http://www.twovs.com/test/up.htm"enctype="multipart/form-data"><inputtype="text"name="name"value="1111"/><inputtype="file"name="file"/><inputtype="submit"/></form></body></html>
UpdateTest4Servlet.java Spring MVC Controller
/**
* $id: com.twovv.update.UpdateTest4Servlet.java by fjt 2013-2-15
*
* @version:0.1
*
* Copyright (c) 2013 twovv.com
*/package com.twovv.update;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;/** @Class: UpdateTest4Servlet @TODO: 测试上传 */@ControllerpublicclassUpdateTest4Servlet{/**
* @method: fileUpload() -by fjt
* @TODO: 文件上传
* @return String
*/@RequestMapping(value="/test/up",method=RequestMethod.POST)publicString fileUpload(@RequestParamString name,@RequestParamMultipartFile file){////你自己的存储逻辑//这里只打印一下接收到的信息System.out.println(name);/**
* file.getName()获取不到文件名,只能拿到一个“file”字符串
* Servlet3没有提供直接获取文件名的方法,可以从请求头中解析出来:
* file.getHeader("content-disposition")方法间接获取得到。
*/System.out.println(file.getName());System.out.println(file.getSize());return"";}}
简单的配置完成,现在可以通过访问http://www.twovs.com/upload.jsp来测试通过servlet3.0的新特性来上传文件了。
碰到的问题:
Spring @RequestParam绑定不到值(获取不到值)。
异常日志:
2013-02-1504:05:02,862[DEBUG]-[org.springframework.web.servlet.DispatcherServlet]DispatcherServletwith name 'SpringMvc' processing POST request for[/ueditor/test/up.htm]2013-02-1504:05:02,869[DEBUG]-[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]Looking up handler method for path /test/up.htm
2013-02-1504:05:02,879[DEBUG]-[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]Returning handler method [public java.lang.String com.twovv.update.UpdateTest4Servlet.fileUpload(java.lang.String,org.springframework.web.multipart.MultipartFile)]2013-02-1504:05:02,879[DEBUG]-[org.springframework.beans.factory.support.DefaultListableBeanFactory]Returning cached instance of singleton bean 'updateTest4Servlet'2013-02-1504:05:02,895[DEBUG]-[org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor]Opening JPA EntityManagerinOpenEntityManagerInViewInterceptor2013-02-1504:05:03,055[DEBUG]-[org.hibernate.internal.SessionImpl]Opened session at timestamp:136087230292013-02-1504:05:03,112[DEBUG]-[org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver]Resolving exception from handler [public java.lang.String com.twovv.update.UpdateTest4Servlet.fileUpload(java.lang.String,org.springframework.web.multipart.MultipartFile)]: org.springframework.web.bind.MissingServletRequestParameterException:RequiredString parameter 'name'isnot present
2013-02-1504:05:03,118[DEBUG]-[org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver]Resolving exception from handler [public java.lang.String com.twovv.update.UpdateTest4Servlet.fileUpload(java.lang.String,org.springframework.web.multipart.MultipartFile)]: org.springframework.web.bind.MissingServletRequestParameterException:RequiredString parameter 'name'isnot present
2013-02-1504:05:03,118[DEBUG]-[org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver]Resolving exception from handler [public java.lang.String com.twovv.update.UpdateTest4Servlet.fileUpload(java.lang.String,org.springframework.web.multipart.MultipartFile)]: org.springframework.web.bind.MissingServletRequestParameterException:RequiredString parameter 'name'isnot present
2013-02-1504:05:03,119[DEBUG]-[org.springframework.web.servlet.DispatcherServlet]NullModelAndView returned to DispatcherServletwith name 'SpringMvc': assuming HandlerAdapter completed request handling
2013-02-1504:05:03,119[DEBUG]-[org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor]Closing JPA EntityManagerinOpenEntityManagerInViewInterceptor2013-02-1504:05:03,148[DEBUG]-[org.springframework.orm.jpa.EntityManagerFactoryUtils]Closing JPA EntityManager2013-02-1504:05:03,155[DEBUG]-[org.springframework.web.servlet.DispatcherServlet]Successfully completed request
异常原因:根据Spring3的提示,缺少multipart-config配置
In order to useServlet3.0 based multipart parsing, you need to mark the DispatcherServletwith a "multipart-config" section in web.xml,orwith a javax.servlet.MultipartConfigElementin programmatic Servlet registration,orincase of a custom Servletclass possibly with a javax.servlet.annotation.MultipartConfig annotation on your Servletclass.Configuration settings such as maximum sizes or storage locations need to be applied at that Servlet registration level asServlet3.0 does not allow for those settings to be donefrom the MultipartResolver.
解决办法:
按照提示需要在web.xml中配置的DispatcherServlet中添加multipart-config片段。
MultipartConfig所有的属性都是可选的,具体属性如下:
- fileSizeThreshold int 当数据量大于该值时,内容将被写入文件。 
- location String 存放生成的文件地址。 
- maxFileSize long 允许上传的文件最大值。默认值为 -1,表示没有限制。 
- maxRequestSize long 针对该 multipart/form-data 请求的最大数量,默认值为 -1,表示没有限制。 
1、location属性,既是保存路径(在写入的时候,可以忽略路径设定),又是上传过程中临时文件的保存路径,一旦执行write方法之后,临时文件将被自动清除。
2、上传过程中无论是单个文件超过maxFileSize值,或者上传总的数据量大于maxRequestSize值都会抛出IllegalStateException异常
参考:
Servlet 3.0's FileUpload Sample
Using a MultipartResolver with Servlet 3.0 
JSR 315: JavaTM Servlet 3.0 Specification 
JSR-000315 JavaTM Servlet 3.0
转载自:http://www.sxrczx.com/t/article/a3fadb5bd6734a8891c4dad43510ca5e.htm
Spring MVC3.2 通过Servlet3.0实现文件上传的更多相关文章
- Servlet3.0学习总结——基于Servlet3.0的文件上传
		Servlet3.0学习总结(三)——基于Servlet3.0的文件上传 在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileu ... 
- Servlet3.0学习总结(三)——基于Servlet3.0的文件上传
		在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件,在Servlet3.0中提供了对文件上传的原生支持,我们不 ... 
- java-基于Servlet3.0的文件上传
		Servlet3.0学习总结(三)——基于Servlet3.0的文件上传 在Servlet3.0中使用request.getParts()获取上传文件 
- Servlet3.0之八:基于Servlet3.0的文件上传@MultipartConfig
		在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件,在Servlet3.0中提供了对文件上传的原生支持,我们不 ... 
- servlet3.0的文件上传代码配置怎么写
		之前学习过xml配置servlet3.0的文件上传,但是变成code方式一直不知道怎么弄,相比较起来apache的文件上传配置和xml倒是没什么太大区别. 直接上代码:无需依赖,只要一个方法就好了cu ... 
- Servlet3.0 multipart 文件上传技术
		Servlet3.0 javaConfig配置 传统的servlet都是在web.xml中配置,从Servlet 3.0开始提供了ServletContainerInitializer接口,允许使用代 ... 
- Servlet3.0的文件上传功能
		在Servlet3.0之前,文件上传需要借助于第三方插件,在Servlet3.0之后,Servlet本身开始支持文件上传功能. 获取上传的文件可以通过HTTPServletRequest的getPar ... 
- spring mvc 3.0 实现文件上传功能
		http://club.jledu.gov.cn/?uid-5282-action-viewspace-itemid-188672 —————————————————————————————————— ... 
- phpcms v9.6.0任意文件上传漏洞(CVE-2018-14399)
		phpcms v9.6.0任意文件上传漏洞(CVE-2018-14399) 一.漏洞描述 PHPCMS 9.6.0版本中的libs/classes/attachment.class.php文件存在漏洞 ... 
随机推荐
- 马士兵Spring-hibernate整合
			spring整合hibernate: 1.sessionFactory只需要一个就可以了,单例,适合spring管理: 2.HIbernate中的SessionFactory是接口:spring中实现 ... 
- 教你判断一个APP页面是原生的还是H5页面 。(还没看)
			来源:https://www.25xt.com/appdesign/11851.html 刚好是周末,无意之间学堂君在收集相关资料的时候,发现有部分童鞋在问<如何判断一个APP页面是不是H5页面 ... 
- nodejs+express+ejs+mongoose实例
			nodejs+express+ejs+mongoose实例 nodejs学得异常痛苦,在这里将学的东西做一番整理,算是自我安慰吧.根据网上todo示例,用express和mongoose重写了部分代码 ... 
- javascript中数组的强大用法·
			1 归并 var a = [{name: 'tom'},{name: 'aiscy'},{name: 'judy'},{name: 'mike'}];a.reduce(function(prev, i ... 
- oracle sqlplus conn 远程连接
- Pthreads n 体问题
			▶ <并行程序设计导论>第六章中讨论了 n 体问题,分别使用了 MPI,Pthreads,OpenMP 来进行实现,这里是 Pthreads 的代码,分为基本算法和简化算法(引力计算量为基 ... 
- win8 机器硬盘异响
			win8系统安装在ssd上,挂了一块希捷2T的机器硬盘做数据存储用,经常发生异响.类似通电断电的声音.因为硬盘的APM节能使磁头归位造成的声音. 使用CrystalDiskInfo软件禁用APM,异响 ... 
- VB指针操作和消息钩子
			二.VB怎么用指针 要想弄明白VB怎么使用指针,就必须要弄明白两件事,第一,如何取得数组的指针,第二,如何将指针所指向的数组取出来. A.在讲解这两个问题之前,我们需要了解几个 ... 
- Linux TCP/IP 连接查看和问题解决
			netstat -nat|awk '{print awk $NF}'|sort|uniq -c|sort -n 上面的 命令可以帮助分析哪种Tcp状态数量异常 netstat -nat|gr ... 
- Git----远程仓库01
			到目前为止,我们已经掌握了如何在Git仓库里对一个文件进行时光穿梭,你再也不用担心文件备份或者丢失的问题了 可是用过集中式版本控制系统SVN的童鞋们会站出来说,这些功能在SVN里早就有了,没看出Git ... 
