亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Feign實(shí)現(xiàn)微服務(wù)間文件下載

 更新時(shí)間:2019年04月26日 09:51:37   作者:AaronSimon  
這篇文章主要為大家詳細(xì)介紹了使用Feign實(shí)現(xiàn)微服務(wù)間文件下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在使用Feign做服務(wù)間調(diào)用的時(shí)候,當(dāng)下載大的文件會(huì)出現(xiàn)堆棧溢出的情況。另外,文件管理服務(wù)(服務(wù)提供者)文件下載接口無返回值,是通過HttpServletRespoonse傳輸?shù)牧鲾?shù)據(jù)來響應(yīng),那么服務(wù)消費(fèi)者該如何接受下載的數(shù)據(jù)呢?

一. 示例介紹

我們調(diào)用feign_upload_second的下載文件接口下載文件,feign_upload_second內(nèi)部使用feign調(diào)用feign_upload_first實(shí)現(xiàn)文件下載。

二、feign_upload_first服務(wù)提供者

服務(wù)提供者下載文件接口

@RequestMapping(value = "/downloadFile",method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
 public void downloadFile(HttpServletResponse response){
  String filePath = "D://1.txt";
  File file = new File(filePath);
  InputStream in = null;
  if(file.exists()){
  try {
   OutputStream out = response.getOutputStream();
   in = new FileInputStream(file);
   byte buffer[] = new byte[1024];
   int length = 0;
   while ((length = in.read(buffer)) >= 0){
   out.write(buffer,0,length);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if(in != null){
   try {
    in.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   }
  }
  }
 }

三、feign_upload_second服務(wù)消費(fèi)者

服務(wù)提供者遠(yuǎn)程調(diào)用接口

@RequestMapping(value = "/downloadFile",method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
 Response downloadFile();

用feign.Response來接收

服務(wù)提供者下載文件接口

@RequestMapping(value = "/download",method = RequestMethod.GET)
 public ResponseEntity<byte[]> downFile(){
 ResponseEntity<byte[]> result=null ;
 InputStream inputStream = null;
 try {
  // feign文件下載
  Response response = uploadService.downloadFile();
  Response.Body body = response.body();
  inputStream = body.asInputStream();
  byte[] b = new byte[inputStream.available()];
  inputStream.read(b);
  HttpHeaders heads = new HttpHeaders();
  heads.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=123.txt");
  heads.add(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON_VALUE);

  result = new ResponseEntity <byte[]>(b,heads, HttpStatus.OK);
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  if(inputStream != null){
  try {
   inputStream.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
 }
 return result;
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論