久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

java 圖片與base64相互轉(zhuǎn)化的示例

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

需要導(dǎo)入:

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;

/** * 圖片轉(zhuǎn)base64字符串 * @param path * @return */public static String PictoBase64(String path) { InputStream in = null; byte[] data = null; // 讀取圖片字節(jié)數(shù)組 try { in = new FileInputStream(path); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 對(duì)字節(jié)數(shù)組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64編碼過的字節(jié)數(shù)組字符串}/** * base64寫圖片 * @param imgStr * @return */public static boolean Base64toPic(String imgStr) { // 對(duì)字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成圖片 if (imgStr == null) // 圖像數(shù)據(jù)為空 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) {// 調(diào)整異常數(shù)據(jù)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相互轉(zhuǎn)化的示例的詳細(xì)內(nèi)容,更多關(guān)于java 圖片與base64相互轉(zhuǎn)化的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 普陀区| 上高县| 滕州市| 常山县| 长垣县| 温泉县| 阳原县| 祁门县| 从化市| 泸西县| 湛江市| 浦江县| 彰化市| 炉霍县| 扎鲁特旗| 绍兴县| 边坝县| 英德市| 永吉县| 安顺市| 漳浦县| 洛隆县| 库伦旗| 海南省| 奇台县| 鹿泉市| 虹口区| 依安县| 肇庆市| 民县| 芒康县| 麻阳| 沛县| 峨山| 宾阳县| 渝北区| 周口市| 长沙县| 开化县| 扎囊县| 桦南县|