springboot文件上传
如果报错如下:
The field file exceeds its maximum permitted size of 1048576 bytes.
或者如下:
Required request part ‘file’ is not present
上面的报错可能是因为spring boot 内置的MultipartResolver有点问题,可以这样解决:
1、显性注册MultipartResolver:
// 显示声明CommonsMultipartResolver为mutipartResolver
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver()
{
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
// resolver.setDefaultEncoding("UTF-8");
// resolver.setResolveLazily(true);// resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
// resolver.setMaxInMemorySize(40960);
resolver.setMaxUploadSize(10 * 1024 * 1024);// 上传文件大小 5M 5*1024*1024
return resolver;
}
2、并且在spring boot的启动类添加注解:
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
这个注解的意思是排除内置的MultipartResolver。如果没有这个注解,显性注册的MultipartResolver会无效。这样处理以后就能正常上传了
版权声明:本文为qq_32649581原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。