package com.template.common.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; /** * @Author: binguo * @Date: 2024/1/9 星期二 15:17 * @Description: com.template.common.utils * @Version: 1.0 */ public class imageUtils { public static String getBase64Url(String imagePath){ ByteArrayOutputStream outPut = new ByteArrayOutputStream(); byte[] data = new byte[1024]; try { // 创建URL URL url = new URL(imagePath); // 创建链接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(10 * 1000); if(conn.getResponseCode() != 200) { return "fail";//连接失败/链接失效/图片不存在 } InputStream inStream = conn.getInputStream(); int len = -1; while ((len = inStream.read(data)) != -1) { outPut.write(data, 0, len); } inStream.close(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 byte[] encode = Base64.getEncoder().encode(outPut.toByteArray()); String res = new String(encode); return res; } //https://www.jb51.net/program/288342ppk.htm 图片压缩大小 }