java将inputStream转换为base64字符串

java将inputStream转换为base64字符串

问题描述:

有一种方法可以将inputStream转换为String,并将其编码为base64,对吧?
在我的函数中,我得到了InputStream参数,需要将它插入到我的Oracle数据库表的BLOB字段中。有没有办法做到这一点? (我的数据库对象包含保存图像的字符串字段,但我找不到任何方法将inputStream转换为base64格式的字符串)
谢谢!

There is a way to convert inputStream to a String, and encode it to base64, right? In my function, I get InputStream param, and need to insert it into the BLOB field in my Oracle database table. Is there a way to do that? (my database object contains string field to save the image, but I don't find any way to convert the inputStream to string in base64 format) Thank you!

您可以使用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