如何使用Image中的Data URI作为InputStream?

Sky*_*Sky 5 java base64 facebook canvas image

我从html5画布中检索了base64数据uri.在我的servlet中,我想解码数据uri并将其用作输入流,如下面的"xxx"所示.以下编码是我将html5画布中的图像发布到我的Facebook帐户中.我正在使用restfb.

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

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

更新越来越近但仍然无法正常工作!

在我的jsp中:

var d = document.getElementById('img').src;
window.location.href = "upload?src=" + d;
Run Code Online (Sandbox Code Playgroud)

在我的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"));
Run Code Online (Sandbox Code Playgroud)

我的编码中是否有任何错误,因为它似乎在servlet中的某处出现错误.我无法查看在服务器上运行的错误.

And*_*son 3

这需要与这个答案几乎完全相反!或者至少是相反的情况。它响应Image基数 64 String,而此用例是String响应Image.

寻找其中的javax.xml.bind.DatatypeConverter.parseBase64Binary(String)一个。byte[]String

使用byte[]来构造一个ByteArrayInputStream.