package org.jeecg.common.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class Base64ImageUtil { /** * 超鏈接獲取圖片 轉base64字符串 * @param imageUrl 超鏈接 * @return */ public static String getURLImage(String imageUrl) { String base64String = ""; try { //new一個URL對象 URL url = new URL(imageUrl); //打開鏈接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //設置請求方式為"GET" conn.setRequestMethod("GET"); //超時響應時間為5秒 conn.setConnectTimeout(5 * 1000); //通過輸入流獲取圖片數據 InputStream inStream = conn.getInputStream(); //得到圖片的二進制數據,以二進制封裝得到數據,具有通用性 byte[] data = readInputStream(inStream); BASE64Encoder encode = new BASE64Encoder(); base64String = encode.encode(data); } catch (Exception e) { e.printStackTrace(); } return base64String; } /** * 把base64字符串轉換成文件保存到本地 * @param base64String * @param filePath * @param fileName * @throws IOException */ public static void convertBase64ToFile(String base64String, String filePath, String fileName) throws IOException { File file = null; File filedir = new File(filePath); if (!filedir.exists()) { filedir.mkdirs(); } file = new File(filePath + File.separator + fileName); if (file.exists()){ file.delete(); } BASE64Decoder decoder = new BASE64Decoder(); // Base64解碼,對字節數組字符串進行Base64解碼並生成文件 byte[] byt = decoder.decodeBuffer(base64String); for (int i = 0, len = byt.length; i < len; ++i) { // 調整異常數據 if (byt[i] < 0) { byt[i] += 256; } } OutputStream out = null; InputStream input = new ByteArrayInputStream(byt); try { // 生成指定格式的文件 out = new FileOutputStream(filePath + File.separator + fileName); byte[] buff = new byte[1024]; int len = 0; while ((len = input.read(buff)) != -1) { out.write(buff, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { out.flush(); out.close(); } } /** * 把文件轉為base64字符串 * @param filePath * @return */ public static String readBase64File(String filePath) { String StringBase64 = ""; File file = null; file = new File(filePath); if (!file.exists()){ return StringBase64; } InputStream in = null; byte[] data = null; // 讀取文件字節數組 try { in = new FileInputStream(filePath); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } // 對字節數組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); // 返回 Base64 編碼過的字節數組字符串 return encoder.encode(data); } private static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); //創建一個Buffer字符串 byte[] buffer = new byte[1024]; //每次讀取的字符串長度,如果為-1,代表全部讀取完畢 int len = 0; //使用一個輸入流從buffer里把數據讀取出來 while ((len = inStream.read(buffer)) != -1) { //用輸出流往buffer里寫入數據,中間參數代表從哪個位置開始讀,len代表讀取的長度 outStream.write(buffer, 0, len); } //關閉輸入流 inStream.close(); //把outStream里的數據寫入內存 return outStream.toByteArray(); } }
//獲取圖片轉base64字符串 base64String = getURLImage(httppath); //保存到本地 convertBase64ToFile(base64String, filepath, fileName);
讀取base64文件為字符串
String imgBase64 = "data:image/jpeg;base64," + readBase64File(filepath);