關(guān)于圖片與base64相互轉(zhuǎn)換的工具類-創(chuàng)新互聯(lián)

最近在編寫平臺對接軟件,需要從Oracle中取出blob類型圖片數(shù)據(jù)轉(zhuǎn)換為base64字符串寫入到xml中,這里記錄一下用到的轉(zhuǎn)換方法

// 將一張本地圖片轉(zhuǎn)化成Base64字符串
public  static String GetImageStrFromPath(String imgPath) {
    InputStream in = null;
    byte[] data = null;
    // 讀取圖片字節(jié)數(shù)組
    try {
        in = new FileInputStream(imgPath);
        data = new byte[in.available()];
        in.read(data);
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 對字節(jié)數(shù)組Base64編碼
    BASE64Encoder encoder = new BASE64Encoder();
    // 返回Base64編碼過的字節(jié)數(shù)組字符串
    return encoder.encode(data);
}

// 將一張網(wǎng)絡(luò)圖片轉(zhuǎn)化成Base64字符串
public  String GetImageStrFromUrl(String imgURL) {
    byte[] data = null;
    try {
        // 創(chuàng)建URL
        URL url = new URL(imgURL);
        // 創(chuàng)建鏈接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5 * 1000);
        InputStream inStream = conn.getInputStream();
        data = new byte[inStream.available()];
        inStream.read(data);
        inStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 對字節(jié)數(shù)組Base64編碼
    BASE64Encoder encoder = new BASE64Encoder();
    // 返回Base64編碼過的字節(jié)數(shù)組字符串
    return encoder.encode(data);
}

// 將blob類型圖片轉(zhuǎn)換為Base64字符串
public static String blobToBase64(Blob blob) {
    String result = "";
    if (null != blob) {
        try {
            InputStream msgContent = blob.getBinaryStream();
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[100];
            int n = 0;
            while (-1 != (n = msgContent.read(buffer))) {
                output.write(buffer, 0, n);
            }
            result = new BASE64Encoder().encode(output.toByteArray());
            output.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    } else {
        return null;
    }
}

// 將base64字符串轉(zhuǎn)換成圖片
public static boolean GenerateImage(String imgStr) {
    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;
            }
        }
        // 生成jpeg圖片
        String imgFilePath = "d://222.jpg";
        OutputStream out = new FileOutputStream(imgFilePath);
        out.write(b);
        out.flush();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。

創(chuàng)新互聯(lián)建站基于成都重慶香港及美國等地區(qū)分布式IDC機房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)光華機房服務(wù)器托管報價,主機托管價格性價比高,為金融證券行業(yè)服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨享,G口帶寬及機柜租用的專業(yè)成都idc公司。

網(wǎng)站欄目:關(guān)于圖片與base64相互轉(zhuǎn)換的工具類-創(chuàng)新互聯(lián)
網(wǎng)頁網(wǎng)址:http://muchs.cn/article2/dpedic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、App開發(fā)、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站