Feign realizes uploading and downloading cross service files

  • 2021-07-22 09:55:57
  • OfStack

This article example for everyone to share Feign cross-service file upload and download operations, for your reference, the specific content is as follows

1. Cross-service file upload. At present, feign does not support calling file upload interface, so it needs to be configured by itself to meet the calling mode of feign

First, you need to add the feign dependency to the pom file


<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>

② Upload interface


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

 String PREFIX_PATH = "/oss/files";
 /**
  *  Upload storage file 
  * @param file
  * @return
  * @throws IOException
  */
 @PostMapping(value = PREFIX_PATH + "/", consumes = MULTIPART_FORM_DATA_VALUE)
 FeignResult<FileEntity> save(@RequestPart(value = "file") MultipartFile file) throws IOException;
 }

Add configuration to meet the call of feign


@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;
 }
}

④. controller layer invocation of external services


public class TestController extends BaseRestController {
 @Autowired
 FileClient client;
 /**
  *  Upload a file 
  **/
 @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;
 }
} 

You can upload successfully at this location

2. Cross-service file download

Download interface (also written in public interface FileClient) with feign. Response as return value


/**
  *  Download a file 
  * @param id
  * @return
  * @throws IOException
  */
 @GetMapping(value = PREFIX_PATH + "/{id}/data")
 Response download(@PathVariable("id") String id) throws IOException;

② controller layer call of external service


 /**
  * By id Download stored files 
  */
 @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 Flow anomaly ", e);
  }
 }

At this point, the download of the file is complete.


Related articles: