-1

I want to make an http request using a stream (because I think there is no other way) but I can only send a byte array using streams. I want to send a file by encoding it in Base64 then sending the encoded base64 code so I can decode it later. So far I can easily encode it using

byte[] contentBytes = loadFile(txt);
byte[] encoded = Base64.getEncoder().encode(contentBytes);
String encodedContentString = new String(encoded, StandardCharsets.US_ASCII);

But as one can see I get a string. Can I turn it into a byte array not or can I encode the file into a byte array directly? Maybe I used it wrong but I am pretty sure I can't just use encodedContentStrin.getBytes()

2five
  • 135
  • 1
  • 2
  • 11

1 Answers1

0

You already have the encoded data as a byte array after

byte[] encoded = Base64.getEncoder().encode(contentBytes);

There is no need to convert it to a String.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Oh yeah but, you know, when I run this and then print it out, in this example it prints out `[B@17d99928` and I am fairly certain that this is not text that is base64 encoded – 2five Feb 15 '21 at 01:12
  • You should have included that in your question. This is one of the most-asked questions on Stack Overflow. Please see the duplicate target. – Jim Garrison Feb 15 '21 at 01:17