根据图片地址转换base64码并输出本地

转码 同时被 2 个专栏收录
1 篇文章 0 订阅
3 篇文章 0 订阅

public static void main(String[] args) throws IOException {

    String urlPath = "\n" +
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1592375119711&di=df41052a64fbb26a80b96368aa63802f&imgtype=0&src=http%3A%2F%2Fgbres.dfcfw.com%2FFiles%2Fpicture%2F20200519%2FA470FA6879010F8F0A5EE64267367E5E_w640h512.jpg\n" +
            "\n";

// String imgData = new sun.misc.BASE64Encoder().encode(Img_url.getBytes());
ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
URL url = new URL(urlPath);
System.out.println(“url:” + url);
byte[] by = new byte[1024];
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setConnectTimeout(1000 * 5);
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
int len = -1;
while ((len = inputStream.read(by)) != -1) {
data.write(by, 0, len);
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
String imgData = Base64.getMimeEncoder().encodeToString(data.toByteArray());
System.out.println(“imgData:” + imgData);
OutputStream out = null;
//解码输出本地
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer(imgData);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
out = new FileOutputStream(“D:\人脸信息图片.jpg”);
out.write(b);
out.flush();
out.close();

}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值