接下来用很短的篇幅介绍下spring mvc 3.1文件上传
其实spring的官方文档关于这块介绍已经说的很清楚了,就不过多啰嗦了,简单的把官方文档复述一遍:
spring mvc 将fileupload进行了二次封装,主要为MultipartHttpServletRequest和MultipartFile 两个对象,
分别用语多文件和单文件上传.
准备工作:导入fileupload.jar ,commons-io.jar包
首先是对springMVC-servlet.xml进行配置,增加对文件上传的支持:
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.
CommonsMultipartResolver" p:maxUploadSize="-1"/>
里面有一些可选属性,例如:maxUploadSize(文件大小限制:byte,-1为不限制大小(默认值))具体就不列出了。
1.单文件上传:
controller:
@RequestMapping(value="/fileupload",method=RequestMethod.POST)
public String getFile(@RequestParam("file")MultipartFile file) throws IOException{
//do upload
return "index";
}
jsp:
<form action="user/fileupload.action" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
OK ,这样单文件就ok 了。接下来就是对获得的文件进行本地保存了,代码很简单,这里就不罗嗦了。
2.多文件上传:
controller:
@RequestMapping(value="/fileupload",method=RequestMethod.POST)
public String getFile(MultipartHttpServletRequest request) throws IOException{
Iterator<String> it = request.getFileNames();
while(it.hasNext()){
List<MultipartFile> files = request.getFiles(it.next());
for(MultipartFile file : files){
//do upload
//System.out.println(file.getOriginalFilename());
}
}
return "index";
}
jsp:
<form action="user/fileupload.action" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
个人建议不管多文件单文件,直接使用多文件上传(MultipartHttpServletRequest 参数),省的烦了。
ok了,上传就那么简单,动手试试吧!
分享到:
相关推荐
spring MVC 3.1+spring security3.1+mybaits+ 注解 实现安全机制。jar包没有上传。自己补充
Spring MVC 3.1演示 该项目包含展示样式的微型样本,这些样本演示了Spring MVC 3.1中可用的自定义和功能。 部署该应用程序,导航到主页localhost:8080 / spring-mvc-31-demo并按照此处列出的各种链接进行操作。 该...
一个简单SpringMVC例子,从请求到controller跳转再到页面显示能完整执行成功。使用myeclipse6.0搭建,使用Spring3.1部分jar包,使用jdk1.6.0_45和自带的myeclipse tomcat。
spring 3.1 mvc sample Spring 3.1的最新例子
这是一个以SpringMVC3.1 + Mybatis3.1为核心框架的Demo源码,本人经调试并测试通过,供广大初爱好J2EE编程者学习。
targetNamespace="http://www.springframework.org/schema/mvc
springMVC3.1 +mybatis3.1集成,包括事务,jqueryEasyUI分页,json,数据库连接池等。环境使用eclipse4.3+maven3.0.5+tomcat7
演示spring3.1 mvc一个入门的例子
web applications using version 3.1 of the Spring Framework. Topics include but are not limited to: • The building blocks of the Spring MVC components • Configuring your development environment • ...
Spring MVC and Spring Web Flow have been upgraded in the new Spring Framework 3.1 and are engineered with important considerations for design patterns and expert object-oriented programming techniques...
spring mvc3.2.7(基于注解配置)+servlet3.1(jetty)+maven3+eclipse
一个最简单的 Spring2.0mvc Hibernate3.1 登录实例,即看即会。 基于MYSQL数据库。 保证可用! 注意:因上传容量所限,dom4j-1.6.1.jar未放入工程,请自行加入该JAR包(其他版本也可),否则无法运行。
Spring MVC框架所需要的全部jar包。
NULL 博文链接:https://xly1981.iteye.com/blog/1897539
NULL 博文链接:https://qisehua.iteye.com/blog/1729015
一、Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar Hibernate 3.6.8:hibernate3.jar...
spring-aop-3.0.xsd 这个下载之后可以直接放在eclipese的xml的catalog中配置,已到达eclipse可以自动提示xml的功能
Spring MVC 学习总结(五)——校验与文件上传 目录 2.2.7、范围 2.2.8、其它注解 2.3、注解控制器参数 1.4、在UI中添加错误标签 1.5、测试运行 三、使用jQuery扩展插件Validate实现前端校验 3.1、jQuery扩展插件...
这是在Spring的官方网站上的实例代码,适合初学者想入门Spring MVC的朋友。这里有源代码,操作步骤的教程文档。算是较详细了。 不过要理解其内容你还必须学习Spring的一些基本知识,比如依赖注入,Annotation的基本...
Spring MVC and Spring Web Flow have been upgraded in the new Spring Framework 3.1 and are engineered with important considerations for design patterns and expert object-oriented programming techniques...