springcloud如何使用Feign后台内部传递MultipartFile

网友投稿 611 2022-08-23


springcloud如何使用Feign后台内部传递MultipartFile

目录如何使用Feign后台内部传递MultipartFile先修改FeignClient接口内部调用注意点Feign进行跨服务传递MultipartFile文件通过feign进行跨服务传递MultipartFile文件

如何使用Feign后台内部传递MultipartFile

先修改Feign Client接口

import feign.codec.Encoder;

import feign.form.spring.SpringFormEncoder;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.context.annotation.Bean;

import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RequestPart;

import org.springframework.web.multipart.MultipartFile;

/**

* @author linli

* @date 20-06-27

*/

@FeignClient(value = "upload", fallbackFactory = UploadFallbackFactory.class, configuration = UploadClient.MultipartSupportConfig.class)

public interface UploadClient {

@PostMapping(path = "/upload-text", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

String uploadText(@RequestPart(name = "file") MultipartFile file);

/**

* 引用配置类MultipartSupportConfig.并且实例化

*/

class MultipartSupportConfig {

@Bean

public Encoder feignFormEncoder() {

return new SpringFormEncoder();

}

}

}

若SpringFormEncoder 引入报错,加上下面的依赖

io.github.openfeign.form

feign-form

3.3.0

io.github.openfeign.form

feign-form-spring

3.3.0

内部调用

private String uploadFile(String str) {

FileOutputStream fos = null;

FileInputStream fis = null;

MultipartFile multipartFile = null;

byte[] bt = str.getBytes();

FiLbJvClQble file = null;

try {

file = File.createTempFile("file" + UUID.randomUUID(), ".txt");

fos = new FileOutputStream(file);

fos.write(bt, 0, bt.length);

fis = new FileInputStream(file);

multipartFile = new MockMultipartFile("file", file.getName(),

MediaType.TEXT_PLAIN_VALUE, fis);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return uploadClient.uploadText(multipartFile);

}

注意点

Feign进行跨服务传递MultipartFile文件

通过调用服务进行文件上传,避免每个需要上传文件的模块都写一遍上传服务,造成代码冗余。

本文主要包含通过feign进行文件上传模块。

使技术人员在开发过程中遇到问题时有地可查,有章可循。

通过feign进行跨服务传递MultipartFile文件

添加依赖

io.github.openfeign.form

feign-form

3.0.3

io.github.openfeign.form

feign-form-spring

3.0.3

添加配置文件

package com.ruiyi.twowayreferral.configurer;

import feign.codec.Encoder;

import feign.form.spring.SpringFormEncoder;

import org.springframework.beans.factory.ObjectFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;

import org.springframework.cloud.openfeign.support.SpringEncoder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class MultipartSupportConfig {

@Autowired

private ObjectFactory messageConverters;

@Bean

public Encoder feignFormEncoder() {

return new SpringFormEncoder(new SpringEncoder(messageConverters));

}

}

代码示例

@FeignClient(value = "controller-center")

public interface CallFrignService {

/**

* @Create 文件上传 wanggx_ruiyi 2019.11.15

* @param uploadPath 文件上传地址

* @param file 上传的文件

* @return

*/

@PostMapping(value = "/api/v1/common/file/fileUpload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

String fileUpload(@RequestParam(value = "uploadPath", required = true) String uploadPath,@RequestPart(value = "file", required = true) MultipartFile file);

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:django订阅jira webhook事件数据(django 用户登录)
下一篇:django订阅gerrit事件流数据(django 消息推送)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~