當(dāng)使用 getContent() 時(shí)炸客,文本主體部分(例如主類(lèi)型是“text”的主體部分)返回 Unicode 字符串對(duì)象。通常打毛,這些主體部分內(nèi)部用某些非 Unicode 字符集保持文本數(shù)據(jù)速蕊。JavaMail(通過(guò) "text/plain" DataContentHandler)試著將那些數(shù)據(jù)轉(zhuǎn)換成 Unicode 字符串」猿穑基礎(chǔ) JDK 的字符集可用于做這種工作憾儒。如果 JDK 不支持特別的字符集,那么就引發(fā) UnsupportedEncodingException乃沙。在這種情況下起趾,可以使用 getInputStream() 方法來(lái)將內(nèi)容作為字節(jié)流檢索。下面是一個(gè)例子:
String s;
if (part.isMimeType("text/plain")) {
try {
s = part.getContent();
} catch (UnsupportedEncodingException uex) {
InputStream is = part.getInputStream();
/*
* Read the input stream into a byte array.
* Choose a charset in some heuristic manner, use
* that charset in the java.lang.String constructor
* to convert the byte array into a String.
*/
s = convert_to_string(is);
} catch (Exception ex) {
// Handle other exceptions appropriately
}
}