您的位置:首页 > 理论基础 > 计算机网络

HttpClient下载文件

2012-12-25 17:06 369 查看
private boolean download(String url, String bundleId, String filemd5) {
HttpClient httpClient1 = new DefaultHttpClient();
HttpGet httpGet1 = new HttpGet(url);
try {
HttpResponse httpResponse1 = httpClient1.execute(httpGet1);

StatusLine statusLine = httpResponse1.getStatusLine();
if (statusLine.getStatusCode() == 200) {
String filePath = this.getFilePath(bundleId) + filemd5
+ this.getFileSuffix(url); // 文件路径
File file = new File(filePath);
FileOutputStream outputStream = new FileOutputStream(file);
InputStream inputStream = httpResponse1.getEntity()
.getContent();
byte b[] = new byte[1024];
int j = 0;
while ((j = inputStream.read(b)) != -1) {
outputStream.write(b, 0, j);
}
outputStream.flush();
outputStream.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
httpClient1.getConnectionManager().shutdown();
}
return true;
}


JAVA获取cpu个数: Runtime.getRuntime().availableProcessors()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: