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

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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值