Implement Base64 carriage return problem with Java and send to browser

I have a servlet that resized and encoded an image in base64. I encode it like this:

BufferedImage newBuf = .. a bufferedImage...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, sImgFormat, baos);
baos.flush();
imageInBytes = baos.toByteArray();

Then I encode this in base64 to send to a browser like this

sun.misc.BASE64Encoder encoder = new BASE64Encoder();
String sEncImage = "data:image/jpg;base64," + encoder.encodeBuffer(imageInBytes);

The browser will receive the encoding, and it works, except for carriage returns ("\ n"), sequentially embedded in a line that distorts the image. When I remove the carriage, the image will be beautiful. Is there a way to generate an encoding without carriage return. Or should I filter it before sending?

(I am using J2SE 1.4.2 and should continue to do this)

+5
source share
1 answer

, sun.misc.Base64encoder - . sun.misc, JVM Oracle (, IBM Websphere). Base64 encoder Base64OutputStream.

+4

All Articles