How to send an image to a Javascript client using JSON from a Java server

廉价感情. 提交于 2019-12-22 11:29:29

问题


I am working on Google Contacts API and I received all data and sending as string to JSON (javascript) but when I get an image from contacts I can receive image. How can I send it to JSON? How can send the image file to a URL? (Can I use signpost?)

if (photoLink.getEtag() != null) {
 GDataRequest request = myService.createLinkQueryRequest(photoLink);
 request.execute();                                                                                    
 // No Authentication header information  
 InputStream stream = request.getResponseStream();    
 Image image=  ImageIO.read(stream);
 }

回答1:


If you're trying to send the actual image encoded as data using JSON, you can just send an HTML img tag with the src attribute containing the encoded image data, like so:

<img src="data:image/png;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
/ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7">

Browser Support List (Includes Android Browser & iOS Safari:

http://en.wikipedia.org/wiki/Data_URI_scheme#Web_browser_support




回答2:


You should simply use a servlet for sending images and just send some url variable in JSON. (like ?pic_id=034enifuwbf0329 .

You could use inline image, but that would fail miserably on certain IE versions, while the other solution is browser-agnostic.



来源:https://stackoverflow.com/questions/11302863/how-to-send-an-image-to-a-javascript-client-using-json-from-a-java-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!