java读取URL 返回XML 文件1M 浏览器直接打开5秒钟 使用下面代码 却要50多秒 求大神指导

zhujyy110 2013-12-26 10:34:35

String url="####################";
URLConnection uc=null;
InputStream in=null;
InputStreamReader rd=null;
List<MatchInfo> mList1 = new ArrayList<MatchInfo>();
try {
Date stratDate = new Date();
uc = new URL(url).openConnection();
uc.setConnectTimeout(10000);
uc.setDoOutput(true);
in = new BufferedInputStream(uc.getInputStream());
rd = new InputStreamReader(in, "UTF-8");
java.io.BufferedReader br = new java.io.BufferedReader(rd);
String tempbf;
StringBuffer temp=new StringBuffer();
while ((tempbf = br.readLine()) != null) {
temp.append(tempbf);
temp.append("\r\n");
}
String content = HttpClientSSL.getContent1(url);
System.out.println("抓取 结束" + " 耗时----"
+ (stratDate.getTime() - new Date().getTime()) / 1000.0);
...全文
959 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
浪漫小和 2013-12-27
  • 打赏
  • 举报
回复
uc = new URL(url).openConnection(); 应该是这里,因为网络的原因。
浪漫小和 2013-12-27
  • 打赏
  • 举报
回复
这个代码并不多,你可以调适下。
teemai 2013-12-27
  • 打赏
  • 举报
回复
String content = HttpClientSSL.getContent1(url); 这前后加个时间看看执行了多长时间。 这之前的代码应该很快
zhujyy110 2013-12-27
  • 打赏
  • 举报
回复
String url="####################"; URLConnection uc=null; InputStream in=null; InputStreamReader rd=null; List<MatchInfo> mList1 = new ArrayList<MatchInfo>(); try { Date stratDate = new Date(); uc = new URL(url).openConnection(); uc.setConnectTimeout(10000); uc.setDoOutput(true); in = new BufferedInputStream(uc.getInputStream()); rd = new InputStreamReader(in, "UTF-8"); java.io.BufferedReader br = new java.io.BufferedReader(rd); String tempbf; StringBuffer temp=new StringBuffer(); while ((tempbf = br.readLine()) != null) { temp.append(tempbf); temp.append("\r\n"); } //String content = HttpClientSSL.getContent1(url); System.out.println("抓取 结束" + " 耗时----" + (stratDate.getTime() - new Date().getTime()) / 1000.0); 这段被注释掉//String content = HttpClientSSL.getContent1(url);
huntor 2013-12-27
  • 打赏
  • 举报
回复
另外 初始化 StringBuffer/StringBuilder时传入一个足够的初始长度。
tony4geek 2013-12-27
  • 打赏
  • 举报
回复
deubg 看在什么地方卡了。 就知道那步问题了
ghostkngiht 2013-12-27
  • 打赏
  • 举报
回复
看看是不是HttpClientSSL.getContent1这个方法里有太慢的写法。
zhujyy110 2013-12-26
  • 打赏
  • 举报
回复
java读取URL 返回1M 的 XML文件 浏览器直接打开5秒钟 使用代码 却要50多秒 求大神指导

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧