2011-10-28 27 views
6
發佈字節數組

目標是什麼?發佈一個包含base64編碼的byte []數組的JSON是一個有效的替代方案嗎?如何通過RestTemplate

回答

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

相關問題