android Java BASE64編碼和解碼

來源:互聯網
上載者:User

標籤:

 

1、Base64  的作用

    主要不是加密,它主要的用途是把一些位元轉成一般字元用於網路傳輸。
    由於一些二進位字元在傳輸協議中屬於控制字元,不能直接傳送需要轉換一下就可以了。

2、如何獲得BASE64 代碼,有兩種方式

   方式一:解決eclipse中無法直接使用Base64Encoder的問題

  •    今天要使用Base64Encoder,卻發現在Eclipse中無法找到該類,google了哈,原來Base64Encoder並不屬於JDK標準庫範疇,但是又包含在了JDK中。
  •    解決方案:按照如下方法設定Eclipse匯入%JAVA_HOME%\jre\lib目錄下的rt.jar包即可,Project->Properties,選擇Java Build Path設定項,再選擇Libraries標籤,
  •    Add External Jars添加%JAVA_HOME%\jre\lib\rt.jar就可以使用啦!

     

    方式二: 下載原始碼,然後把代碼匯入自己的項目中

        csdn: http://download.csdn.net/detail/yanzi2015/8685199

 

3、Base64對字串進行編碼和解碼

   

package com.android20;import sun.misc.BASE64Decoder;public class Bian {    //將 s 進行 BASE64 編碼     public static String getBASE64(String s) {         if (s == null) return null;         return (new sun.misc.BASE64Encoder()).encode( s.getBytes() );     }     //將 BASE64 編碼的字串 s 進行解碼     public static String getFromBASE64(String s) {         if (s == null) return null;         BASE64Decoder decoder = new BASE64Decoder();         try {             byte[] b = decoder.decodeBuffer(s);             return new String(b);         } catch (Exception e) {             return null;         }     }}

 

android Java BASE64編碼和解碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.