Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ansidev/5816b8b3108c30b5279f5fec67506798 to your computer and use it in GitHub Desktop.
Save ansidev/5816b8b3108c30b5279f5fec67506798 to your computer and use it in GitHub Desktop.
package studio.safe.spring.resttemplate;
import java.io.File;
import java.io.IOException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String[] args) throws IOException {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
FileSystemResource value = new FileSystemResource(new File("D://test.png"));
map.add("file", value);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange("http://localhost/api/v1/users/avatar", HttpMethod.POST, requestEntity, String.class);
}
}
@trickstyler
Copy link

I can't even start to tell you how thankful I am for this gist. Been looking for hours for it! thank you!

@happyastronaut
Copy link

How does controller looks like at the server side?

@tomafy
Copy link

tomafy commented Feb 16, 2018

Really helpful!!

@user20161119
Copy link

thanks,why use fileinputstream or byte[] array replace FileSystemResource not wok?

Copy link

ghost commented Apr 23, 2018

Can i use MultipartFile instead of FileSystemResource?

@pooja-ga
Copy link

pooja-ga commented Jul 3, 2018

Thanks. Very Helpful.

@mabonani
Copy link

Thanks. Very Helpful.

@Lnaugsh
Copy link

Lnaugsh commented Dec 28, 2018

Thanks.

@athiththan11
Copy link

Thanks. Very very Helpful.

@lamymay
Copy link

lamymay commented Jan 15, 2020

Can i use MultipartFile instead of FileSystemResource?

of course not, here is the reason
spring-projects/spring-framework#18147

@devAnirudh
Copy link

Thanks a lot, this worked like a charm. I was looking for solution for hours.

@Prakash30577
Copy link

Wow Lot of thanks , You Rock!

@progeek29
Copy link

I want to convert "byte [ ] response to Multipart"?

here is my controller-

@GetMapping("/reportpdf1")
public ResponseEntity<byte[]> pdfReport1( ) {
byte[] response = null;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
String filename = "";
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
filename = "steve.pdf";
headers.setContentDispositionFormData(filename, filename);
response = pdfservice.pdfReport1( );
return new ResponseEntity<>(response,headers, HttpStatus.OK);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment