以下示例显示如何在使用Spring Web MVC框架的表单中上传文件和处理。首先使用Eclipse IDE来创建一个WEB工程,实现一个上传文件并保存的功能。并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:

  1. 创建一个名称为 FileUpload 的动态WEB项目。
  2. 在 com.yiibai.springmvc 包下创建两个Java类FileModelFileUploadController
  3. jsp子文件夹下创建两个视图文件:fileUpload.jsp 和 success.jsp
  4. WebContent文件夹下创建一个文件夹:temp
  5. 下载Apache Commons FileUpload库:commons-fileupload.jarApache Commons IO库:commons-io.jar。把它们放在CLASSPATH中。
  6. 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。

完整的项目文件目录结构如下所示 -

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;
}
}
Java

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";
}
}
}
Java

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>
XML

这里的第一个服务方法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>
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>
HTML

完成创建源和配置文件后,发布应用程序到Tomcat服务器。

现在启动Tomcat服务器,现在尝试访问URL => http://localhost:8080/FileUpload/fileUploadPage ,如果Spring Web应用程序没有问题,应该看到以下结果:

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

Spring MVC文件上传处理的更多相关文章

  1. Spring MVC 笔记 —— Spring MVC 文件上传

    文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...

  2. Spring MVC文件上传教程 commons-io/commons-uploadfile

    Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...

  3. 【Java Web开发学习】Spring MVC文件上传

    [Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...

  4. Spring mvc文件上传实现

    Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...

  5. Spring mvc 文件上传到文件夹(转载+心得)

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  6. spring mvc 文件上传 ajax 异步上传

    异常代码: 1.the request doesn't contain a multipart/form-data or multipart/mixed stream, content type he ...

  7. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  8. Strut2 和Spring MVC 文件上传对比

    在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...

  9. Spring MVC 文件上传 & 文件下载

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...

  10. 【Spring】Spring MVC文件上传--整合bootstrap-fileinput和jQuery-File-Upload

    前言 这里分享两个使用Spring MVC进行文件上传的简单示例, 分别整合bootstrap-fileinput 和 Jquery File Upload , 代码十分简单, 都是入门的示例,因此这 ...

随机推荐

  1. jquery--动态篇

    jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元素的st ...

  2. 【sql】关联查询+表自关联查询

    表: 经销商 dealer   字段 uid  parent_uid  name 联系人 contact  字段 uid  dealer_id  contact_main 需求: 想要查询到经销商的信 ...

  3. linux基础-第十八单元_nginx部署

    一.基本环境配置 1.1.安装常用软件 yum install wget -y 1.2.Install yum repo mv /etc/yum.repos.d/CentOS-Base.repo /e ...

  4. nav标签使用说明

    一.html nav标签语法与结构   -   TOP 1.基本语法 <nav>内容</nav> 2.nav加id <nav id=”abc”>内容</nav ...

  5. pytz 格式化北京时间 6分钟问题

    使用datetime直接构造时间的时候,设置时区是没有北京时间的,一般来说习惯了linux的同志都会默认用上海时间来代替,这里却有一个问题,如果要进行时区转换,上海时间比北京时间差6分钟... 比如: ...

  6. RAID详解[RAID0/RAID1/RAID10/RAID5] (转)

    一.RAID定义RAID(Redundant Array of Independent Disk 独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘 ...

  7. 【Android Studio探索之路系列】之中的一个:Android Studio开篇

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.co ...

  8. 理解shell中的atime,mtime,ctime

    所有文件都有3个时间信息,保存在文件系统中 atime (Access time)是文件最后一此读的时间 或者执行文件的时间 mtime (Modified time)是文件最后一次写的时间(是在写入 ...

  9. springMVC4(5)RestTemplate控制层单元測试

    在前面我们进行web測试,总要在游览器进行.数据组装.请求方法更给等都极为麻烦. RestTemplate是Spring提供的一个web层測试模板类,我们能够通过RestTemplate在client ...

  10. C语言-srand种子详解

    rand() 函数取得随机数的时候是通过一个叫做"种子"的变量经过计算得出一个数值, 然后得出的数值再作为新的"种子"参与下一次的运算, 这样就得到了所谓的随机 ...