一、將本地圖片轉(zhuǎn)換成Base64編碼字符串/** * 將本地圖片轉(zhuǎn)換成Base64編碼字符串 * * @param imgFile 圖片目錄路徑 * @return */ public static String getImgFileToBase64(String imgFile) { //將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理 InputStream inputStream = null; byte[] buffer = null; //讀取圖片字節(jié)數(shù)組 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 { // 關(guān)閉inputStream流 inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 對(duì)字節(jié)數(shù)組Base64編碼 return new BASE64Encoder().encode(buffer); } 二、將網(wǎng)絡(luò)圖片轉(zhuǎn)換成Base64編碼字符串/** * 將網(wǎng)絡(luò)圖片轉(zhuǎn)換成Base64編碼字符串 * * @param imgUrl 網(wǎng)絡(luò)圖片Url * @return */ public static String getImgUrlToBase64(String imgUrl) { InputStream inputStream = null; ByteArrayOutputStream outputStream = null; byte[] buffer = null; try { // 創(chuàng)建URL URL url = new URL(imgUrl); // 創(chuàng)建鏈接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod('GET'); conn.setConnectTimeout(5000); inputStream = conn.getInputStream(); outputStream = new ByteArrayOutputStream(); // 將內(nèi)容讀取內(nèi)存中 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 { // 關(guān)閉inputStream流 inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { // 關(guān)閉outputStream流 outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 對(duì)字節(jié)數(shù)組Base64編碼 return new BASE64Encoder().encode(buffer); } 三、將網(wǎng)絡(luò)鏈接圖片或者本地圖片文件轉(zhuǎn)換成Base64編碼字符串(就是對(duì)上面的兩種進(jìn)行整合)/** * 將網(wǎng)絡(luò)鏈接圖片或者本地圖片文件轉(zhuǎn)換成Base64編碼字符串 * * @param imgStr 網(wǎng)絡(luò)圖片Url/本地圖片目錄路徑 * @return */ public static String getImgStrToBase64(String imgStr) { InputStream inputStream = null; ByteArrayOutputStream outputStream = null; byte[] buffer = null; try { //判斷網(wǎng)絡(luò)鏈接圖片文件/本地目錄圖片文件 if (imgStr.startsWith('http://') || imgStr.startsWith('https://')) { // 創(chuàng)建URL URL url = new URL(imgStr); // 創(chuàng)建鏈接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod('GET'); conn.setConnectTimeout(5000); inputStream = conn.getInputStream(); outputStream = new ByteArrayOutputStream(); // 將內(nèi)容讀取內(nèi)存中 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 { // 關(guān)閉inputStream流 inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { // 關(guān)閉outputStream流 outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } // 對(duì)字節(jié)數(shù)組Base64編碼 return new BASE64Encoder().encode(buffer); } 四、將圖片Base64編碼轉(zhuǎn)換成img圖片文件(解碼)/** * 將圖片Base64編碼轉(zhuǎn)換成img圖片文件 * * @param imgBase64 圖片Base64編碼 * @param imgPath 圖片生成路徑 * @return */ public static boolean getImgBase64ToImgFile(String imgBase64, String imgPath) { boolean flag = true; OutputStream outputStream = null; try { // 解密處理數(shù)據(jù) 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 { // 關(guān)閉outputStream流 outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return flag; } |
|
來(lái)自: 歲月流逝啊 > 《待分類(lèi)》