Java——inputStream轉換成Base64字符串

	/**
     * 將inputstream轉爲Base64
     * 
     * @param is
     * @return
     * @throws Exception
     */
    private String getBase64FromInputStream(InputStream is) throws Exception {
        // 將圖片文件轉化爲字節數組字符串,並對其進行Base64編碼處理
        byte[] data = null;

        // 讀取圖片字節數組
        try {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = is.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new Exception("輸入流關閉異常");
                }
            }
        }

        return Base64.byteArrayToBase64(data);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章