Java圖片和base64字符串的相互轉化案例

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class TestBase64 {

    public static void main(String[] args) throws Exception{

        InputStream in = new FileInputStream("E://555.png");
        byte[] bytes = new byte[in.available()];

        in.read(bytes);

        BASE64Encoder encoder = new BASE64Encoder();
        String enStr = encoder.encode(bytes);

        in.close();
        System.out.println(enStr);

        Thread.sleep(4000);

        BASE64Decoder decoder = new BASE64Decoder();

        byte[] res = decoder.decodeBuffer(enStr);

        OutputStream out = new FileOutputStream("E:\\base64.png");
        out.write(res);

        out.close();
    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章