[Java] java HttpClient 下载一张图片 →→→→→进入此内容的聊天室

来自 , 2020-09-22, 写在 Java, 查看 163 次.
URL http://www.code666.cn/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. }

回复 "java HttpClient 下载一张图片"

这儿你可以回复上面这条便签

captcha