搭建springboot后,新建控制器后,访问控制器接口,控制台报错:
Unable to process parts as no multi-part configuration has been provided
报错为:无法处理部件,未提供多部件配置。
看错误中的 multi-part 中字眼就知道是上传文件的组件没有注入到springboot中自定义的servlet中去即可。
registrationBean.setMultipartConfig(multipartConfigElement);
完整代码:
在你的启动类Application中的自定义servlet中添加代码:
//自动注入springboot的默认上传配置
@Autowired
private MultipartConfigElement multipartConfigElement;
@Bean
public ServletRegistrationBean<DispatcherServlet> servletMvc() {
//注解扫描上下文
AnnotationConfigWebApplicationContext applicationContext
= new AnnotationConfigWebApplicationContext();
//扫描包
applicationContext.scan("com.xx.com");
//通过构造函数指定dispatcherServlet的上下文
DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
//用ServletRegistrationBean包装servlet
ServletRegistrationBean<DispatcherServlet> registrationBean
= new ServletRegistrationBean<>(dispatcherServlet);
registrationBean.setLoadOnStartup(0);
registrationBean.setMultipartConfig(multipartConfigElement);
//指定name,如果不指定默认为dispatcherServlet
registrationBean.setName("rest");
return registrationBean;
}
本文详细介绍了在SpringBoot项目中遇到的文件上传错误:Unable to process parts as no multi-part configuration has been provided。文章提供了完整的解决方案,包括如何在自定义servlet中正确配置MultipartConfigElement,确保文件上传组件正常工作。
1815

被折叠的 条评论
为什么被折叠?



