Base64字符串與文件相互轉換

一.base64轉文件

1.1基礎封裝方法

 * @description  base64字符串轉文件
     * @param base64  base64字符串
     * @param name  創建的文件名
     * @return base64轉換後的文件
     **/
    public static File base64ToFile(String base64, String name) {
        String fname = name + DirKeys.DIR_POSTFIX_PNG;//保存照片的文件名:名字+格式,xxx.png
        FileUtils.mkdirs(DirKeys.DIR_PICTURE);//創建文件目錄
        File picture = new File(DirKeys.DIR_PICTURE, fname);//保存照片的文件
        FileOutputStream out = null;
        try {
            // 解碼,然後將字節轉換爲文件
            byte[] bytes = Base64.decode(base64, Base64.DEFAULT);// 將字符串轉換爲byte數組
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(picture);
            int byteread;
            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread); // 文件寫操作
            }
        } catch (IOException ioe) {
            Logs.e("Base64File38:" + ioe);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                Logs.e("Base64File46:" + e);
            }
        }
        return picture;
    }

2.2添加監聽事件

如果多條base64字符串需要同時轉文件,需要設置監聽事件,讓字符串和文件一一對應,不然會混亂。

Base64和File相互轉換所需要的接口回調
public interface Base64FileListener {
    void ResponseString(String ResponseString);//返回的錯誤原因字符串

    void ResponseFile(File ResponseFile);//返回的文件
}

2.3升級後的方法

    public static void base64ToFile(String base64, String name, Base64FileListener listener) {
        String fname = name + DirKeys.DIR_POSTFIX_PNG;//保存照片的名字
        FileUtils.mkdirs(DirKeys.DIR_PICTURE);
        File picture = new File(DirKeys.DIR_PICTURE, fname);//保存照片的文件
        FileOutputStream out = null;
        try {
            // 解碼,然後將字節轉換爲文件
            byte[] bytes = Base64.decode(base64, Base64.DEFAULT);// 將字符串轉換爲byte數組
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(picture);
            int byteread;
            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread); // 文件寫操作
            }
        } catch (IOException e) {
            Logs.e("Base64FileUtil110:" + e);
            listener.ResponseString(e.toString());
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                Logs.e("Base64FileUtil120:" + e);
                listener.ResponseString(e.toString());
            }
        }
        listener.ResponseFile(picture);
    }

2.4方法調用

    /*Base64字符串轉文字測試*/
    private void test() {
        Base64FileUtil.base64ToFile(Constant.image0, "image0", new Base64FileListener() {
            @Override
            public void ResponseString(String ResponseString) {
                Logs.e("image0轉圖片失敗,原因:"+ResponseString);
            }

            @Override
            public void ResponseFile(File ResponseFile) {
                image0 = ResponseFile.getAbsolutePath();
                Logs.i("image0圖片地址:" + image0);
            }
        });
    }

二.文件轉base64

file:需要轉換的文件

FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
/*需要上傳的圖片信息(爲照片base64照片編碼)*/
picBase64 = Base64.encodeToString(bytes, Base64.DEFAULT);
Logs.v("爲照片base64照片編碼:\n" + picBase64);

2.1封裝成方法

    /**
     * 文件轉base64字符串
     *
     * @param file 需要轉換的文件
     * @return 轉換成功後的字符串
     */
    public static String fileToBase64(File file) {
        String base64 = null;
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] bytes = new byte[in.available()];
            int length = in.read(bytes);
            base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            Logs.e("Base64FileUtil 77:" + e);
        } catch (IOException e) {
            Logs.e("Base64FileUtil 79:" + e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                Logs.e("Base64FileUtil 86:" + e);
            }
        }
        return base64;
    }

三.注意事項

Base64轉文件,無需考慮文件的格式,base64只是一種數據模式,最終是轉成字節數據,字節數據再轉成你需要的格式。

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