package com.css.common.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* 字符编码的过滤
*
* @version 1.0
*
*/
public class EncodeUtil {
/**
* 默认的URLEncode字符编码
*/
private static String DEFAULT_URL_ENCODE = "GBk";
public static String setEncode(String str) throws Exception {
return setEncode(str,DEFAULT_URL_ENCODE);
}
public static String setEncode(String str, String enc) throws Exception {
try {
return URLEncoder.encode(str, enc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new Exception(EncodeUtil.class.getName() + ":URLEncoder.encode设置编码错误:"+e.getMessage());
}
}
public static String setDecode(String str) throws Exception {
return setDecode(str,DEFAULT_URL_ENCODE);
}
public static String setDecode(String str, String enc) throws Exception {
try {
return URLDecoder.decode(str, enc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new Exception(EncodeUtil.class.getName() + ": URLDecoder.decode设置编码错误:"+e.getMessage());
}
}
public static void main(String[] args) throws Exception {
String str = EncodeUtil.setDecode("过滤词");
System.out.println(str);
}
}