【发布时间】:2015-08-27 13:39:00
【问题描述】:
我以这种方式通过电子邮件发送一个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);
我通过电子邮件收到的文件在 notepad++ 或 excel(不识别重音符号)中默认打开为“UTF-8 without BOM”。
所以我需要用excel打开,所以要有带有BOM编码的UTF-8。
你有什么建议吗?
【问题讨论】:
-
您可以添加 BOM,但默认行为是不包含它。大多数工具在使用 UTF-8 编码时不会编写 BOM。您需要将 notepad++ 设置为默认读取 UTF-8。
标签: java excel utf-8 character-encoding