Java将网络链接图片或者本地图片文件转换成Base64编码字符串

2023-03-06 16:25:31 浏览数 (1)

完整Demo

代码语言:javascript复制
/**
 * 将网络链接图片或者本地图片文件转换成Base64编码字符串
 *
 * @param imgStr 网络图片Url/本地图片目录路径
 * @return
 */
public static String getImgStrToBase64(String imgStr) {
    InputStream inputStream = null;
    ByteArrayOutputStream outputStream = null;
    byte[] buffer = null;
    try {
        //判断网络链接图片文件/本地目录图片文件
        if (imgStr.startsWith("http://") || imgStr.startsWith("https://")) {
            // 创建URL
            URL url = new URL(imgStr);
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            inputStream = conn.getInputStream();
            outputStream = new ByteArrayOutputStream();
            // 将内容读取内存中
            buffer = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            buffer = outputStream.toByteArray();
        } else {
            inputStream = new FileInputStream(imgStr);
            int count = 0;
            while (count == 0) {
                count = inputStream.available();
            }
            buffer = new byte[count];
            inputStream.read(buffer);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                // 关闭inputStream流
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                // 关闭outputStream流
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    // 对字节数组Base64编码
    return Base64.getEncoder().encodeToString(buffer);
}

将图片Base64编码转换成img图片文件

请查看站内文章 ---> 将图片Base64编码转换成img图片文件

0 人点赞