How to send an image to a Javascript client using

2019-05-21 19:13发布

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);
 }

2条回答
孤傲高冷的网名
2楼-- · 2019-05-21 19:41

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

查看更多
地球回转人心会变
3楼-- · 2019-05-21 20:06

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.

查看更多
登录 后发表回答