How to use Data URI from Image as an InputStream?(如何使用 Image 中的 Data URI 作为 InputStream?)
问题描述
我已从 html5 画布中检索到 base64 数据 uri.在我的 servlet 中,我想解码数据 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.
推荐答案
这需要与this answer几乎完全相反!或者至少,它的反面.它将 Image 回答为 base 64 String,而这个用例是 String 到 Image.
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.
查看 javax.xml.bind.DatatypeConverter.parseBase64Binary(String) 得到Stringbyte[]>.
Look to javax.xml.bind.DatatypeConverter.parseBase64Binary(String) to get a byte[] of the String.
使用 byte[] 构造一个 ByteArrayInputStream.
Use the byte[] to construct a ByteArrayInputStream.
这篇关于如何使用 Image 中的 Data URI 作为 InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Image 中的 Data URI 作为 InputStream?
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
