Last active
May 14, 2024 19:44
-
-
Save ansidev/5816b8b3108c30b5279f5fec67506798 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Thanks a lot, this worked like a charm. I was looking for solution for hours.
Wow Lot of thanks , You Rock!
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
of course not, here is the reason
spring-projects/spring-framework#18147