文本文件编码为UTF_8? [英] Text file encoding to UTF_8?

查看:194
本文介绍了文本文件编码为UTF_8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个发送电子邮件附件的Java项目。




在我的测试用例中,我添加了一些日语单词一些Hiraganas和Katakanas到我附加的testfile.txt(我以UTF-8编码保存。 )




但是当我发送测试邮件给自己时,在打开附件testfile.txt之后,每个日语Chars都变成????



所以我只是想知道为什么会发生这种情况?



谢谢



Allan



PS更具体来说,这里是我的代码。
我正在使用mail.jar发送电子邮件。



这是我如何获取文件:

  / ** 
*向电子邮件添加附件。
* @param filePath
* /
public void setFile(String filePath){

attachment = new File(filePath);

}

以下是我将文件附加到我的MIME邮箱中部分。

  / *如果给出附件,请添加附件。* / 
if(attachment!= null){
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(attachment);
multipart.addBodyPart(attachmentPart);
}


解决方案

你需要确保你'使用适当的字符集读取和写入文件。



因此不是这样,这将使用平台的默认字符集:

  Reader reader = new FileReader(/ testfile.txt); 
// ...

但更重要的是,使用 InputStreamReader 其中明确指定正确的字符集:

  Reader reader = new InputStreamReader(new FileInputStream(/ testfile.txt),UTF- 8\" ); 
// ...

另外,在 Content-键入您必须设置字符集属性的电子邮件附件的标题,您必须使用UTF-8写出附件。由于您使用的邮件API不清楚,因此无法给出更多详细信息。或者,您也可以坚持使用 InputStream / OutputStream ,因为将流内容作为纯字节,因此将不要影响字节代表的字符集。






更新:您正在使用Javamail的< a href =http://download.oracle.com/javaee/6/api/javax/mail/internet/MimeBodyPart.html =nofollow> MimeBodyPart ,而不显式指定具有charset属性的内容类型。现在,您依赖邮件客户端是否将内容视为UTF-8。修正如下:

  MimeBodyPart attachmentPart = new MimeBodyPart(); 
attachmentPart.attachFile(attachment);
attachmentPart.setHeader(Content-Type,text / plain; charset = utf-8);
multipart.addBodyPart(attachmentPart);


I'm writing a Java project sending email with attachment.

In my test case, I add some Japanese words "some Hiraganas and Katakanas" to my attached testfile.txt (which I saved in UTF-8 encoding.)

But when I send my test email to myself, after I opened the attached testfile.txt, every Japanese Chars turns to be "????".

So I'm just wondering why this happens...?

Thank you

Allan

P.S. to be more specific, here is my code. I am using mail.jar to send email.

Here is how I get the file:

/**
 * Add an attachment to the Email.
 * @param filePath
 */
public void setFile(String filePath){

    attachment = new File(filePath);

}

and below is how I attach the file into my MIME email part.

/*Add attachment if an attachment is given.*/
    if(attachment != null){
    MimeBodyPart attachmentPart = new MimeBodyPart();
    attachmentPart.attachFile(attachment);
    multipart.addBodyPart(attachmentPart);
    }

解决方案

You need to ensure that you're reading and writing the file using the proper charset.

I.e. thus not so, which would use platform's default charset:

Reader reader = new FileReader("/testfile.txt");
// ...

But more so, using InputStreamReader wherein you explicitly specify the proper charset:

Reader reader = new InputStreamReader(new FileInputStream("/testfile.txt"), "UTF-8");
// ...

Also, in the Content-Type header of the email attachment you have to set the charset attribute and you have to write out the attachment using UTF-8. Further detail can't be given as it's unclear what mail API you're using. Alternatively, you can also stick to using InputStream/OutputStream only as that would stream the content as pure bytes and thus wouldn't affect the charset the bytes represent.


Update: you're using Javamail's MimeBodyPart without explicitly specifying the content type with the charset attribute. Now you're dependent on the mail client whether it treats the content as UTF-8 or not. Fix it as follows:

MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(attachment);
attachmentPart.setHeader("Content-Type", "text/plain;charset=utf-8");
multipart.addBodyPart(attachmentPart);

这篇关于文本文件编码为UTF_8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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