URLDecoder 和 URLEncoder 用于完成普通字符串 和 application/x-www-form-urlencoded MIME 字符串之间的相互转换
URLDecoder类包含一个decode(String s,String enc)静态方法,它可以将application/x-www-form-urlencoded MIME字符串转成普通字符串;
URLEncoder类包含一个encode(String s,String enc)静态方法,它可以将普通字符串转换成application/x-www-form-urlencoded MIME字符串。
import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URLDecoder; import java.net.URLEncoder; /** * @author ceshi * @Title: JunitCoder * @ProjectName ceshi * @Description: ceshi * @date 2018/6/2619:21 */ public class JunitCoder { private Logger logger = LoggerFactory.getLogger(JunitCoder.class); @Test public void testCoder()throws Exception{ String str1= "\u4f60\u597d\uff01"; String str2= "你好!"; logger.info(URLEncoder.encode(str2, "gbk")); logger.info(URLEncoder.encode(str2, "utf-8")); logger.info(URLDecoder.decode(str1, "utf-8")); logger.info(URLDecoder.decode(str1, "ISO-8859-1")); logger.info(URLDecoder.decode(str1, "gbk")); logger.info(str1); } }
运行结果: