天天看點

java中文亂碼decode_Javamail中的常見中文亂碼問題與解決辦法(綜合)

接收郵件時,擷取某些郵件發送程式發送的email位址,發送位址顯示為亂碼

public static String getFrom(Message msg){

String from="";

try{

if(msg.getFrom()[0]!=null)

from=msg.getFrom()[0].toString();

if(from.startsWith("=?GB")||from.startsWith("=?gb")){

from=MimeUtility.decodeText(from);

}else{

from=StringUtils.toChinese(from);

}

}catch(Exception e){

e.printStackTrace();

}

return from;

}

StringUtils類方法

public static String toChinese(String strvalue){

try{

if(strvalue==null)

return null;

else{

strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");

return strvalue;

}

}catch(Exception e){

return null;

}

}

public static String getFromBASE64(String s) {

if (s == null)

return null;

try {

byte[] b = Base64.decodeBase64(s);

return new String(b);

} catch (Exception e) {

return null;

}

}