Java對網路圖片/本地圖片轉換成Base64編碼和解碼
阿新 • • 發佈:2018-11-29
一、將本地圖片轉換成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編碼 returnnew BASE64Encoder().encode(buffer); }
二、將網路圖片轉換成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); }
三、將網路連結圖片或者本地圖片檔案轉換成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; }