java convert inputStream to base64 string

前端 未结 4 618
说谎
说谎 2021-02-07 01:47

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 Oracl

4条回答
  •  被撕碎了的回忆
    2021-02-07 02:48

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

提交回复
热议问题