溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用Feign怎么實(shí)現(xiàn)一個跨服務(wù)文件上傳下載功能

發(fā)布時間:2021-03-08 14:30:38 來源:億速云 閱讀:360 作者:Leah 欄目:編程語言

本篇文章給大家分享的是有關(guān)利用Feign怎么實(shí)現(xiàn)一個跨服務(wù)文件上傳下載功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1、跨服務(wù)文件上傳,目前feign不支持調(diào)用文件上傳接口,需要自行配置來滿足feign的調(diào)用方式

①.首先需要在pom文件里添加feign依賴

<dependency> 
 <groupId>io.github.openfeign.form</groupId> 
 <artifactId>feign-form-spring</artifactId> 
 <version>3.2.2</version> 
</dependency> 
<dependency> 
 <groupId>io.github.openfeign.form</groupId> 
 <artifactId>feign-form</artifactId> 
 <version>3.2.2</version> 
</dependency>

②.上傳的接口

@FeignClient(value = "fdn-storage", configuration = {FileFeignConfig.class})
public interface FileClient {

 String PREFIX_PATH = "/oss/files";
 /**
  * 上傳存儲文件
  * @param file
  * @return
  * @throws IOException
  */
 @PostMapping(value = PREFIX_PATH + "/", consumes = MULTIPART_FORM_DATA_VALUE)
 FeignResult<FileEntity> save(@RequestPart(value = "file") MultipartFile file) throws IOException;
 }

③.添加配置來滿足feign的調(diào)用

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

 @Bean
 @Primary
 @Scope("prototype")
 public Encoder feignEncoder() {
  return new SpringFormEncoder(new SpringEncoder(messageConverters));
 }

 @Bean
 public feign.Logger.Level multipartLoggerLevel() {
  return feign.Logger.Level.FULL;
 }
}

④.外部服務(wù)的controller層調(diào)用

public class TestController extends BaseRestController {
 @Autowired
 FileClient client;
 /**
  * 上傳文件
  **/
 @PostMapping(value = "/" , consumes = MULTIPART_FORM_DATA_VALUE)
 public FileEntity save(@RequestPart(value = "file") MultipartFile file) throws IOException {
  FileEntity fileEntity = client.save(file).getData();
  return fileEntity;
 }
}

到此位置就可以上傳成功了

2、跨服務(wù)的文件下載

①.下載的接口(也是寫在public interface FileClient),是用feign.Response來作為返回值的

/**
  * 下載文件
  * @param id
  * @return
  * @throws IOException
  */
 @GetMapping(value = PREFIX_PATH + "/{id}/data")
 Response download(@PathVariable("id") String id) throws IOException;

②.外部服務(wù)的controller層調(diào)用

 /**
  *由id下載存儲的文件
  */
 @GetMapping(value = "/{id}/data")
 public void downloadFile(@PathVariable String id, HttpServletResponse servletResponse) throws IOException {
  Response response = client.download(id);
  Response.Body body = response.body();
  for(Object key : response.headers().keySet()){
   List<String> kList = (List)response.headers().get(key);
   for(String val : kList){
    servletResponse.setHeader(StringUtils.toString(key), val);
   }
  }
  try(InputStream inputStream = body.asInputStream();
   OutputStream outputStream = servletResponse.getOutputStream()
  ){
   byte[] b = new byte[inputStream.available()];
   inputStream.read(b);
   outputStream.write(b);
   outputStream.flush();
  }catch (IOException e){
   throw new RestException("IO流異常", e);
  }
 }

以上就是利用Feign怎么實(shí)現(xiàn)一個跨服務(wù)文件上傳下載功能,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI