Spring MVC单文件上传(附带实例)
创建应用 springMVCDemo11,将 Spring MVC 相关的 JAR 包、commons-fileupload 组件相关的 JAR 包以及 JSTL 相关的 JAR 包导入应用的 lib 目录中,如图 1 所示。
图 1 springMVCDemo11 2)创建 web.xml 文件
在 WEB-INF 目录下创建 web.xml 文件。为防止中文乱码,需要在 web.xml 文件中添加字符编码过滤器,这里不再赘述。
3)创建文件选择页面在 WebContent 目录下创建 JSP 页面 oneFile.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 meta http-equiv= Content-Type content= text/html; charset=UTF-8 title Insert title here /title /head body form action= ${pageContext.request.contextPath }/onefile method= post enctype= multipart/form-data 选择文件: input type= file name= myfile br 文件描述: input type= text name= description br input type= submit value= 提交 /form /body /html4)创建 POJO 类
在 src 目录下创建 pojo 包,在该包中创建 POJO 类 FileDomain。然后在该 POJO 类中声明一个 MultipartFile 类型的属性封装被上传的文件信息,属性名与文件选择页面 oneFile.jsp 中的 file 类型的表单参数名 myfile 相同。具体代码如下:
package pojo; import org.springframework.web.multipart.MultipartFile; public class FileDomain { private String description; private MultipartFile myfile; public String getDescription() { return description; public void setDescription(String description) { this.description = description; public MultipartFile getMyfile() { return myfile; public void setMyfile(MultipartFile myfile) { this.myfile = myfile; }5)创建控制器类
在 src 目录下创建 controller 包,并在该包中创建 FileUploadController 控制器类。具体代码如下:
package controller; import java.io.File; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import pojo.FileDomain; @Controller public class FileUploadController { // 得到一个用来记录日志的对象,这样在打印信息时能够标记打印的是哪个类的信息 private static final Log logger = LogFactory.getLog(FileUploadController.class); * 单文件上传 @RequestMapping( /onefile ) public String oneFileUpload(@ModelAttribute FileDomain fileDomain, HttpServletRequest request) { * 文件上传到服务器的位置 /uploadfiles ,该位置是指 * workspace/.metadata/.plugins/org.eclipse * .wst.server.core/tmp0/wtpwebapps, 发布后使用 String realpath = request.getServletContext() .getRealPath( uploadfiles String fileName = fileDomain.getMyfile().getOriginalFilename(); File targetFile = new File(realpath, fileName); if (!targetFile.exists()) { targetFile.mkdirs(); // 上传 try { fileDomain.getMyfile().transferTo(targetFile); logger.info( 成功 } catch (Exception e) { e.printStackTrace(); return showOne }6)创建 Spring MVC 的配置文件
在上传文件时需要在配置文件中使用 Spring 的 CommonsMultipartResolver 类配置 MultipartResolver 用于文件上传,应用的配置文件 springmvc-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:p= http://www.springframework.org/schema/p xmlns:context= http://www.springframework.org/schema/context xsi:schemaLocation= http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd !-- 使用扫描机制扫描包 -- context:component-scan base-package= controller / !-- 完成视图的对应 -- !-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -- bean 7)创建成功显示页面在 WEB-INF 目录下创建 JSP 文件夹,并在该文件夹中创建单文件上传成功显示页面 showOne.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 meta http-equiv= Content-Type content= text/html; charset=UTF-8 title Insert title here /title /head body ${fileDomain.description } !-- fileDomain.getMyFile().getOriginalFilename()-- ${fileDomain.myfile.originalFilename } /body /html8)测试文件上传发布 springMVCDemo11 应用到 Tomcat 服务器并启动 Tomcat 服务器,然后通过地址 http://localhost:8080/springMVCDemo11/oneFile.jsp 运行文件选择页面,运行结果如图 2 所示。
图 2 单文件选择页面在图 2 中选择文件并输入文件描述,然后单击 提交 按钮上传文件,若成功则显示如图 3 所示的结果。
图 3 单文件成功上传结果22596.html
htmljavaJSPServletspringSpringMVCSTLWeb
相关文章
- Spring学习笔记(三)——Spring注解开发&spring与Junit整合测试
- Spring学习笔记(五)——JdbcTemplate和spring中声明式事务
- Spring学习笔记(十八)——spring日志框架的配置和使用
- Spring+MyBatis实例详解「建议收藏」
- Spring Framework 简介
- 利用 Spring Boot Admin 对 Spring Boot 应用监控以及配置认证
- Spring启动过程中实例化前部分的分析
- Spring启动过程中实例化部分代码的分析(Bean的推断构造方法)
- Spring(3)bean 注入-构造方法注入 那么又为什么需要依赖注入呢?
- Spring循环依赖-spring源码详解(四)
- aop事务管理-Spring事务实现原理
- Spring:AOP
- Spring Cloud Security的核心组件-Cloud Security Filter示例
- (一)spring cloud架构整合-springcloud简介详解架构师
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例详解编程语言
- Spring Boot(三):logback打印日志详解编程语言
- Spring Boot(十三):spring boot小技巧详解编程语言
- Spring Security form login 权限认证模式的实例详解编程语言
- Spring MVC类型转换的意义
- Spring MVC验证器应用实例(超详细)
- Spring MVC使用SessionLocaleResolver实现用户自定义切换语言实例
- Spring框架致力于搭建基于MSSQL的稳健开发体系(spring mssql)