国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

java 圖片與base64相互轉化的示例

瀏覽:3日期:2022-08-22 09:15:56

需要導入:

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.UUID;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;

/** * 圖片轉base64字符串 * @param path * @return */public static String PictoBase64(String path) { InputStream in = null; byte[] data = null; // 讀取圖片字節數組 try { in = new FileInputStream(path); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 對字節數組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64編碼過的字節數組字符串}/** * base64寫圖片 * @param imgStr * @return */public static boolean Base64toPic(String imgStr) { // 對字節數組字符串進行Base64解碼并生成圖片 if (imgStr == null) // 圖像數據為空 return false; BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解碼 byte[] b = decoder.decodeBuffer(imgStr); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) {// 調整異常數據b[i] += 256; } } String imgName = UUID.randomUUID().toString().replace('-', ''); String imgType = '.png'; // 生成png圖片 String imgFilePath = 'C:/Users/Administrator/Desktop/' + imgName + imgType;// 新生成的圖片 OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; }}

以上就是java 圖片與base64相互轉化的示例的詳細內容,更多關于java 圖片與base64相互轉化的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 伊宁市| 长宁区| 渭源县| 浦江县| 塘沽区| 民和| 阿拉尔市| 永安市| 清镇市| 平邑县| 麻栗坡县| 游戏| 札达县| 固阳县| 华容县| 三原县| 石家庄市| 新和县| 枣强县| 枞阳县| 泾阳县| 丽江市| 突泉县| 镇安县| 永丰县| 永仁县| 胶州市| 兰溪市| 收藏| 伊春市| 延寿县| 彝良县| 介休市| 汝南县| 天峻县| 葫芦岛市| 漯河市| 松潘县| 托克逊县| 铜川市| 宁远县|