Java中根据URL下载gif图片文件

超、凢脫俗 2023-01-21 02:07 350阅读 0赞
  1. import java.io.*;
  2. import java.net.URL;
  3. import java.net.URLConnection;
  4. public static void main(String[] args){
  5. String url="https://xxxx/ecertwebunloginserver/verifyCode/creator";
  6. for(int i=1;i<=100;i++){
  7. // 构造URL
  8. URL urL = new URL(url);
  9. // 打开连接
  10. URLConnection con = urL.openConnection();
  11. // 输入流
  12. InputStream is = con.getInputStream();
  13. // 1K的数据缓冲
  14. byte[] bs = new byte[1024];
  15. // 读取到的数据长度
  16. int len;
  17. String filename="C:\\Users\\admin\\Desktop\\验证码图片\\下载\\"+i+".png";
  18. // 输出的文件流
  19. OutputStream os = new FileOutputStream(filename);
  20. // 开始读取
  21. while ((len = is.read(bs)) != -1) {
  22. os.write(bs, 0, len);
  23. }
  24. // 完毕,关闭所有链接
  25. os.close();
  26. is.close();
  27. }
  28. }

发表评论

表情:
评论列表 (有 0 条评论,350人围观)

还没有评论,来说两句吧...

相关阅读