httpclient4下载文件

今天在java里用到httpclient4去下载金山词霸的读音。

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
byte[] b = EntityUtils.toByteArray(entity);
FileOutputStream fileOut = new FileOutputStream(path + key.getText() + i + ".mp3"); // 保存的文件名
fileOut.write(b);
fileOut.flush();
fileOut.close();
用EntityUtils.toByteArray方法比input = entity.getContent()然后input.read(b) 好。

你可能感兴趣的:(httpclient4下载文件)