62,615
社区成员
发帖
与我相关
我的任务
分享
public class MailTest {
public static void main(String[] args) {
String str = "×Ô¶¯»Ø¸´";
char[] chs = str.toCharArray();
for(int i = 0; i < chs.length; i++) {
System.out.println(Integer.toHexString(chs[i]));
}
}
}
String str = new String(new byte[]{ (byte)0xb8, (byte)0xb4 }, "gbk");
System.out.println(str);
import java.io.UnsupportedEncodingException;
import javax.mail.internet.MimeUtility;
public class MailTest {
public static void main(String[] args) throws UnsupportedEncodingException {
String[] strs = {
"hello ×Ô¶¯»Ø¸´:=?gbk?B?1eLA79bQzsTP1Mq+1f2zow==?=",
"hello ×Ô¶¯»Ø¸´:",
"=?gbk?B?1eLA79bQzsTP1Mq+1f2zow==?=" };
for(int i = 0; i < strs.length; i++) {
System.out.println(getSubject(strs[i]));
}
}
public static String getSubject(String headerSubject)
throws UnsupportedEncodingException {
if(headerSubject == null) {
return null;
}
int start = headerSubject.indexOf("=?");
if(start < 0) {
return new String(headerSubject.getBytes("iso8859-1"), "gbk");
}
if(start == 0) {
return MimeUtility.decodeText(headerSubject);
}
String mixed = headerSubject.substring(0, start);
String subject = new String(mixed.getBytes("iso8859-1"), "gbk");
return subject + MimeUtility.decodeText(headerSubject.substring(start));
}
}
System.out.println("subject:"+message.getHeader("subject")[0]);
输出
subject:hello ×Ô¶¯»Ø¸´:
=?gbk?B?1eLA79bQzsTP1Mq+1f2zow==?=
System.out.println("subject:"+MimeUtility.decodeText(message.getHeader("subject")[0]));//一样是乱码