【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题

本文详细介绍了在SpringCloud环境中解决文件上传问题的过程,包括项目结构、关键代码展示以及问题总结。重点在于使用@RequestPart注解进行文件上传,并在FeginClient中配合使用,确保正确处理Multipart数据,避免资源类型错误。
摘要由CSDN通过智能技术生成

我在网上查了很多关于 Spring Cloud 文件上传的相关资料也花费了不少时间,根据他们提供的方案修改也没有得到解决,经过自己探讨与摸索,终于解决了我的问题,也与大家分享了下,如下:

一、项目结构

首先介绍一下项目结构,我们开发的项目结构比较简单

xxx-api工程:这个工程主要是对外提供接口.

service-xxx工程:这个工程承载核心业务处理服务

二、上代码

对于开发者来说,看代码比较实在,如下:

xxx-api工程下的文件上传相关代码:

1、xxx-api项目的 Controller类  至关重要的 @RequestPart 注解

    @PostMapping(path = "/uploadImg")
    public Result<String> uploadImg(@RequestPart("file") MultipartFile file, @RequestHeader String accessToken) {
        Resp result = authService.authUserByToken(accessToken);
        if (result.getCode() > -1) {
            String accountId = BeanUtil.beanToMap(result.getData()).get("account").toString();
            return filePoolService.uploadImg(file, accountId);
        } else {
            return Result.fail("获取用户信息失败,请重新尝试");
        }
    }

2、FeginClient类 注意标注 @PostMapping(path = "/api/file/uploadImg",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 和 @RequestPart 注解

/**
     * @param file      文件
     * @param accountId 用户唯一ID
     * @return 结果说明
     */
    @PostMapping(path = "/api/file/uploadImg",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Result<String> uploadImg(@RequestPart("file") MultipartFile file, @RequestParam String accountId);

二、service-xxx 工程,如下: 至关重要的 @RequestPart 注解

    @PostMapping(path = "/uploadImg")
    public Result<String> uploadImg(@RequestPart("file") MultipartFile file,@RequestParam String accountId) throws Exception {
        if (file.isEmpty()) {
            return Result.fail("没有要上传的文件");
        }
        BufferedImage image = ImageIO.read(file.getInputStream());
        if (image == null) {
            return Result.fail("文件错误");
        }
        byte[] imageInByte = ImgDealUtils.compress(image);
        String name = "";
        String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf('.'));
        if (fileName != null && !fileName.equals("")) {
            name = fileName;
        }
        return Result.success(this.filePoolService.saveFile(name + ".jpg", accountId, imageInByte));
    }

总结一下:

        对于文件类微服务的调用,要用 @RequestPart 注解标注,在合适位置配合注解 @PostMapping(path = "/api/file/uploadImg",consumes =MediaType.MULTIPART_FORM_DATA_VALUE) 进行 修饰使用,否则会报资源类型和其他的一些问题。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾利克斯冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值