1.获取浏览器实例,并且设置,请求超时,等待数据超时时间
private static final int REQUEST_TIMEOUT = 10*1000;//设置请求超时10秒钟
private static final int SO_TIMEOUT = 10*1000; //设置等待数据超时时间10秒钟
public HttpClient getHttpClient(){
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpParams);
return client;
}
2。下载实例
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpGet httpGet = new HttpGet(params[0]);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
total = (int) httpEntity.getContentLength();
bar.setMax(total);
File file2 = new File(path);
createPath(file2);
String fileName = params[0].substring(params[0].lastIndexOf("/")+1,params[0].length());
File file = new File(path + fileName);
fileNameAbsolute = file.getAbsolutePath();
inputStream = httpEntity.getContent();
fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int numread;
while ((numread = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, numread);
count += numread;
}
fos.flush();