Javamail base64解码

时间:2013-01-15 11:37:54

标签: base64 javamail

当我收到消息时,我使用javamail来获取消息: com.sun.mail.util.BASE64DecoderStream,

我知道这是多部分消息的一部分,在我有消息的来源

Content-Type: image/png; name=index_01.png

Content-Transfer-Encoding:base64

如何编码此消息??

编辑: 我有那个代码:

else if (mbp.getContent() instanceof BASE64DecoderStream){
                        InputStream is = null;
                        ByteArrayOutputStream os = null;

                            is = mbp.getInputStream();
                            os = new ByteArrayOutputStream(512);
                            int c = 0;
                            while ((c = is.read()) != -1) {
                                os.write(c);
                            }



                            System.out.println(os.toString()); 

                    }

该代码返回奇怪的字符串,例如: RA?的EXIF II * ???????????? E'鸭子??????? A)

3 个答案:

答案 0 :(得分:0)

Sun的base 64编码器位于可选包中,可以随时移动或重命名而不会发出警告,也可能在其他Java运行时中丢失,也可能禁用对这些包的访问。最好不要依赖它。

我想说,使用来自Base64Apache Commons,应该这样做。希望您能重建并修复源代码。

答案 1 :(得分:0)

当您阅读图像部分的内容时,您期待什么?图像以编码格式存储在消息中,但JavaMail在将字节返回给您之前解码数据。如果将字节存储在文件中,则可以使用许多图像查看/编辑应用程序显示图像。如果要使用Java程序显示它们,则需要使用java.awt.image包中的(例如)API将字节转换为适当的Java Image对象。

答案 2 :(得分:0)

com.sun.mail.util.BASE64DecoderStream取决于平台。您不能依赖于始终是处理base64解码的类的类型。

相反,javamail API已经支持解码:

// part is instanceof javax.mail.Part
ByteArrayOutputStream bos = new ByteArrayOutputStream();
part.getDataHandler().writeTo(bos);

String decodedContent = bos.toString()