把微信头像保存到本地的两种方式
发布日期:2021-05-15 08:19:47 浏览次数:17 分类:精选文章

本文共 2312 字,大约阅读时间需要 7 分钟。

������������������������

���������������������������������������������������������

������������������������������������������������������������������������������������

������������������URLConnection������ InputStream

���������������������������������������������������������������������������������������������������������������

public static void getAvatarByURLConnection(String url) throws IOException {  
URL pigUrl = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) pigUrl.openConnection();
InputStream inputStream = conn.getInputStream();
byte[] buffer = new byte[1024 * 1024];
int len = 0;
OutputStream outputStream = new FileOutputStream("E:/avatar.jpg");
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
}

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


������������������ImageIO���������������������������

������������������������������������������������������������������

public static void getAvatarByImageIO(String url) throws IOException {  
BufferedImage avatarImage = ImageIO.read(new URL(url));
File file = new File("E:/avatar.jpg");
ImageIO.write(avatarImage, "jpg", file);
}

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


���������������������������������

  • ���������������������������������������������������������ImageIO���������������������������������������������URLConnection������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������URL���������������������������������������
  • ���������������������������������������������������������������������������JPG���PNG���������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������

    上一篇:利用自定义注解,AOP + redis限制ip访问接口次数
    下一篇:springboot拦截器

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月23日 20时48分07秒