Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

352
Visualizações
Convert InputStream to base64 string

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

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Trujillo Relatório

0

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.

about 4 years ago · Santiago Trujillo Relatório

0

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();
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda