how to use spring-cloud-feign to download file?

See original GitHub issue

i try to define a feign-client like this:

@FeignClient(value = "epolicy", path = "/epolicy/download", url = "http://10.1.118.45:8001")
public interface EPolicyApi {

    @RequestMapping(method = RequestMethod.POST)
    byte[] download(@RequestParam("policyNo") String policyNo, @RequestParam("idNo") String idNo);
}

my http response’s MediaType is application/msdownload。

should i customize a HttpMessageConvter or do some other job?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
beldoncommented, Sep 11, 2017

Yes, I do it. Here is my code,including uploading and downloading. I use feign-form to upload file. feign-form

Feign-form maven dependencies

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

<dependency>
	<groupId>io.github.openfeign.form</groupId>
	<artifactId>feign-form-spring</artifactId>
	<version>2.2.1</version>
</dependency>
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class InMemoryMultipartFile implements MultipartFile {

    private final String name;
    private final String originalFileName;
    private final String contentType;
    private final byte[] payload;

    public InMemoryMultipartFile(File file) throws IOException {
        this.originalFileName = file.getName();
        this.payload = FileCopyUtils.copyToByteArray(file);
        this.name = "file";
        this.contentType = "application/octet-stream";
    }

    public InMemoryMultipartFile(String originalFileName, byte[] payload) {
        this.originalFileName = originalFileName;
        this.payload = payload;
        this.name = "file";
        this.contentType = "application/octet-stream";
    }

    public InMemoryMultipartFile(String name, String originalFileName, String contentType, byte[] payload) {
        if (payload == null) {
            throw new IllegalArgumentException("Payload cannot be null.");
        }
        this.name = name;
        this.originalFileName = originalFileName;
        this.contentType = contentType;
        this.payload = payload;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getOriginalFilename() {
        return originalFileName;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public boolean isEmpty() {
        return payload.length == 0;
    }

    @Override
    public long getSize() {
        return payload.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return payload;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(payload);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(payload);
    }
}
@FeignClient(value = "material", configuration = MaterialClient.MultipartSupportConfig.class)
public interface MaterialClient {

    @PostMapping("/uploadFile")
    @Headers("Content-Type: multipart/form-data")
    ResponseMO uploadFile(@RequestPart("file") MultipartFile file);


    @GetMapping("/oss/downFile")
    MultipartFile downFile(@RequestParam("key") String key);


    class MultipartSupportConfig {

        @Autowired
        ObjectFactory<HttpMessageConverters> messageConverters;

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

        @Bean
        @Primary
        @Scope("prototype")
        public Decoder decoder() {
            Decoder decoder = (response, type) -> {
                if (type instanceof Class && MultipartFile.class.isAssignableFrom((Class) type)) {
                    Collection<String> contentTypes = response.headers().get("content-type");
                    String contentType = "application/octet-stream";
                    if (contentTypes.size() > 0) {
                        String[] temp = new String[contentTypes.size()];
                        contentTypes.toArray(temp);
                        contentType = temp[0];
                    }


                    byte[] bytes = StreamUtils.copyToByteArray(response.body().asInputStream());
                    InMemoryMultipartFile inMemoryMultipartFile = new InMemoryMultipartFile("file","", contentType,bytes);
                    return inMemoryMultipartFile;
                }
                return new SpringDecoder(messageConverters).decode(response, type);
            };
            return new ResponseEntityDecoder(decoder);
        }
    }
}
1reaction
lexburnercommented, Sep 11, 2017

@beldon Your rewritten behavior is consistent with my thoughts.But my final practice is to use byte[] as the return value which supported by ByteArrayHttpMessageConverter.Then use the service turns byte[] to file or other type. In my opinion,your inMemoryMultipartFile may be in line with OO design.but MultipartSupportConfig almost will not be reused.:)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feign for downloading file - spring boot - Stack Overflow
I am trying to find a simple way to use Feign to download a csv file (retaining the filename).
Read more >
File upload and download using openfeign - Spring Cloud
Table of Contents. Using Feign independently. Upload files; Download the file. Using Spring Cloud Feign. Uploading files; Download the file.
Read more >
File Upload With Open Feign - Baeldung
In this tutorial, we'll demonstrate how to upload a file using Open Feign. Feign is a powerful tool for microservice developers to ...
Read more >
OpenFeign] 5 Feign Spring Cloud upload、download File ...
今天博主要总结的是如何运用Spring Cloud Feign 来实现文件上传下载功能。 ... log.info("Use feign Call service file download");
Read more >
[Solved]-Feign for downloading file-Springboot - appsloveworld
Related Query · Feign for downloading file · Spring Boot endpoint for downloading File automatically · How to make file downloadable from server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found