22

There is a way to convert an InputStream to a String, and encode it to base64, right?

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?

(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.)

Lii
  • 10,777
  • 7
  • 58
  • 79
foo
  • 437
  • 1
  • 7
  • 22
  • 1
    Did you try anything at all? If your InputStream contains image data, don't use a ``String`` though, use ``byte[]``. – f1sh Mar 08 '17 at 09:51
  • Ok, what is it mean? I need to change this field to Byte[]? and how can I convert it to Byte[] from inputStream? thanks! – foo Mar 08 '17 at 09:56

4 Answers4

29

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);

Use this:

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

mhasan
  • 3,595
  • 1
  • 17
  • 35
  • new FileInputStream need to get file - I don't have file name.... I need to set inputStream.... – foo Mar 08 '17 at 10:09
  • that should be ok as long as you have InputStream with you . you use the mentioned logic – mhasan Mar 08 '17 at 10:10
  • Base64.DEFAULT - get compilation error- DEFAULT is not field. what shuld I need to do? – foo Mar 08 '17 at 10:11
28

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

something like

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

Here you can use Base64 to convert Byte Array to String.

Sample Code

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

Now you can use your String.

Raj S. Rusia
  • 690
  • 10
  • 14
  • `IOUtils` is not found, consult this https://stackoverflow.com/questions/24578243/cannot-resolve-symbol-ioutils – joe_inz Oct 12 '21 at 14:38
3

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

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.

After that To Encode/Decode in 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.

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

InputStream inputStream = ......;
String sql = "INSERT INTO TABLE_NAME(COLUMN_NAME) values (?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setBlob(1, inputStream);
statement.executeUpdate();
Alboz
  • 1,725
  • 18
  • 26
  • @f1sh I know that but this is what the the OP asked for. – Alboz Mar 08 '17 at 10:10
  • so what can I do? where do I put the image data before insert it to DB blob? – foo Mar 08 '17 at 10:12
  • @foo If all you want to do is to save an InputStream into the DB you can do it directly without transforming it to anything else... – Alboz Mar 08 '17 at 10:18
  • @foo see me updated answer about How to persist an InputStream as Blob directly to the DB – Alboz Mar 08 '17 at 10:24
  • To mee this is much more memory efficient way to do it, the byte[] in the accepted answer will eat it. You dont need a temp space to persist a stream. – mcvkr Aug 09 '18 at 20:49
2

As I mentioned, you shouldn't use String for 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[].

Use IOUtils to get a byte[] from the 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:

yourPreparedStatement.setBytes(nIndex, encoded);
f1sh
  • 10,458
  • 3
  • 23
  • 49