2011-10-28 7 views
6
를 통해

목표를 바이트 배열을 게시하려면 : 포스트의 이미지를 어떤 대안이어떻게 RestTemplate

MultiValueMap<String, Object> parts = new 
LinkedMultiValueMap<String, Object>(); 
parts.add("field 1", "value 1"); 
parts.add("file", new 
ClassPathResource("myFile.jpg")); 
template.postForLocation("http://example.com/myFileUpload", parts); 

의 변화가 있습니까 사용하여 현재

RestTemplate

를 사용하고 계십니까? 유효한 대안으로 base64로 인코딩 된 바이트 [] 배열을 포함하는 JSON을 게시하고 있습니까?

답변

3

비트 맵을 바이트 배열로 바꾼 다음 Base64로 인코딩 한 다음 Jackson을 내 직렬기로 사용하여 RestTemplate을 통해 전송합니다.

8

그래,이 같은과 이미지가 페이로드의 경우 나는

생각하고 헤더를 조정할하려는 경우 당신은 이런 식으로 게시 할 수 있습니다 : 그렇지 않으면

HttpHeaders headers = new HttpHeaders(); 
headers.set("Content-Type", "image/jpeg"); 
InputStream in = new ClassPathResource("myFile.jpg").getInputStream(); 

HttpEntity<byte[]> entity = new HttpEntity<>(IOUtils.toByteArray(in), headers); 
template.exchange("http://example.com/myFileUpload", HttpMethod.POST, entity , String.class); 

:

InputStream in = new ClassPathResource("myFile.jpg").getInputStream(); 
HttpEntity<byte[]> entity = new HttpEntity<>(IOUtils.toByteArray(in)); 
template.postForEntity("http://example.com/myFileUpload", entity, String.class); 
+0

코드에 감사드립니다.이 모든 것이 어떻게 작동하는지 이해하게되었습니다. – eladyanai

관련 문제