SpringMVC使用MultipartResolver实现文件上传
作者:loli_kong 时间:2023-02-19 08:34:38
SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的。 所以,如果要实现文
件的上传,只要在 spring-mvc.xml 中注册相应的 MultipartResolver 即可。
MultipartResolver 的实现类有两个:
CommonsMultipartResolver
StandardServletMultipartResolver
两个的区别:
第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,但它能在比较旧的servlet 版本中使用。
第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,但是只能在Servlet 3 以上的版本使用。
我们这里使用第一种来实现上传。
1.导入依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2.再application中添加配置
<!-- 多部分文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="104857600"/>
<property name="maxInMemorySize" value="4096"/>
</bean>
**maxUploadSize:**设置允许上传文件的最大值,以字节为单位。默认为-1,表示无限制。
**defaultEncoding:**表示用来请求解析request请求的默认编码格式,当没有指定时默认使用ISO-8859-1。当request自己指明了他的编码格式的时候就会忽略这里的defaultEncoding。
3.编写controller代码
@Autowired
private HttpServletRequest request;
@PostMapping("native")
public String nativeUpload(@RequestParam("file") MultipartFile file, String folder) throws UnsupportedEncodingException {
// 拿到webapp下面img文件夹的位置
String path = request.getSession().getServletContext().getRealPath("img");
// getOriginalFilename获取文件的原始名称,例如:2.jpg
String filePath = path + "\\" + file.getOriginalFilename();
File destFile = new File(filePath);
// 如果目录不存在
if (!destFile.getParentFile().exists()) {
// 创建目录(可能位多层)
destFile.mkdirs();
}
// destFile:目标文件
try {
file.transferTo(destFile);
} catch (IOException e) {
e.printStackTrace();
}
String res = "http://localhost:9101/img/" + file.getOriginalFilename();
return new String(res.getBytes(), "UTF-8");
}
这里MultipartFile是spring类型,代表HTML中form data方式上传的文件,包含二进制数据+文件名称
来源:https://blog.csdn.net/loli_kong/article/details/124601693
标签:SpringMVC,MultipartResolver,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android开发笔记之:Handler Runnable与Thread的区别详解
2023-11-10 12:29:01
![](https://img.aspxhome.com/file/2023/7/99807_0s.png)
浅析Android代码质量管理
2021-06-18 23:02:03
![](https://img.aspxhome.com/file/2023/6/139166_0s.jpg)
java中的interface接口实例详解
2023-10-12 22:03:10
Java 获取当前时间及实现时间倒计时功能【推荐】
2022-08-21 16:08:27
![](https://img.aspxhome.com/file/2023/7/75657_0s.png)
mybatis foreach遍历LIST读到数据为null的问题
2021-05-24 20:15:27
![](https://img.aspxhome.com/file/2023/4/68554_0s.jpg)
Springboot项目打war包docker包找不到resource下静态资源的解决方案
2022-01-01 07:03:55
C#使用DLLImport调用外部DLL的方法
2022-07-17 04:41:18
深入理解Java设计模式之桥接模式
2022-04-11 07:34:53
![](https://img.aspxhome.com/file/2023/0/113180_0s.jpg)
C#中类的异常处理详解
2023-02-21 17:25:01
Android中Socket大文件断点上传示例
2022-07-11 02:33:32
![](https://img.aspxhome.com/file/2023/9/117139_0s.jpg)
Spring boot动态修改日志级别的方法
2023-04-04 09:36:00
![](https://img.aspxhome.com/file/2023/3/79853_0s.png)
javaweb实现app扫码登录功能
2022-03-25 03:48:19
关于@Autowired注入依赖失败的问题及解决
2023-11-29 00:50:42
![](https://img.aspxhome.com/file/2023/4/60474_0s.png)
Java的Spring框架中AOP项目的一般配置和部署教程
2021-07-26 16:49:25
在idea中使用JaCoCo插件统计单元测试覆盖率的实现
2022-12-27 12:31:28
![](https://img.aspxhome.com/file/2023/2/69682_0s.jpg)
SSH框架网上商城项目第23战之在线支付功能实现
2023-04-22 22:49:19
RestTemplate文件上传下载与大文件流式下载
2023-10-15 16:54:14
Android Walker登录记住密码页面功能实现
2023-12-16 05:45:03
![](https://img.aspxhome.com/file/2023/6/137436_0s.jpg)
Spring工作原理简单探索
2023-04-26 10:30:56
![](https://img.aspxhome.com/file/2023/7/94327_0s.jpg)
详解java基础--提示对话框的使用
2023-01-31 12:11:46
![](https://img.aspxhome.com/file/2023/2/80902_0s.jpg)