Python 官方文档:入门教程 => 点击学习
目录如何使用Feign后台内部传递MultipartFile先修改FeignClient接口内部调用注意点Feign进行跨服务传递MultipartFile文件通过feign进行跨服
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;
@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);
class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
}
若SpringFormEncoder 引入报错,加上下面的依赖
<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>
private String uploadFile(String str) {
FileOutputStream fos = null;
FileInputStream fis = null;
MultipartFile multipartFile = null;
byte[] bt = str.getBytes();
File 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进行文件上传模块。
使技术人员在开发过程中遇到问题时有地可查,有章可循。
添加依赖
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.0.3</version>
</dependency>
添加配置文件
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<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
代码示例
@FeignClient(value = "controller-center")
public interface CallFrignService {
@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);
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: springcloud如何使用Feign后台内部传递MultipartFile
本文链接: https://lsjlt.com/news/141317.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0