天天看點

java網絡程式設計之application/x-www-form-urlencoded MIME

URLEncoder和URLDecoder用于完成普通字元串和application/x-www-form-urlencoded MIME字元串之間的互相轉換.

java網絡程式設計之application/x-www-form-urlencoded MIME

編碼和解碼:

  在Web的浏覽器中,不同的浏覽器的編碼和解碼規則是不一樣的.

  對于W3C浏覽器:遵循W3C組織規範的浏覽器,(非IE).

               編碼:byte[] data = “楊哥”.getByte(String charsetName);

               解碼:String  str = new String(data, String charsetName);

   對于IE浏覽器:

              編碼使用的application/x-www-form-urlencoded MIME機制.

import java.net.URLDecoder;
import java.net.URLEncoder;

public class URLecodeDemo {
	public static void main(String[] args) throws Exception {
		String name = "楊哥 sunshine";
		//編碼
		String encode = URLEncoder.encode(name, "UTF-8");
		System.out.println(encode);//%E6%9D%A8%E5%93%A5+sunshine
		//解碼
		String decode = URLDecoder.decode(encode, "UTF-8");
		System.out.println(decode);//楊哥 sunshine
	}
}