MultipartResolver实现文件上传功能
作者:紫_色 时间:2021-06-19 22:38:15
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
标签:MultipartResolver,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot、mybatis返回树结构的数据实现
2022-05-12 18:56:08
![](https://img.aspxhome.com/file/2023/5/77365_0s.png)
Java实现抢红包功能
2021-08-05 07:47:58
![](https://img.aspxhome.com/file/2023/9/67109_0s.jpg)
C#实现获取机器码的示例详解
2021-11-13 06:57:19
![](https://img.aspxhome.com/file/2023/6/80096_0s.png)
Java流式操作之Collectors工具类操作指南
2022-12-01 01:25:55
![](https://img.aspxhome.com/file/2023/9/72389_0s.png)
hadoop实现grep示例分享
2023-12-24 05:31:04
grails不能运行fork模式解决方法
2023-05-17 18:37:59
Java简单计时的实现案例(可以用来限时循环)
2023-02-01 21:39:19
java过滤器中Filter的ChainFilter过滤链
2021-09-29 00:03:42
![](https://img.aspxhome.com/file/2023/5/72575_0s.png)
详解如何在SpringBoot项目中使用统一返回结果
2023-10-30 16:41:21
![](https://img.aspxhome.com/file/2023/4/58714_0s.png)
Java 前台加后台精品图书管理系统的实现
2023-10-23 04:06:47
![](https://img.aspxhome.com/file/2023/3/79863_0s.png)
使用Springboot根据配置文件动态注入接口实现类
2022-11-18 06:56:45
java+SpringBoot设计实现评教系统
2023-02-05 13:12:18
![](https://img.aspxhome.com/file/2023/6/69896_0s.jpg)
详解Spring关于@Resource注入为null解决办法
2023-02-13 18:14:13
![](https://img.aspxhome.com/file/2023/7/82017_0s.png)
详解在LINUX上部署带有JAR包的JAVA项目
2022-12-30 21:07:14
![](https://img.aspxhome.com/file/2023/7/78427_0s.png)
如何写好一个Spring组件的实现步骤
2023-01-08 20:24:12
![](https://img.aspxhome.com/file/2023/2/75032_0s.jpg)
使用java生成字母验证码
2021-10-29 23:50:25
java线程之join方法的使用介绍
2023-01-15 12:18:40
Spring Bean常用依赖注入方式详解
2022-06-05 21:43:33
JAVA实现对阿里云DNS的解析管理
2023-11-24 13:25:15
Java实现角色扮演游戏的示例代码
2023-03-31 19:41:45
![](https://img.aspxhome.com/file/2023/9/80939_0s.jpg)