(已解决)用OpenFeign上传文件信息报错Current request is not a multipart request/ no multipart boundary was found解决

项目场景:

使用springcloud +Openfeign+minio上传文件时报错


问题描述

其实时在上传文件时,请求头中的信息没有被feign支成功正确的扫描到。
报错

报错2024-06-04 09:39:16.826 ERROR 13708 --- [nio-8888-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause

org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
	at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:189) ~[tomcat-embed-core-9.0.55.jar:9.0.55]
	at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:205) ~[tomcat-embed-core-9.0.55.jar:9.0.55]
	at 

问题分析

服务发送方


@RestController
@RequestMapping("/product/oss")
public class FileController {
@Resource
private OpenFenginService openFenginService;
    //上传文件
    @PostMapping("/upload")
    public String uploadImage(@RequestParam("uploadFile") MultipartFile uploadFile) throws Exception {
        String url = openFenginService.upload(uploadFile);
        return url;
    }

opfeignservice中代码

@FeignClient(value = "jingxi-oss")
public interface OpenFenginService {
    @PostMapping(value = "/oss/upload")
    public String upload(@RequestPart("uploadFile") MultipartFile uploadFile ) throws Exception;
    @GetMapping("/oss/test")
    public String test(@RequestParam String test);
}

服务接受方

package com.oss.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;

import com.oss.entity.FileInfo;
import com.oss.entity.ImgDto;
import com.oss.entity.Result;
import com.oss.service.FileService;
import com.oss.util.MinioUtil;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
//热更新
@RefreshScope
@RestController
@RequestMapping("/oss")
public class FileController {
    @Resource
    private FileService fileService;
    @Resource
    private MinioUtil minioUtil;

    @NacosValue(value = "${storage.service.type}",autoRefreshed = true)
    private String storageType;



    @RequestMapping("/testGetAllBuckets")
    public String testGetAllBuckets() throws Exception {
        List<String> allBucket = fileService.getAllBucket();
        return allBucket.get(0);
    }
    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("uploadFile") MultipartFile uploadFile ) throws Exception {
        String bucket ="demo";
        String objectName="test";
        String url = fileService.uploadFile(uploadFile, bucket, objectName);
        return url;
    }
    @GetMapping("/test")
    public String test(@RequestParam String test){
        return test;
    }




}


解决方案:

修改openfeign中方法:

修改的两点:

  1. 请求参数中注解加上consumes = MediaType.MULTIPART_FORM_DATA_VALUE
  2. @RequestParam修改成为@RequestPart

@FeignClient(value = "jingxi-oss")
public interface OpenFenginService {
    @PostMapping(value = "/oss/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String upload(@RequestPart("uploadFile") MultipartFile uploadFile ) throws Exception;
    @GetMapping("/oss/test")
    public String test(@RequestParam String test);
}

在这里插入图片描述

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值