java将inputStream转换为base64字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42667942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:17:50  来源:igfitidea点击:

java convert inputStream to base64 string

javastringbase64inputstream

提问by foo

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!

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

采纳答案by mhasan

You can try something like this using the Base64 API.

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

Use this:

用这个:

http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/org/apache/commons/codec/binary/Base64.html

http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/org/apache/commons/codec/binary/Base64.html

回答by Alboz

The simplest way would be to use IOUtilsfrom apache-commons to do that:

最简单的方法是使用来自 apache-commons 的IOUtils来做到这一点:

String result= IOUtils.toString(inputStream, ENCODING); 

From the documentation:

从文档:

toString(byte[] input, String encoding) Gets the contents of a byte[] as a String using the specified character encoding.

toString(byte[] input, String encoding) 使用指定的字符编码以字符串形式获取 byte[] 的内容。

After that To Encode/Decode in Base64:

之后在 Base64 中编码/解码:

// Encode 
String resultBase64Encoded = Base64.getEncoder().encodeToString(result.getBytes("utf-8"));


// Decode
byte[] asBytes = Base64.getDecoder().decode(resultBase64Encoded);
String resultAsStringAgain= String(asBytes, "utf-8")

Note:I'm assuming you use JDK 8 for the Encode/Decode part.

注意:我假设您将 JDK 8 用于编码/解码部分。

Apparently the OP wants just to persist an InputStream to the DB. You can do that directly using JDBC:

显然,OP 只想将 InputStream 持久化到数据库。您可以直接使用 JDBC 执行此操作:

InputStream inputStream = ......;
String sql = "INSERT INTO TABLE_NAME(COLUMN_NAME) values (?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setBlob(1, inputStream);
statement.executeUpdate();

回答by f1sh

As I mentioned, you shouldn't use Stringfor binary data. The base64-encoded data can be stored as a String though. But since your database column is a blob, I would continue to work with a byte[].

正如我提到的,你不应该使用String二进制数据。不过,base64 编码的数据可以存储为字符串。但是由于您的数据库列是一个 blob,我将继续使用byte[].

Use IOUtils to get a byte[]from the InputStream:

使用 IOUtilsbyte[]从以下获取InputStream

byte[] bytes = IOUtils.toByteArray(yourInputStream);
byte[] encoded = java.util.Base64.getEncoder().encode(bytes);

Then write it to the database. Writing a blob using jdbc and a PreparedStatement looks like this:

然后将其写入数据库。使用 jdbc 和 PreparedStatement 编写 blob 如下所示:

yourPreparedStatement.setBytes(nIndex, encoded);

回答by Raj S. Rusia

There is nice way to do this is using IOUtilsto convert the InputStream into a Byte Array...

有一个很好的方法是使用 IOUtils将 InputStream 转换为Byte Array...

something like

就像是

    InputStream is;
    byte[] bytes = IOUtils.toByteArray(is);

Here you can use Base64to convert Byte Arrayto String.

在这里您可以使用Base64转换Byte ArrayString.

Sample Code

示例代码

    String encoded = Base64.getEncoder().encodeToString(bytes);

Now you can use your String.

现在您可以使用您的String.