JAVA - getBytes返回没有BOM的UTF-8

时间:2015-08-27 13:39:00

标签: java excel utf-8 character-encoding

我以这种方式通过电子邮件发送txt文件。文件的数据在StringBuffer中。

档案构建:

StringBuffer lBufferFinal = new StringBuffer();
// Put some text in lBufferFinal
FichierByteVO lFichier = new FichierByteVO();
    lFichier.setFileName("Statistic.txt");
    lFichier.setMIMEType("text/plain");
    lFichier.setFile(lBufferFinal.toString().getBytes(Charset.forName("UTF-8")));

在电子邮件中添加文件:

MimeMessage lMessage = new MimeMessage(lSession);
// Do thing tu put sender, receiver, subject etc to the MimeMessage
Multipart = new MimeMultipart();
BodyPart lMessageBodyPart = new MimeBodyPart();
lMessageBodyPart.setText("Here is an email !");
lMultipart.addBodyPart(lMessageBodyPart);
lMessageBodyPart = new MimeBodyPart();
DataHandler lDataHandler = new DataHandler(new ByteArrayDataSource(lFichier.getFile(), lFichier.getMIMEType()));
        lMessageBodyPart.setDataHandler(lDataHandler);
                    lMessageBodyPart.setFileName(lFichier.getFileName());
        lMultipart.addBodyPart(lMessageBodyPart);
    }
    lMessage.setContent(lMultipart);

我通过电子邮件收到的文件默认打开为" UTF-8没有BOM"在notepad ++中,或在excel中(不识别重音符号)。

所以我需要用excel打开,所以要使用BOM编码的UTF-8。

你有什么建议吗?

2 个答案:

答案 0 :(得分:1)

最后,我使用.CSV扩展名和ISO-8859-1,它运行良好!

答案 1 :(得分:0)

使用BOM不方便。您可以像这样输入邮件的编码:

JavaMailSenderImpl mailer = new JavaMailSenderImpl();
...
mailer.setDefaultEncoding("ISO-8859-1");

这将识别重音

相关问题