Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker DevOps


Spring Cloud OpenFeign MultipartException 上传文件异常

Spring Boot OpenFeign 大约 1014 字

错误信息

错误一:MultipartException

MultipartException: Current request is not a multipart request

错误二:FileUploadException

FileUploadException: the request was rejected because no multipart boundary was found

错误三:FileNotFoundException

FileNotFoundException: MultipartFile resource [file] cannot be resolved to absolute file path

解决方法

FeignClient

设置consumesmultipart/form-data

注意:MultipartFile需标注为@RequestPart,而不是@RequestParam

@FeignClient(name = "file-upload", url = "${file-upload.url}")
public interface FileUploadFeignClient {

    @PostMapping(value = "/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    UploadResponse fileUpload(@RequestPart("file") MultipartFile file);

}

Controller

@RequestMapping("/file")
@RestController
public class FileUploadController {

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadResponse upload(@RequestPart("file") MultipartFile file) {
        // logic
        return UploadResponse.builder().build();
    }

}
阅读 92 · 发布于 2022-10-26

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

扫描下方二维码关注公众号和小程序↓↓↓

扫描二维码关注我
昵称:
随便看看 换一批