Spring MVC文件上传处理
以下示例显示如何在使用Spring Web MVC框架的表单中上传文件和处理。首先使用Eclipse IDE来创建一个WEB工程,实现一个上传文件并保存的功能。并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:
- 创建一个名称为 FileUpload 的动态WEB项目。
- 在
com.yiibai.springmvc包下创建两个Java类FileModel,FileUploadController。 - 在
jsp子文件夹下创建两个视图文件:fileUpload.jsp和success.jsp。 - 在
WebContent文件夹下创建一个文件夹:temp。 - 下载Apache Commons FileUpload库:commons-fileupload.jar和Apache Commons IO库:commons-io.jar。把它们放在
CLASSPATH中。 - 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。
完整的项目文件目录结构如下所示 -

FileModel.java 的代码如下所示 -
package com.yiibai.springmvc;
import org.springframework.web.multipart.MultipartFile;
public class FileModel {
private MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
FileUploadController.java 的代码如下所示 -
package com.yiibai.springmvc;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class FileUploadController {
@Autowired
ServletContext context;
@RequestMapping(value = "/fileUploadPage", method = RequestMethod.GET)
public ModelAndView fileUploadPage() {
FileModel file = new FileModel();
ModelAndView modelAndView = new ModelAndView("fileUpload", "command", file);
return modelAndView;
}
@RequestMapping(value="/fileUploadPage", method = RequestMethod.POST)
public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model) throws IOException {
if (result.hasErrors()) {
System.out.println("validation errors");
return "fileUploadPage";
} else {
System.out.println("Fetching file");
MultipartFile multipartFile = file.getFile();
String uploadPath = context.getRealPath("") + File.separator + "temp" + File.separator;
//Now do something with file...
FileCopyUtils.copy(file.getFile().getBytes(), new File(uploadPath+file.getFile().getOriginalFilename()));
String fileName = multipartFile.getOriginalFilename();
model.addAttribute("fileName", fileName);
return "success";
}
}
}
FileUpload-servlet.xml 的代码如下所示 -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.yiibai.springmvc" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
这里的第一个服务方法fileUploadPage(),我们已经在名为“command”的ModelAndView对象中传递了一个空的FileModel对象,因为如果JSP文件中使用<form:form>标签,spring框架期望一个名称为“command”的对象。 因此,当调用fileUploadPage()方法时,它返回fileUpload.jsp视图。
第二个服务方法fileUpload()将根据 URL => FileUpload/fileUploadPage上的POST方法进行调用。将根据提交的信息准备要上传的文件。最后从服务方法返回“success”视图,这将呈现success.jsp视图。
fileUpload.jsp 的代码如下所示 -
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Spring MVC上传文件示例</title>
</head>
<body>
<form:form method="POST" modelAttribute="fileUpload"
enctype="multipart/form-data">
请选择一个文件上传 :
<input type="file" name="file" />
<input type="submit" value="提交上传" />
</form:form>
</body>
</html>
这里使用带有value =“fileUpload”的modelAttribute属性来映射文件用服务器模型上传控件。
success.jsp 的代码如下所示 -
<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>Spring MVC上传文件示例</title>
</head>
<body>
文件名称 :
<b> ${fileName} </b> - 上传成功!
</body>
</html>
完成创建源和配置文件后,发布应用程序到Tomcat服务器。
现在启动Tomcat服务器,现在尝试访问URL => http://localhost:8080/FileUpload/fileUploadPage ,如果Spring Web应用程序没有问题,应该看到以下结果:

提交所需信息后,点击提交按钮提交表单。 如果 Spring Web 应用程序没有问题,应该看到以下结果:

Spring MVC文件上传处理的更多相关文章
- Spring MVC 笔记 —— Spring MVC 文件上传
文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...
- Spring MVC文件上传教程 commons-io/commons-uploadfile
Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...
- 【Java Web开发学习】Spring MVC文件上传
[Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...
- Spring mvc文件上传实现
Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...
- Spring mvc 文件上传到文件夹(转载+心得)
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- spring mvc 文件上传 ajax 异步上传
异常代码: 1.the request doesn't contain a multipart/form-data or multipart/mixed stream, content type he ...
- spring mvc文件上传(单个文件上传|多个文件上传)
单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包 1.所需jar包: commons-fileupload-1.3.1.jar ...
- Strut2 和Spring MVC 文件上传对比
在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...
- Spring MVC 文件上传 & 文件下载
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...
- 【Spring】Spring MVC文件上传--整合bootstrap-fileinput和jQuery-File-Upload
前言 这里分享两个使用Spring MVC进行文件上传的简单示例, 分别整合bootstrap-fileinput 和 Jquery File Upload , 代码十分简单, 都是入门的示例,因此这 ...
随机推荐
- 【mybatis】mybatis使用java实体中定义的常量,或静态方法
mybatis使用java实体中定义的常量 示例代码: <select id="findDealerInfo" parameterType="com.pisen.c ...
- ios(iphone/ipad)一个简单的用代码判断当前设备的方法
直接NSLog(@"current_device:%@",[UIDevice currentDevice].model); 即可看出它输出的是当前设备,所以根据这个字符串可简单的判 ...
- NET PROVIDER 连接 Oracle数据库
NET 数据库连接 ORacle http://www.devart.com/ DataDirect http://www.datadirect.com/ Oracle免客户端For . ...
- [shell 编程] if [ $# -eq 0 ]该语句是什么含义?
$0: shell或shell脚本的名字$*:以一对双引号给出参数列表$@:将各个参数分别加双引号返回$#:参数的个数$_:代表上一个命令的最后一个参数$$:代表所在命令的PID$!:代表最后执行的后 ...
- unity 部分obj不接受后处理
考虑了很多方案,比如渲染次序和mask(stencilebuffer) 渲染次序 sorting order(深度) renderer都有的属性能开放出来,sprite renderer原本就开放在i ...
- npm模块安装机制
npm 是 Node 的模块管理器,功能极其强大.它是 Node 获得成功的重要原因之一.正因为有了npm,我们只要一行命令:npm install,就能安装别人写好的模块 . 一.从 npm ins ...
- 【Docker安全】关于Docker使用root与非root用户的场景中的容器与host中的执行用户的研究
参考: http://blog.csdn.net/yygydjkthh/article/details/47694929
- pig笔记
1.安装Pig 将pig添加到环境变量当中 2.pig使用 首先将数据库中的数据导入到HDFS上 sqoop import --connect jdbc:mysql://192.168.1.10:33 ...
- [Javascript] Use a Pure RNG with the State ADT to Select an Element from State
The resultant in a State ADT instance can be used as a means of communication between different stat ...
- nmap小技巧[1] 探测大网络空间中的存活主机
url: nmap是所有安全爱好者应该熟练掌握的扫描工具,本篇介绍其在扫描大网络空间时的用法. 为什么要扫描大网络空间呢? 有这样的情形: 内网渗透 攻击者单点突破,进入内网后,需进一步扩大成果, ...