public String GetUrlImageToBase64(String url) throws Exception { if (url == null || "".equals(url.trim())) return null; URL u = new URL(url); // 打开图片路径 HttpURLConnection conn = (HttpURLConnection) u.openConnection(); // 设置请求方式为GET conn.setRequestMethod("GET"); // 设置超时响应时间为5秒 conn.setConnectTimeout(5000); // 通过输入流获取图片数据 InputStream inStream = conn.getInputStream(); // 读取图片字节数组 byte[] data = new byte[inStream.available()]; inStream.read(data); inStream.close(); // 返回Base64编码过的字节数组字符串 String base64string = new String(Base64.encode(data)); return base64string; }