当前位置:网站首页>MultipartFile file upload
MultipartFile file upload
2022-07-31 03:02:00 【wfsm】
引用:https://www.cnblogs.com/896240130Master/p/6430908.html
springmvc 中,The upload of the file is through
MultipartResolver
实现的,所以,如果要实现文件的上传,只要在spring-servlet.xml
中注册相应的MultipartResovler
MultipartResolver
实现类:
CommonsMultipartResolver
StandardServletMultipartResolver
区别:
第一个需要使用 Apache 的 commons-fileupload
等jar包支持,But can be in the relatively old servlet 版本中使用
The second one does not require a third partyjar包支持,他使用servletBuilt-in upload function,但只能在servlet3 以上版本使用
引入 CommonsMultipartResolver
在spring-servlet.xml
中 引入 CommonsMultipartResolver
的bean
<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>
引入StandardServletMultipartResolver
在spring-servlet.xml
中引入 StandardServletMultipartResolver
的bean
<bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver" name="multipartResolver">
</bean>
在 web.xml
中 添加 multipart-config
配置
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<multipart-config>
<!-- 上传文件的大小限制 5M-->
<max-file-size>5242880</max-file-size>
<!-- The size limit for files in one form submission 10M-->
<max-request-size>10485760</max-request-size>
<!-- The size of the file will be automatically saved to the hard disk, 0 代表所有-->
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
代码:
jar包:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
@RestController
public class HelloController {
@PostMapping("/upload")
public String hello(HttpServletRequest request){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
System.out.println(multipartResolver.isMultipart(request));
// 判断是否有文件上传
if (multipartResolver.isMultipart(request)){
// 获取MultipartHttpServletRequest
MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request);
// MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 获取request中 所有文件名
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()){
MultipartFile file = multiRequest.getFile(iterator.next());
if (!file.isEmpty()){
// upload to this path
String path = "F:\\images";
String fileName = file.getOriginalFilename();
String newName = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
File targetFile = new File(path, newName);
try {
file.transferTo(targetFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "success";
}
@PostMapping("/upload02")
public String upload(MultipartFile[] files,HttpServletRequest request) throws IOException {
if (files != null && files.length>0){
for (MultipartFile file : files) {
System.out.println("文件流"+file.getInputStream());
System.out.println("文件字节大小"+file.getSize());
System.out.println("文件类型"+file.getContentType());
System.out.println("文件数据:"+file.getBytes());
System.out.println(file.getName());
System.out.println(file.getOriginalFilename());
// servletContext.getRealPath("/")
String filePath = request.getSession().getServletContext().getRealPath("/")+"assets";
File dir = new File(filePath);
if (!dir.exists()){
dir.mkdirs();
}
String fileUrl = filePath +File.separator+ file.getOriginalFilename();
file.transferTo(new File(fileUrl));
}
}
return "hehe";
}
}
遇到的问题
postman发送 带file的请求
MultipartFile 为null
解决: 1. 没有注入bean ,CommonsMultipartResolver
或者StandardServletMultipartResolver
,
2 . The name passed in by the front end must be the same as MultipartFile 的名字相同
request 转 MultipartHttpServletRequest 报错
解决:multipartResolver.resolveMultipart(request)
// 获取MultipartHttpServletRequest MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request);
引用:https://blog.csdn.net/MTone1/article/details/83826124
springboot中使用 CommonsMultipartResolver
默认引入的是StandardServletMultipartResolver
注入CommonsMultipartResolver
@Bean
CommonsMultipartResolver commonsMultipartResolver(){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setDefaultEncoding("utf-8");
multipartResolver.setResolveLazily(true);
multipartResolver.setMaxInMemorySize(40960);
// 上传文件大小 50M : 50*1024*1024
multipartResolver.setMaxUploadSize(50*1024*1024);
return multipartResolver;
}
边栏推荐
- Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
- 什么是系统?
- 工程(五)——小目标检测tph-yolov5
- 华为分布式存储FusionStorage知识点总结【面试篇】
- Thesis framework of the opening report
- 关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
- Discussion on Service Commitment of Class Objects under Multithreading
- TCP/IP四层模型
- 测试中的误报和漏报同样的值得反复修正
- Why is String immutable?
猜你喜欢
YOLOV5学习笔记(三)——网络模块详解
接口测试关键技术
分布式系统架构需要解决的问题
LeetCode中等题之分数加减运算
【编译原理】递归下降语法分析设计原理与实现
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
【Android】Room —— SQLite的替代品
图解lower_bound&upper_bound
LeetCode简单题之两个数组间的距离值
CentOS7下mysql5.7.37的安装【完美方案】
随机推荐
SQL注入 Less54(限制次数的SQL注入+union注入)
Chapter 9 SVM Practice
What is distributed and clustered?What is the difference?
Installation, start and stop of redis7 under Linux
10、Redis实现点赞(Set)和获取总点赞数
2022牛客多校联赛第四场 题解
YOLOV5 study notes (2) - environment installation + operation + training
f.grid_sample
Thesis framework of the opening report
The modification is not properly placed in the sandbox, causing Apple compatibility issues
MultipartFile文件上传
遗留系统的自动化策略
华为分布式存储FusionStorage知识点总结【面试篇】
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
TCP详解(三)
LeetCode 每日一题 2022/7/25-2022/7/31
mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
11. Redis implements follow, unfollow, and follow and follower lists
SQL injection Less54 (limited number of SQL injection + union injection)
什么是系统?