【java代码段】判段字符串的编码格式并转为UTF-8的编码格式

//判断字符串的编码格式	 
public static String getEncoding(String str) {               
	String encode = "GB2312";              
	try {                 
		 if (str.equals(new String(str.getBytes(encode), encode))) {                       
		 	String s = encode;                      
		 	return s;                 
		  }              
	} catch (Exception exception) {
	}            
	   
	encode = "ISO-8859-1";              
	try {                  
		if (str.equals(new String(str.getBytes(encode), encode))) {                       
			String s1 = encode;                      
			return s1;                   
		}               
	} catch (Exception exception1) { 
	}    
	           
	encode = "UTF-8";              
	try {                  
		if (str.equals(new String(str.getBytes(encode), encode))) {                       
			String s2 = encode;                      
			return s2;                   
		}               
	} catch (Exception exception2) { 
	}        
	       
	encode = "GBK";              
	try {                  
		if (str.equals(new String(str.getBytes(encode), encode))) {                      
			String s3 = encode;                     
			return s3;                   
		}               
	} catch (Exception exception3) { 
	}           
	return "";           
} 

//将不同编码格式转为UTF-8显示出来	
public static String toChinese(String strvalue, String encodeFormat) {		
	try {			
		if (strvalue == null) {				
			return "";			
		} else {				
			strvalue = new String(strvalue.getBytes(encodeFormat), "UTF-8").trim();					return strvalue;			
		}		
	} catch (Exception e) {			
		return "";		
	}	
}

你可能感兴趣的:(【java代码段】判段字符串的编码格式并转为UTF-8的编码格式)