springboot文件(图片)上传问题之一
最近把springboot版本从2.0.0换成版本2.3.12.RELEASE导致之前文件(图片)是成功的,换了版本号后导致MultipartFile接收到的数据一直是null
控制层代码
这里的参数MuiltiPartFile是org.springframework.web.multipart.MultipartFile类型的参数
/**
* 图片上传
* @return
*/
@PostMapping("/upload")
public Result uploadImage(@RequestPart("files") MultipartFile[] files) {
return Result.ok(iPostService.uploadImage(files));
}
服务层代码
@Override
public List<String> uploadImage(MultipartFile[] files){
//获取文件字节数组
File pfile = new File(postFilesPath);
if (!pfile.exists()) {//判断文件夹是否存在,不存在时,创建文件夹
pfile.mkdirs();
}
logger.info("文件:"+files);
logger.info("文件长度length:"+files.length);
List<String> fileIds=new ArrayList<>();
int index = 1;
if (files != null) {
List<PostAttachment> postAttachments=new ArrayList<>();
for (MultipartFile file : files) {
try {
String fileType = FileUtils.getFileTypeByStream(file.getBytes());
String fileId = CommonUtil.GUID();
fileIds.add(fileId);
String lastFileName = fileId + "." + fileType;
String fileFullPath = postFilesPath + File.separator + lastFileName;
// 图片存储的全路径
FileCopyUtils.copy(file.getBytes(), new File(fileFullPath));
PostAttachment postAttachment = new PostAttachment();
postAttachment.setId(fileId);
postAttachment.setFileType(fileType);
postAttachment.setFileOrder(index++);//文件顺序
postAttachment.setIsDelete(Keys.constant.delete.isNotDelete);
postAttachments.add(postAttachment);
}catch (IOException e) {
log.error(e.getMessage(), e);
}
}
postAttachmentService.saveBatch(postAttachments);
}
return fileIds;
}
反复的测试,结果还是一样的。
传到控制层的代码就一直是null。
最后找的的解决办法是:
在boot应用的main方法所在的类中注入:
package com.hhh.cloud.framework;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@SpringBootApplication
@ComponentScan(basePackages={"com.hhh.cloud.framework"})//扫描接口
@MapperScan({"com.hhh.cloud.framework.*.*.mapper","com.hhh.cloud.framework.*.mapper"})
@EnableFeignClients
public class BotApplication {
public static void main(String[] args) {
SpringApplication.run(BotApplication.class, args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate(new HttpComponentsClientHttpRequestFactory());
}
/**
* 监听器:监听HTTP请求事件
* 解决RequestContextHolder.getRequestAttributes()空指针问题
* @return
*/
@Bean
public RequestContextListener requestContextListener(){
return new RequestContextListener();
}
//解决上传文件接收为null的方法
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
}
}