如何将图像发送到使用JSON从Java服务器端的Javascript(How to send an

2019-09-19 00:00发布

我工作的谷歌联系人API和我收到的所有数据和发送的字符串JSON(JavaScript)的,但是当我从通讯录中获得的图像,我可以接收图像。 我怎么可以把它送到JSON? 如何发送的图像文件的URL? (我可以用路标?)

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

Answer 1:

如果你想发送编码为使用JSON数据的实际图像,你可以发送HTML img标签与src包含编码的图像数据,像这样的属性:

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

浏览器支持列表(包括Android浏览器和iOS的Safari:

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



Answer 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.



文章来源: How to send an image to a Javascript client using JSON from a Java server