当前位置:网站首页>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实现类:
CommonsMultipartResolverStandardServletMultipartResolver
区别:
第一个需要使用 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;
}

边栏推荐
- 4、敏感词过滤(前缀树)
- 【shell基础】判断目录是否为空
- Mysql 45讲学习笔记(二十四)MYSQL主从一致
- 10 权限介绍
- AI software development process in medical imaging field
- YOLOV5学习笔记(二)——环境安装+运行+训练
- CloudCompare&PCL 计算两个点云之间的重叠度
- The modification is not properly placed in the sandbox, causing Apple compatibility issues
- 【编译原理】递归下降语法分析设计原理与实现
- Local area network computer hardware information collection tool
猜你喜欢

局域网电脑硬件信息收集工具

共模电感的仿真应用来了,满满的干货送给大家

19.支持向量机-优化目标和大间距直观理解

Local area network computer hardware information collection tool

19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing

12 磁盘相关命令

IIR滤波器和FIR滤波器

8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)

LeetCode简单题之两个数组间的距离值

Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
随机推荐
Chapter 9 SVM实践
SonarQube的BUG定义
Multilingual settings of php website (IP address distinguishes domestic and foreign)
StringJoiner详解
php 网站的多语言设置(IP地址区分国内国外)
多线程下类对象的服务承诺探讨
【C语言基础】解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token
2022牛客多校联赛第四场 题解
C#远程调试
跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
10 Permission introduction
SQL注入 Less47(报错注入) 和Less49(时间盲注)
The difference between link and @import
Installation of mysql5.7.37 under CentOS7 [perfect solution]
原子操作 CAS
Difference between CMOS and TTL?
What is distributed and clustered?What is the difference?
MultipartFile文件上传
学习DAVID数据库(1)
7、私信列表