java URLEncoder 和Base64.encode()

來源:互聯網
上載者:User

標籤:ack   urlencode   try   nbsp   doc   detail   href   void   details   

參考:

http://www.360doc.com/content/10/1103/12/1485725_66213001.shtml (URLEncode)

http://blog.csdn.net/uikoo9/article/details/27981219

電腦中的資料都是二進位的,不管是字串還是檔案,而加密後的也是二進位的,

而我們要看到的往往是字串,本文就介紹了將byte[]轉為各種進位以及base64編碼。

 

是一種編碼方式,可以理解為複雜的進位,很多演算法加密後輸出的都是byte[],而這個byte[]對我們顯示的形式是不友好的(亂碼),

所以一般都是轉為base64的,當然也可以轉為其他進位。

public static void main(String[] s){
String sss = "Aa123Aa程式";
byte[] bytes = null;
try {
bytes = sss.getBytes("UTF-8");
for(byte b: bytes){
System.out.print(b+"(#)");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}


String encode;
try {

encode = Base64.encode(bytes);
System.out.println("\nBase64.encode 結果: " + encode);
byte[] bytess = Base64.decode(encode);
for(byte b: bytess){
System.out.print(b+"(#)");
}
String sss2 = new String(bytess, "UTF-8");
System.out.print("\nsss2 === "+ sss2);

sss = "Aa123 Aa程式#程式*程式+and程式";
encode = java.net.URLEncoder.encode(sss, "UTF-8");
System.out.println("\njava.net.URLEncoder 結果: " + encode);
String sss3 = URLDecoder.decode(encode, "UTF-8");
System.out.println("\njava.net.URLDecoder 結果: " + sss3);
} catch (Exception e) {
e.printStackTrace();
}

運行結果:

65(#)97(#)49(#)50(#)51(#)65(#)97(#)-25(#)-88(#)-117(#)-27(#)-70(#)-113(#)
Base64.encode 結果: QWExMjNBYeeoi+W6jw==
65(#)97(#)49(#)50(#)51(#)65(#)97(#)-25(#)-88(#)-117(#)-27(#)-70(#)-113(#)
sss2 === Aa123Aa程式
java.net.URLEncoder 結果: Aa123+Aa%E7%A8%8B%E5%BA%8F%23%E7%A8%8B%E5%BA%8F*%E7%A8%8B%E5%BA%8F%2Band%E7%A8%8B%E5%BA%8F

java.net.URLDecoder 結果: Aa123 Aa程式#程式*程式+and程式

java URLEncoder 和Base64.encode()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.