Convert InputStream to base64 string(将 InputStream 转换为 base64 字符串)
问题描述
有一种方法可以将 InputStream 转换为 String,并将其编码为 base64,对吧?
There is a way to convert an InputStream to a String, and encode it to base64, right?
在我的函数中,我得到了 InputStream 参数,需要将它插入到我的 Oracle 数据库表中的 BLOB 字段中.
In my function, I get InputStream parameter, and need to insert it into the BLOB field in my Oracle database table.
有没有办法做到这一点?
Is there a way to do that?
(我的数据库对象包含用于保存图像的字符串字段,但我没有找到任何方法将 InputStream 转换为 base 64 格式的字符串.)
(My database object contains string field to save the image, but I don't find any way to convert the InputStream to string in base 64 format.)
推荐答案
您可以使用 Base64 API 尝试类似的操作.
You can try something like this using the Base64 API.
InputStream finput = new FileInputStream(file);
byte[] imageBytes = new byte[(int)file.length()];
finput.read(imageBytes, 0, imageBytes.length);
finput.close();
String imageStr = Base64.encodeBase64String(imageBytes);
使用这个:
http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/org/apache/commons/codec/binary/Base64.html
这篇关于将 InputStream 转换为 base64 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 InputStream 转换为 base64 字符串
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
