写一个java程序访问url,并根据stream算出响应时间及读取打印页面内容
package cn.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class duqubaidu {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.baidu.com");
long start = System.currentTimeMillis();
InputStream openStream = url.openStream();
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(openStream));
String tmp = null;
while((tmp = reader.readLine()) != null) {
builder.append(tmp + "\n");
}
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start) + "ms");
System.out.println(builder.toString());
}
}
输出结果 :主要看 DOCTYPE html
"D:\Program Files\Java\jdk1.8.0_141\bin\java" "-javaagent:D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.2.2\lib\idea_rt.jar=59519:D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_141\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_141\jre\lib\rt.jar;F:\spark\target\classes;E:\develop\repository_pinyougou\org\scala-lang\scala-library\2.11.8\scala-library-2.11.8.jar;E:\develop\repository_pinyougou\org\apache\spark\spark-core_2.11\2.1.3\spark-core_2.11-2.1.3.jar;E:\develop\repository_pinyougou\org\apache\avro\avro-mapred\1.7.7\avro-mapred-1.7.7-hadoop2.jar;E:\develop\repository_pinyougou\org\apache\avro\avro-ipc\1.7.7\avro-ipc-1.7.7.jar;E:\develop\repository_pinyougou\org\apache\avro\avro\1.7.7\avro-1.7.7.jar;E:\develop\repository_pinyougou\org\apache\avro\avro-ipc\1.7.7\avro-ipc-1.7.7-tests.jar;E:\develop\repository_pinyougou\org\codehaus\jackson\jackson-core-asl\1.9.13\jackson-core-asl-1.9.13.jar;E:\develop\repository_pinyougou\org\codehaus\jackson\jackson-mapper-asl\1.9.13\jackson-mapper-asl-1.9.13.jar;E:\develop\repository_pinyougou\com\twitter\chill_2.11\0.8.0\chill_2.11-0.8.0.ja