简体   繁体   English

JAVA-getBytes返回不带BOM的UTF-8

[英]JAVA - getBytes return UTF-8 without BOM

I send a txt file by email in this way. 我以这种方式通过电子邮件发送txt文件。 File's data is in a StringBuffer. 文件的数据在StringBuffer中。

File construction : 文件构造:

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")));

Adding the file in an email : 在电子邮件中添加文件:

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

The file I receive by email is by default open as "UTF-8 without BOM" in notepad++, or in excel (who does not recognize accents). 默认情况下,我通过电子邮件收到的文件在记事本++或excel(无法识别口音)中以“没有BOM的UTF-8”打开。

So I need to open with excel, so to have UTF-8 with BOM encoding. 因此,我需要使用excel打开,以便具有带有BOM编码的UTF-8。

Do you have any suggestions ? 你有什么建议吗 ?

最后,我使用.CSV扩展名和ISO-8859-1,效果很好!

It is not convenient use the BOM. 使用BOM表不方便。 You can put the encoding of the mail like this: 您可以像下面这样放置邮件的编码:

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

This will recognize the accents 这将识别口音

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM