使用JavaMail阅读阿拉伯语附件 [英] Reading Arabic Attachments using JavaMail

查看:87
本文介绍了使用JavaMail阅读阿拉伯语附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到使用java邮件下载阿拉伯语附件的问题。

I've a problem of downloading arabic attachment files using java mail.

文件名总是不明确的。

问题是 Bodypart 将附件检索为非UTF-8字符。

The problem is the Bodypart retrieves the attachment as non-UTF-8 characters.

private void getAttachments(Message temp) throws IOException, MessagingException {
    List<File> attachments = new ArrayList<File>();

    Multipart multipart = (Multipart) temp.getContent();

    System.out.println(multipart.getCount());

    for (int i = 0; i < multipart.getCount(); i++) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
            continue; // dealing with attachments only
        }
        InputStream is = bodyPart.getInputStream();

        // getFilename always have wrong characters set 
        byte [] fileBytes = bodyPart.getFileName().toString().getBytes();

        String filename = new String(fileBytes, "UTF-8");            

        File f = new File("C:\\Attachments\\" + filename);

         System.out.println(f .getName());

         try {
        if (f == null) {
            //filename = File.createTempFile("VSX", ".out").getName();
            return;
        }

        FileOutputStream fos = new FileOutputStream(f );
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        BufferedInputStream bis = new BufferedInputStream(is);

        int aByte;
        while ((aByte = bis.read()) >=0) {
            bos.write(aByte);
        }

        fos.flush();
        bos.flush();
        bos.close();
        bis.close();
        fos.close();
    } // end of try()
    catch (IOException exp) {
        System.out.println("IOException:" + exp);
    }        

        attachments.add(f);
    }
}


推荐答案

标头是根据RFC 2047中描述的机制(它是 encoded-word )编码的,它表示标题的一部分匹配 =?<编码> B <?编码字节> ?= 是一个字节编码的部分。 < encoding> 说明如何解释字节,并且(因为它是 B 样式,而不是 Q style)< encoded-bytes> 是base-64编码的。

The header is encoded according to the mechanism described in RFC 2047 (it's the encoded-word) which says that a section of a header matching =?<encoding>?B?<encoded-bytes>?= is a byte-encoded section. The <encoding> says how to interpret the bytes, and (because it's the B style, not the Q style) the <encoded-bytes> are base-64 encoded.

这一切都相当复杂。幸运的是,您可以使用静态 javax.mail.internet.MimeUtility.decodeText()方法轻松处理此问题。这意味着您可以切换到:

This is all rather complex. Luckily, you can deal with this easily by using the static javax.mail.internet.MimeUtility.decodeText() method. That means you can switch to this:

String filename = MimeUtility.decodeText(bodyPart.getFileName());

实际上,你最好将它与下一行结合起来:

Actually, you're better off combining that with the next line too as well:

File f = new File("C:\\Attachments",
                  MimeUtility.decodeText(bodyPart.getFileName()));

它更好,因为它避免了构建文件名的麻烦,而不是手动完成所有操作。 (这也意味着您可以将该文字路径名分解到某个配置位置。)

It's better because it avoids more trouble with building filenames than trying to do it all by hand. (It also means that you can factor out that literal pathname into some configuration location.)

这篇关于使用JavaMail阅读阿拉伯语附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆