springBoot版本不同文件(图片)上传的时候控制层MultipartFile接收到的数据一直是null

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;
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值