之前这么写,总是 在连一段时间后,下载变很慢
URL uri = new URL(imgSrc);
InputStream in = uri.openStream();
HttpURLConnection httpConn = (HttpURLConnection)uri.openConnection();
InputStream in = httpConn.getInputStream();
System.out.println("开始下载:URL------------" + imgSrc);
while ((length = in.read(buf, 0, buf.length)) != -1) {
fo.write(buf, 0, length);
}
fo.flush();
fo.close();
in.close();
也不报错,总是在这里变慢:uri.openConnection();
目测是链接没关闭,导致的等待状态。这是java的HttpURLConnection,
后来用HttpClient就没这么慢。
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(imgSrc);
client.executeMethod(get);
InputStream in = get.getResponseBodyAsStream();
System.out.println("开始下载:URL------------" + imgSrc);
while ((length = in.read(buf, 0, buf.length)) != -1) {
fo.write(buf, 0, length);
}
fo.flush();
fo.close();
in.close();
//释放链接
get.releaseConnection();