前端码农
2015-04-13 16:40:36
原
Spring MVC实现的带参数的form表单多文件上传
(由于字数限制请看项目里代码,下面省略部分不重要的代码)
首先是web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <servlet> <servlet-name>upload</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>upload</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>SpringCharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>/WEB-INF/jsp/user/add.jsp</welcome-file> </welcome-file-list> </web-app>
接下来是SpringMVC的配置文件upload-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:mvc="http://www.springframework.org/schema/mvc" 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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.jadyer"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> <property name="maxUploadSize" value="200000"/> </bean> <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException --> <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 --> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop> </props> </property> </bean> </beans>
下面是用于上传的表单页面//WEB-INF//jsp//user//add.jsp
(由于字数限制请看项目里代码)
下面是上传成功后打印用户信息的页面//WEB-INF//jsp//user//list.jsp
(由于字数限制请看项目里代码)
下面是上传文件内容过大时的提示页面//WEB-INF//jsp//error_fileupload.jsp
(由于字数限制请看项目里代码)
接下来是用到的实体类User.java
public class User { private String username; private String nickname; private String password; private String email; //{此处get.set省略} public User() { } public User(String username, String nickname, String password, String email) { this.username = username; this.nickname = nickname; this.password = password; this.email = email; } }
最后是核心的UserController.java
@Controller @RequestMapping("/user") public class UserController { private final static Map<String, User> users = new HashMap<String, User>(); // 模拟数据源,构造初始数据 public UserController() { users.put("张起灵", new User("张起灵", "闷油瓶", "02200059", "menyouping@yeah.net")); users.put("李寻欢", new User("李寻欢", "李探花", "08866659", "lixunhuan@gulong.cn")); users.put("拓拔野", new User("拓拔野", "搜神记", "05577759", "tuobaye@manhuang.cc")); users.put("孙悟空", new User("孙悟空", "美猴王", "03311159", "sunhouzi@xiyouji.zh")); } @RequestMapping("/list") public String list(Model model) { model.addAttribute("users", users); return "user/list"; } @RequestMapping(value = "/add", method = RequestMethod.GET) public String addUser() { return "user/add"; } @RequestMapping(value = "/add", method = RequestMethod.POST) public String addUser(User user, @RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException { // 如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解 // 如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解 // 并且上传多个文件时,前台表单中的所有<input // type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件 for (MultipartFile myfile : myfiles) { // if (myfile.isEmpty()) { // System.out.println("文件未上传"); // } else { System.out.println("文件长度: " + myfile.getSize()); System.out.println("文件类型: " + myfile.getContentType()); System.out.println("文件名称: " + myfile.getName()); System.out.println("文件原名: " + myfile.getOriginalFilename()); System.out.println("========================================"); // 如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中 // 这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的 // String realPath = // request.getSession().getServletContext().getRealPath("/FileTest/WebContent/WEB-INF/upload"); String realPath = request.getSession().getServletContext() .getRealPath("/WEB-INF/upload"); System.out.println(realPath); FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File( realPath, myfile.getOriginalFilename())); // } } users.put(user.getUsername(), user); return "redirect:/user/list"; } }
补充:记得建立这个目录,用于存放上传的文件,即//WEB-INF//upload//
下面是程序运行截图:
添加成功后:
猜你喜欢
- spring mvc+bootstrap实现文件的上传实例
- spring mvc实现文件上传功能
- spring mvc文件上传与下载实例
- spring mvc+easyui开发文件上传下载实例demo源码下载
- Spring MVC+ajax+base64+amazeui框架上传头像带裁剪功能
- spring mvc+Maven+ZUI可拖拽多个文件上传和下载实例
- 基于maven与springMVC的拦截器Interceptor,控制器Controller的使用
- Spring Mvc初学者专用,里面有4套案例!
- Spring MVC多视图配置简单demo实例,支持freemarker、velocity、jsp视图
- spring3.0 mvc和rest风格的小例子配置demo代码教程
- spring mvc注解代码实例教程
- SpringMVC的三种统一异常处理实例代码分享
请下载代码后再发表评论
文件名:WjwTest.zip,文件大小:7278.546K
下载
- /
- /WjwTest
- /WjwTest/.classpath
- /WjwTest/.project
- /WjwTest/.settings
- /WjwTest/.settings/.jsdtscope
- /WjwTest/.settings/org.eclipse.core.resources.prefs
- /WjwTest/.settings/org.eclipse.jdt.core.prefs
- /WjwTest/.settings/org.eclipse.wst.common.component
- /WjwTest/.settings/org.eclipse.wst.common.project.facet.core.xml
- /WjwTest/.settings/org.eclipse.wst.jsdt.ui.superType.container
- /WjwTest/src
- /WjwTest/src/com
- /WjwTest/src/com/jadyer
- /WjwTest/src/com/jadyer/controller
- /WjwTest/src/com/jadyer/model
- /WjwTest/src/com/jadyer
- /WjwTest/src/com
- /WjwTest

- 证精 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- 原 基于maven与springMVC的拦截器Interceptor,控制器Controller的使用
- spring mvc 学习使用模板 基础教程
- spring mvc实现文件上传功能
- 原证 Spring Mvc初学者专用,里面有4套案例!
- 原证 Spring MVC多视图配置简单demo实例,支持freemarker、velocity、jsp视图
- spring mvc注解代码实例教程
- 原证精 spring mvc如何将form表单中的对象类型绑定
- 精 SpringMVC的三种统一异常处理实例代码分享
- spring3.0 mvc和rest风格的小例子配置demo代码教程
- 原 Spring MVC+apache Shiro框架搭建,基于maven构建
- 待 Spring学习笔记之Spring MVC 入门教程

hongyan317 LV11
2020年9月20日
gy964781920 LV5
2020年5月16日
solider12 LV8
2020年3月15日
13940562934 LV22
2019年11月13日
yaoxingxing1 LV13
2019年5月17日
zhh1355 LV14
2019年5月10日
hwqhwq LV20
2019年4月3日
zlongj2015 LV3
2018年11月20日
大鹏小镇 LV14
2018年7月14日
黄丫头 LV10
2018年7月2日

1358849392 LV21
2024年4月12日
zhuohanyuan LV10
2022年9月25日
yymmdm LV6
2022年9月5日
小翊杭宝 LV2
2022年6月21日
czr2233 LV9
2021年9月14日
1585596474
2021年8月28日
暂无贡献等级
不喝冰阔落
2021年7月18日
暂无贡献等级
cxcxcxcxcx123132
2021年4月2日
暂无贡献等级
zlstly LV7
2021年3月15日
peterliu LV3
2020年12月22日