Springboot 上传接受文件上传参数 null,Configuration注入bean 失败?
新手 最近在做一个上传的功能
@Resource MinioUtils minioUtils;
@PostMapping("/upload")
@ApiOperation("文件上传")
@ResponseBody
public List<String> upload(@RequestParam(name = "file", required = false) MultipartFile[] file){
if(file == null || file.length == 0){
throw new CustomException(CustomExceptionType.USER_INPUT_ERROR,"上传文件不能为空");
}
List<String> upload = minioUtils.uploads(file);
return upload;
}
这是我文件上传的 代码, 接收到的file 一直是null 请问这个要怎么处理呢
我搜索了下 有些人说要注入一个BEAN
@Configuration@EnableAutoConfiguration(exclude={MultipartAutoConfiguration.class})
public class UploadConfig {
@Bean(name="multipartResolver")
public MultipartResolver multipartResolver(){
return new CommonsMultipartResolver();
}
}
但是我注入的时候一直报错
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multipartResolver' defined in class path resource [com/szhb/backend/config/UploadConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartResolver]: Factory method 'multipartResolver' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/fileupload/disk/DiskFileItemFactory at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.12.jar:5.3.12]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.12.jar:5.3.12]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) [spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) [spring-boot-2.5.6.jar:2.5.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) [spring-boot-2.5.6.jar:2.5.6]
at com.szhb.backend.BackendApplication.main(BackendApplication.java:16) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartResolver]: Factory method 'multipartResolver' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/fileupload/disk/DiskFileItemFactory
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.12.jar:5.3.12]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.12.jar:5.3.12]
... 19 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/disk/DiskFileItemFactory
at org.springframework.web.multipart.commons.CommonsFileUploadSupport.newFileItemFactory(CommonsFileUploadSupport.java:205) ~[spring-web-5.3.12.jar:5.3.12]
at org.springframework.web.multipart.commons.CommonsFileUploadSupport.<init>(CommonsFileUploadSupport.java:77) ~[spring-web-5.3.12.jar:5.3.12]
at org.springframework.web.multipart.commons.CommonsMultipartResolver.<init>(CommonsMultipartResolver.java:97) ~[spring-web-5.3.12.jar:5.3.12]
at com.szhb.backend.config.UploadConfig.multipartResolver(UploadConfig.java:19) ~[classes/:na]
at com.szhb.backend.config.UploadConfig$$EnhancerBySpringCGLIB$$21d563eb.CGLIB$multipartResolver$0(<generated>) ~[classes/:na]
at com.szhb.backend.config.UploadConfig$$EnhancerBySpringCGLIB$$21d563eb$$FastClassBySpringCGLIB$$191b5c2e.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.12.jar:5.3.12]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.12.jar:5.3.12]
at com.szhb.backend.config.UploadConfig$$EnhancerBySpringCGLIB$$21d563eb.multipartResolver(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_291]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_291]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_291]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_291]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.12.jar:5.3.12]
... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.disk.DiskFileItemFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_291]
at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_291]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_291]
at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_291]
... 34 common frames omitted
大佬们帮我看看呢
回答:
是不是前端请求的问题导致后端接收的 file 为空?
回答:
我是用这个注解
@ResponseBody@RequestMapping(value = "/path", method = RequestMethod.POST)
@ApiOperation(value = "Upload file")
public Object uploadFile(@RequestParam MultipartFile[] files) {
}
回答:
看看是不是前端传文件的时候参数名不叫file
回答:
后端
@PostMapping("/uploadfile")public void uploadfile(@RequestParam MultipartFile file){
if(file==null) {
System.out.println("null file");
} else {
System.out.println("file");
}
}
postman:
后台断点结果:
回答:
public List<String> upload( @RequestParam(name = "file", required = false) MultipartFile[] file){
----------------------------------------------------------------?-------
public List<String> upload(
@RequestParam(name = "file", required = false) MultipartFile file){
以上是 Springboot 上传接受文件上传参数 null,Configuration注入bean 失败? 的全部内容, 来源链接: utcz.com/p/944788.html