1

How to consume an API which accepts only MultipartFile in spring boot? Have to pass a file from Local Server (from some specific path)

@FeignClient(name = "abc-file-upload",url ="https://abc.xyz.app")
public interface ABCFeignClient {

    String AUTH_TOKEN = "Authorization";
    @PostMapping(value = "/api/v1/upload",consumes = "multipart/form-data")
    UploadFileResponse uploadFile(@RequestBody MultipartFile multipartFile, @RequestHeader(AUTH_TOKEN) String api_key);
}

Now I have to call this client method and have to pass a local file from a specific location like (/Users/pradeepkumar/Desktop/File_Download/file1.jpg)

How to get MultipartFile Object for a local file?

tuhin47
  • 5,172
  • 4
  • 19
  • 29
  • Maybe that could help https://stackoverflow.com/questions/18381928/how-to-convert-byte-array-to-multipartfile I guess you need to implement MultipartFile interface. In this implementation you will provide the file name and the data stream for it. – Alexey Usharovski Feb 11 '21 at 14:01
  • Thanks Alexey Usharovski for your reply, as the solution you have suggested not worked for me, but I found the solution from https://stackoverflow.com/questions/1378920/how-can-i-make-a-multipart-form-data-post-request-using-java and It's worked.. – Pradeep Kumar Feb 14 '21 at 14:37

1 Answers1

0

First of all, MultiPartFile is an interface and we can create an object of its multiple implementations like below,

File file = new File("/Users/pradeepkumar/Desktop/File_Download/file1.jpg");

Once you have File object of your local file,

final MultipartFile multipartFile = new MockMultipartFile(filename, filename, mimeType, FileUtils.readFileToByteArray(file));

Also, you can create your own implementation of this interface. Just override all methods and return expected data from all overridden methods.

Brijesh Wani
  • 41
  • 1
  • 7