MultipartResolver实现文件上传功能
作者:紫_色 发布时间:2021-06-19 22:38:15
标签:MultipartResolver,文件上传
springMVC默认的解析器里面是没有加入对文件上传的解析的,,使用springmvc对文件上传的解析器来处理文件上传的时需要用springmvc提供的MultipartResolver的申明,又因为CommonsMultipartResolver实现了MultipartResolver接口,所以我们可以在springmvc配置文件中这样配置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>
首先引入文件上传所需要的包,commons-logging-*.jar commons-io-*.jar commons-fileupload-*.jar
新建一个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>文件上传</title>
</head>
<body>
<%--<form action="user/fileUpload" method="post" enctype="multipart/form-data">--%>
<form action="user/fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="fileUpload" />
<input type="submit" value="上传" />
</form>
</body>
</html>
springmvc上传文件的形式有很多,这里我介绍两种.
第一种,看Controller
package gd.hz.springmvc.controller;
import java.io.File;
import java.io.IOException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller("userController")
@RequestMapping("user")
public class UserController {
// 处理文件上传一
@RequestMapping(value = "fileUpload", method = RequestMethod.POST)
public ModelAndView fileUpload(
@RequestParam("fileUpload") CommonsMultipartFile file) {
// 获取文件类型
System.out.println(file.getContentType());
// 获取文件大小
System.out.println(file.getSize());
// 获取文件名称
System.out.println(file.getOriginalFilename());
// 判断文件是否存在
if (!file.isEmpty()) {
String path = "D:/" + file.getOriginalFilename();
File localFile = new File(path);
try {
file.transferTo(localFile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return new ModelAndView("dataSuccess");
}
}
类CommonsMultipartFile为我们提供了许多对文件处理的方法.例如文件大小,上传文件名称,文件类型,具体用法可以查看spring的文档.transferTo就是将文件输出到指定地方.
文件上传的第二种方法,这种方法比较常用:
package gd.hz.springmvc.controller;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller("userController")
@RequestMapping("user")
public class UserController {
// 处理文件上传二
@RequestMapping(value = "fileUpload2", method = RequestMethod.POST)
public String fileUpload2(HttpServletRequest request)
throws IllegalStateException, IOException {
// 设置上下方文
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 检查form是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 由CommonsMultipartFile继承而来,拥有上面的方法.
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {
String fileName = "demoUpload" + file.getOriginalFilename();
String path = "D:/" + fileName;
File localFile = new File(path);
file.transferTo(localFile);
}
}
}
return "dataSuccess";
}
}
MultipartHttpServletRequest提供了更加灵活的方法,可以获取多个文件和文件名,可以遍历获得每个文件.
来源:http://mylfd.iteye.com/blog/1893648


猜你喜欢
- 一、新时间日期API常用、重要对象介绍ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则Instan
- 1. 前言Spring提供了xml、注解、JavaConfig多种方式来配置bean,不论何种方式,Spring最终都会将bean封装成Be
- RecyclerView是Android 5.0的新特性,可以直接代替ListView与GridView,并且能够实现瀑布流的布局,感觉Re
- 由于要做一个新项目,所以打算做一个简单的图片验证码。先说说思路吧:在服务端,从一个文件夹里面找出8张图片,再把8张图片合并成一张大图,在8个
- 一、项目介绍【知识准备】①Android Interface definition language(aidl,android接口定义语言)
- 随着使用Spring进行开发的个人和企业越来越多,Spring从一个单一简介的框架变成了一个大而全的开源软件,最直观的变化就是Spring需
- HttpWebRequest 是一个Http 请求类,继承于 WebRequest。WebRequest 是一个抽象类,能够对统一资源标识符
- 本问介绍了Collections工具类两种sort()方法,具体如下:一、Collections工具类两种sort()方法格式一: publ
- 其实写到这里,我已经差不多断气了。。。常规套路,这里是前三篇的传送门,需要的同学可以看一下:JAVA写文本编辑器(三) JAVA写文本编辑器
- 本文主要是分析Spring bean的循环依赖,以及Spring的解决方式。 通过这种解决方式,我们可以应用在我们实际开发项目中。1. 什么
- 1. 前言Guava是一个由Google开发的Java核心库,它提供了很多有用的方法和实用工具类,可以帮助开发人员提高代码质量和开发效率。在
- 1 简介Solace是一个强大的实时性的事件驱动消息队列。本文将介绍如何在Spring中使用,虽然代码使用的是Spring Boot,但并没
- 一、事件背景个人感觉自己做性能测试,可以说是轻车熟路了,而且工作多年一直都是这一套测试思路及体系,从未质疑过自己,也许是狮子座的迷之自信吧!
- 好久没有写过博客了,最近因项目需求,需要用到Socket来进行通信,简单写了几个例子,记录一下,代码很简单,无非就是接收与发送,以及接收到数
- 这篇文章介绍使用设计模式中的策略模式和委托来解决多个IfElse判断语句和Switch语句,这种替换方式在其他语言也一样可以做到,比如PHP
- 前言在做JAVA EE开发的过程中,更多的是使用框架来提高开发效率.越来越发现,之前很基础的一些东西,都忘记的差不多了.从今天开始慢慢的复习
- 如下所示:if(str.indexOf(",") >= 0) System.out.println(&
- 一.添加控件IrisSkin2.dll。方法:  
- 问题当我们数据库中的字段和实体类中的字段不一致的时候,查询会出问题数据库字段是 pwdid name pwd1 张三 1234562 李四
- 本文实例讲述了Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法。分享给大家供大家参考,具体如下