中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java將圖片與base64編碼相互轉(zhuǎn)換

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
/**
 * @Descriptionmap 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進行Base64編碼處理
 * @author temdy
 * @Date 2015-01-26
 * @param path 圖片路徑
 * @return
 */
public static String imageToBase64(String path) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進行Base64編碼處理
    byte[] data = null;
    // 讀取圖片字節(jié)數(shù)組
    try {
        InputStream in = new FileInputStream(path);
        data = new byte[in.available()];
        in.read(data);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 對字節(jié)數(shù)組Base64編碼
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(data);// 返回Base64編碼過的字節(jié)數(shù)組字符串
}

/**
 * @Descriptionmap 對字節(jié)數(shù)組字符串進行Base64解碼并生成圖片
 * @author temdy
 * @Date 2015-01-26
 * @param base64 圖片Base64數(shù)據(jù)
 * @param path 圖片路徑
 * @return
 */
public static boolean base64ToImage(String base64, String path) {// 對字節(jié)數(shù)組字符串進行Base64解碼并生成圖片
    if (base64 == null){ // 圖像數(shù)據(jù)為空
        return false;
    }
    BASE64Decoder decoder = new BASE64Decoder();
    try {
        // Base64解碼
        byte[] bytes = decoder.decodeBuffer(base64);
        for (int i = 0; i < bytes.length; ++i) {
            if (bytes[i] < 0) {// 調(diào)整異常數(shù)據(jù)
                bytes[i] += 256;
            }
        }
        // 生成jpeg圖片
        OutputStream out = new FileOutputStream(path);
        out.write(bytes);
        out.flush();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Struts2上傳文件,文件大小默認最大值的修改

下一篇:Java使用gzip對字符串進行壓縮和解壓縮