我需要在utf8中保存电子邮件附件。我尝试此代码,但仍然缺少一些字符:

public static void main(String args[]) throws Exception {
    File emlFile = new File("example.eml");

    InputStream source;

    source = new FileInputStream(emlFile);

    MimeMessage message = new MimeMessage(null, source);

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

    for (int x = 0; x < multipart.getCount(); x++) {

        BodyPart bodyPart = multipart.getBodyPart(x);
        String disposition = bodyPart.getDisposition();

        if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
            System.out.println("Mail have some attachment : ");

            DataHandler handler = bodyPart.getDataHandler();
            System.out.println("file name : " + handler.getName());


            //start reading inpustream from attachment
            InputStream is = bodyPart.getInputStream();
            File f = new File(bodyPart.getFileName());
            OutputStreamWriter sout = new OutputStreamWriter(new FileOutputStream(f), "UTF8");
            BufferedWriter buff_out = new BufferedWriter(sout);
            int bytesRead;
            while ((bytesRead = is.read()) != -1) {
                buff_out.write(bytesRead);
            }
            buff_out.close();

        }
    }
}

最佳答案

您正在从附件读取字节,忽略任何编码,并将字符输出到文件。您很可能会选择要么不将两者混合使用。

如果附件包含原始字节,则对输出进行UTF编码是没有意义的,您可以使用原始流。

如果包含文本,则还需要将附件读取为文本而不是原始字节,并使用编码进行读取和写入。

在后一种情况下,类似;

InputStream is = bodyPart.getInputStream();
InputStreamReader sin = new InputStreamReader(is,
                                              "UTF8"); // <-- attachment charset

File f = new File(bodyPart.getFileName());
OutputStreamWriter sout = new OutputStreamWriter(new FileOutputStream(f), "UTF8");
BufferedReader buff_in = new BufferedReader(sin);
BufferedWriter buff_out = new BufferedWriter(sout);

int charRead;
while ((charRead = buff_in.read()) != -1) {
    buff_out.write(charRead);
}

buff_in.close();
buff_out.close();

07-25 23:51
查看更多