回车解码的问题从Java Base64和发送到浏览器 [英] Carriage return issue decoding Base64 from Java and sending to browser
问题描述
我有了调整和连接codeD图像插入的base64一个servlet。我带code像这样
I have a servlet that has resized and encoded an image into base64. I encode it like this
BufferedImage newBuf = .. a bufferedImage...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, sImgFormat, baos);
baos.flush();
imageInBytes = baos.toByteArray();
我再带code到这一点的base64发送给这样的浏览器
I then encode this into base64 to send to the browser like this
sun.misc.BASE64Encoder encoder = new BASE64Encoder();
String sEncImage = "data:image/jpg;base64," + encoder.encodeBuffer(imageInBytes);
浏览器将接收到的编码和它的作品除了回车(\\ n),它会破坏形象在字符串中嵌入一致。当我删除回车图像罚款。有没有一种方法来生成不回车的编码。或者我必须将它寄回之前筛选出来自己吗?
The browser will receive the encoding and it works except for the carriage returns, ("\n") embedded consistently within the string which corrupts the image. When I remove the carriage returns the image is fine. Is there a way to generate the encoding without the carriage returns. Or must I filter it out myself before sending it back ?
(我使用J2SE 1.4.2,需要继续这样做)
(I am using J2SE 1.4.2 and need to continue to do so)
推荐答案
我怀疑sun.misc.Base64en coder是分块的输出。
我不会用sun.misc类,因为它限制了你的code到Oracle的JVM(例如,它会在IBM的Websphere工作)。我会使用公地<一个href=\"http://commons.apache.org/$c$cc/apidocs/index.html?org/apache/commons/$c$cc/binary/Base64OutputStream.html\"相对=nofollow> Base64的 EN codeR或<一个href=\"http://commons.apache.org/$c$cc/apidocs/org/apache/commons/$c$cc/binary/Base64OutputStream.html\"相对=nofollow> Base64OutputStream 。
I suspect that the sun.misc.Base64encoder is chunking the output. I wouldn't use sun.misc classes as it restricts your code to Oracle JVMs (for example, it would work in IBM Websphere). I'd use the commons Base64 encoder or Base64OutputStream.
这篇关于回车解码的问题从Java Base64和发送到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!