java httpclient 下载_java HttpClient 下载一张图片
[java]代碼庫(kù)public static void downloadImage() {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(
"http://yuncode.net/upload/headicon/4/c/pic_518d1ed6604c4.jpg");
for (int i = 0; i < 30; i++) {
try {
// 執(zhí)行g(shù)etMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 讀取內(nèi)容
String picName = "img//" + i + ".jpg";
InputStream inputStream = getMethod.getResponseBodyAsStream();
OutputStream outStream = new FileOutputStream(picName);
IOUtils.copy(inputStream, outStream);
outStream.close();
System.out.println("OK!");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 釋放連接
getMethod.releaseConnection();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java httpclient 下载_java HttpClient 下载一张图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: cmd批处理剪切_Windows命令行b
- 下一篇: 交换机故障诊断与排除