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 { |
// 执行getMethod |
int statusCode = httpClient.executeMethod(getMethod); |
if (statusCode != HttpStatus.SC_OK) { |
System.err.println( "Method failed: " |
+ getMethod.getStatusLine()); |
} |
// 读取内容 |
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(); |
} |
} |
} |