springcloud 使用feign 上传图片

一,上传图片接口

微服务:web-common

这个接口提供给feign 来RPC调用

@RequestMapping(value = "upload", method = RequestMethod.POST)
    public ApiResult<FileVO> upload(HttpServletRequest request) throws Exception {
        FileVO fileVO =new FileVO();
        MultipartHttpServletRequest murequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> files = murequest.getFileMap();// 得到文件map对象
        for (MultipartFile file : files.values()) {
            //内部操作逻辑,我上传到MINIO
            fileVO = fileService.upload(file);
        }

        ApiResult<FileVO> result = new ApiResult<>();
        result.setCodeToSuccessed();
        result.setData(fileVO);
        return result;

    }


// fileService.upload方法
@Override
    public FileVO upload(MultipartFile file) throws Exception {

        //源文件名
        String originalFilename = file.getOriginalFilename();
        //保存文件的路径(如2020/7/10/4d132f66a7b948c882e2c1649a58ab70.jpg)
        String saveFileName = FileHelper.getFileName(FileHelper.getFileEx(originalFilename));
        //初始化MinioClient
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minioHostUrl)
                .credentials(minioAccessKey, minioSecretKey)
                .build();
        //上传
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(minioBucketName)        //桶名(文件夹名)
                        .stream(file.getInputStream(), file.getSize(), 5 * 1024 * 1024)      //文件,大小
                        .object(saveFileName)       //文件名
                        .build());
        String backFileName = "/" + minioBucketName + "/" + saveFileName;
        return backFileName;
    }

二,fegin调用

微服务:web-test

POM

<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-core -->
    <dependency>
      <groupId>io.github.openfeign</groupId>
      <artifactId>feign-core</artifactId>
      <version>11.2</version>
    </dependency>
    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form-spring</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>

三,编写feign 

package com.tenyears.webAD.feign;

import com.tenyears.model.common.ApiResult;
import com.tenyears.model.webCommon.FileVO;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @description :
 * @auther Tyler
 * @date 2021/6/15
 */

@FeignClient(value = "web-common")
public interface WebCommonFeign {
    @RequestMapping(value = "api/file/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
    ApiResult<FileVO> upload(@RequestPart("file") MultipartFile file);

    

@Configuration
    class MultipartSupportConfig {
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}

注意上面的@Configuration

这样配置又能上传文件,又能传递RequestBody!

@Configuration
    class MultipartSupportConfig {
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }

四,controller

@RestController
@RequestMapping("api/test")
public class TestController {
    Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    WebCommonFeign webCommonFeign;

    /**
     * feign 上传图片
     * @param file
     * @return
     */
    @RequestMapping(value = "test4", method = RequestMethod.POST)
    public ApiResult<FileVO> test4(MultipartFile file) {
        ApiResult<FileVO> result = webCommonFeign.upload(file);
        return result;
    }
}

五,效果

顺带一提,图片+参数的传递方式:

被调用方:

@RequestMapping(value = "uploadPic2MovWithSecond", method = RequestMethod.POST)
    public ApiResult<FileVO> uploadPic2MovWithSecond(
@RequestPart("file") MultipartFile file
,@RequestParam("second")int second) throws Exception {
        

    }

feign:

@RequestMapping(value = "api/file/uploadPic2MovWithSecond",consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
    ApiResult<FileVO> uploadPic2MovWithSecond(
@RequestPart("file") MultipartFile files
,@RequestParam("second")int second) ;

调用方:

@RequestMapping(value = "test6", method = RequestMethod.POST)
    public ApiResult<FileVO> test6(
@RequestPart("file") MultipartFile file
,@RequestParam("second")int second) {
        ApiResult<FileVO> result = webCommonFeign.uploadPic2MovWithSecond(file,second);
        return result;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值