VICKY-TSC
VICKY-TSC

Reputation: 415

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

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

Upvotes: 2

Views: 4229

Answers (2)

Alex W
Alex W

Reputation: 38173

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

Upvotes: 1

Gergely Szilagyi
Gergely Szilagyi

Reputation: 3903

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.

Upvotes: 0

Related Questions