將圖片以流的形式複製,並將中間的字節數組用base64編碼後再解碼

package com.zjxt.demo.test;

import org.apache.commons.codec.binary.Base64;

import java.io.*;


/**
 * @Author: heiheihaxi
 * @Date: 2019/4/26 10:02
 */
public class Test26 {
    public static void main(String[] args) {

        File file=new File("C:\\Users\\zzh\\Desktop\\zciu2rebfbh.jpg");
        InputStream is=null;
        byte [] data=null;
        try {
            is=new FileInputStream(file);
            data = new byte[is.available()];
            is.read(data);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=data)
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String s =null;
        if (null != data){
             s = new String(Base64.encodeBase64(data));
        }

        byte[] bytes = Base64.decodeBase64(s);

        OutputStream os = null;
        try  {

            os = new FileOutputStream(new File("C:\\Users\\zzh\\Desktop\\zc2.jpg"));
            if (null != bytes)
            os.write(bytes);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=os)
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }

}

 

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