Java對網絡圖片/本地圖片轉換成Base64編碼和解碼

1、將本地圖片轉換成Base64編碼字符串

 

/**
     * 將本地圖片轉換成Base64編碼字符串
     *
     * @param imgFile 圖片目錄路徑
     * @return
     */
    public static String getImgFileToBase64(String imgFile) {
        //將圖片文件轉化爲字節數組字符串,並對其進行Base64編碼處理
        InputStream inputStream = null;
        byte[] buffer = null;
        //讀取圖片字節數組
        try {
            inputStream = new FileInputStream(imgFile);
            int count = 0;
            while (count == 0) {
                count = inputStream.available();
            }
            buffer = new byte[count];
            inputStream.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    // 關閉inputStream流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 對字節數組Base64編碼
        return new BASE64Encoder().encode(buffer);
    }

 

2、將網絡圖片轉換成Base64編碼字符串

 

/**
     * 將網絡圖片轉換成Base64編碼字符串
     *
     * @param imgUrl 網絡圖片Url
     * @return
     */
    public static String getImgUrlToBase64(String imgUrl) {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        byte[] buffer = null;
        try {
            // 建立URL
            URL url = new URL(imgUrl);
            // 建立連接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            inputStream = conn.getInputStream();
            outputStream = new ByteArrayOutputStream();
            // 將內容讀取內存中
            buffer = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            buffer = outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    // 關閉inputStream流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    // 關閉outputStream流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 對字節數組Base64編碼
        return new BASE64Encoder().encode(buffer);
    }

 

3、將網絡連接圖片或者本地圖片文件轉換成Base64編碼字符串(就是對上面的兩種進行整合)

 

/**
     * 將網絡連接圖片或者本地圖片文件轉換成Base64編碼字符串
     *
     * @param imgStr 網絡圖片Url/本地圖片目錄路徑
     * @return
     */
    public static String getImgStrToBase64(String imgStr) {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        byte[] buffer = null;
        try {
            //判斷網絡連接圖片文件/本地目錄圖片文件
            if (imgStr.startsWith("http://") || imgStr.startsWith("https://")) {
                // 建立URL
                URL url = new URL(imgStr);
                // 建立連接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                inputStream = conn.getInputStream();
                outputStream = new ByteArrayOutputStream();
                // 將內容讀取內存中
                buffer = new byte[1024];
                int len = -1;
                while ((len = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, len);
                }
                buffer = outputStream.toByteArray();
            } else {
                inputStream = new FileInputStream(imgStr);
                int count = 0;
                while (count == 0) {
                    count = inputStream.available();
                }
                buffer = new byte[count];
                inputStream.read(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    // 關閉inputStream流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    // 關閉outputStream流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 對字節數組Base64編碼
        return new BASE64Encoder().encode(buffer);
    }

將圖片Base64編碼轉換成img圖片文件(解碼)

 

/**
     * 將圖片Base64編碼轉換成img圖片文件
     *
     * @param imgBase64 圖片Base64編碼
     * @param imgPath   圖片生成路徑
     * @return
     */
    public static boolean getImgBase64ToImgFile(String imgBase64, String imgPath) {
        boolean flag = true;
        OutputStream outputStream = null;
        try {
            // 解密處理數據
            byte[] bytes = new BASE64Decoder().decodeBuffer(imgBase64);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {
                    bytes[i] += 256;
                }
            }
            outputStream = new FileOutputStream(imgPath);
            outputStream.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        } finally {
            if (outputStream != null) {
                try {
                    // 關閉outputStream流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }
相關文章
相關標籤/搜索