我需要将电子邮件附件保存在 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();
}
}
}