如何使用数据URI从图片作为InputStream? [英] How to use Data URI from Image as an InputStream?

查看:171
本文介绍了如何使用数据URI从图片作为InputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经取回从HTML5画布中的Base64数据URI。在我的servlet,我想取消code中的数据URI,并使用它作为一个输入流如图XXX的下方。下面的代码是我在HTML5画布图像上传到我的Facebook帐户。我使用restfb。

I have retrieved the base64 data uri from a html5 canvas. Within my servlet, I would like to decode the data uri and use it as an input stream as shown in "xxx" below. The following coding is for me to post the image in the html5 canvas into my facebook account. I am using restfb.

FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
BinaryAttachment.with("test.jpeg", getClass().getResourceAsStream("xxx")),
Parameter.with("message", "Test"));

我怎样才能做到这一点?谢谢你。

How can I achieve that? Thanks.

更新越来越近,但仍然没有工作!

Updated Getting closer but still not working!

在我的jsp:

var d = document.getElementById('img').src;
window.location.href = "upload?src=" + d;

在我的servlet:

In my servlet:

String d = req.getParameter("src");
String head = "data:image/jpeg;base64,";
String base64 = d.substring(head.length()-1);

byte[] buf = DatatypeConverter.parseBase64Binary(base64);
ByteArrayInputStream is = new ByteArrayInputStream(buf);

FacebookType publishPhotoResponse = facebookClient.publish("me/photos", FacebookType.class,
BinaryAttachment.with("test.jpeg", is),
Parameter.with("message", "Test"));

有没有我的编码错误,因为它似乎在servlet内的某处打错误。因为它是在服务器上运行,我不能查看错误。

Is there any errors in my coding as it seems to hit error somewhere within the servlet. I can't view the errors as it is running on a server.

推荐答案

这需要几乎完全相反,以这个答案 !或者至少,它的相反。它回答图片基地64 字符串,而这个用例是字符串图片

This needs almost the exact opposite to this answer! Or at least, the reverse of it. It answers Image to base 64 String, whereas this use-case is String to Image.

查找到<一个href=\"http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html#parseBase64Binary%28java.lang.String%29\"相对=nofollow> javax.xml.bind.DatatypeConverter.parseBase64Binary(字符串) 获得字节[] 字符串

使用字节[] 来构建的 ByteArrayInputStream的

Use the byte[] to construct a ByteArrayInputStream.

这篇关于如何使用数据URI从图片作为InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆