Java httpclient downloads an image

From , 3 Years ago, written in Java, viewed 224 times.
URL https://pastebin.vip/view/c8c41c4a
  1. public static void downloadImage() {
  2.     HttpClient httpClient = new HttpClient();
  3.     GetMethod getMethod = new GetMethod(
  4.         "http://yuncode.net/upload/headicon/4/c/pic_518d1ed6604c4.jpg");
  5.     for (int i = 0; i < 30; i++) {
  6.         try {
  7.             // 执行getMethod
  8.             int statusCode = httpClient.executeMethod(getMethod);
  9.             if (statusCode != HttpStatus.SC_OK) {
  10.                 System.err.println("Method failed: "
  11.                                    + getMethod.getStatusLine());
  12.             }
  13.             // 读取内容
  14.             String picName = "img//" + i + ".jpg";
  15.             InputStream inputStream = getMethod.getResponseBodyAsStream();
  16.             OutputStream outStream = new FileOutputStream(picName);
  17.             IOUtils.copy(inputStream, outStream);
  18.             outStream.close();
  19.             System.out.println("OK!");
  20.         } catch (Exception e) {
  21.             e.printStackTrace();
  22.         } finally {
  23.             // 释放连接
  24.             getMethod.releaseConnection();
  25.         }
  26.     }
  27. }

Reply to "Java httpclient downloads an image"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website