【问题标题】:Javamail base64 decodingJavamail base64 解码
【发布时间】:2012-12-29 12:02:32
【问题描述】:

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

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

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

内容传输编码: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()); 

                    }

并且该代码返回奇怪的字符串,例如: Ř˙á?Exif??II*????????????˙ě?Ducky???????˙á)

【问题讨论】:

    标签: base64 jakarta-mail


    【解决方案1】:

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

    我会说,改用Apache Commons 中的Base64,应该这样做。希望你能重建和修复源代码。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2012-02-25
          • 1970-01-01
          • 1970-01-01
          • 2016-02-19
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多