发表于: 2018-01-29 23:11:54
1 687
今天做的事情:
今天看韬哥之前日报,看到一个知识点,java中获取三种URL的方法。跑一个demo了解下。
通过一个URL链接,我们就可以确定资源的位置,比如网络文件,网络页面以及网络程序,例如: http://www.jnshu.com/school/14038/class。这个链接,规定了http协议,主机名称www.jnshu.com。端口号8080或者个人设置,school/14038/class确定了我们要在站点上所访问的资源,这个URL还可以包含其他元素,比如说文件段已经查询信息等。
public static void main(String[] args) {
try {
// 方法一 建立网络连接 net 包
// URL url = new URL("http://baidu.com");
//打开网络连接
// URLConnection urlConnection = url.openConnection();
// InputStream is = urlConnection.getInputStream();
//方法二
URL url = new URL("http://www.w3school.com.cn/");
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
InputStream is = urlcon.getInputStream();
//方法三
// URL url = new URL("http");
// InputStream is = url.openStream();
//获取当前时间
long L = System.currentTimeMillis();
//建立字符流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
StringBuffer bs = new StringBuffer();
String s = null;
while ((s = bufferedReader.readLine()) != null){
bs.append(L).append("/n");
}
System.out.println(bs.toString());
System.out.println(" content-encode:"+ urlcon.getContentEncoding() );
System.out.println(" content-length:"+urlcon.getContentLength());
System.out.println(" content-type:"+urlcon.getContentType());
System.out.println(" date:"+urlcon.getDate());
System.out.println("总共执行时间为:"+(System.currentTimeMillis()-L)+"毫秒");
} catch (Exception e) {
e.printStackTrace();
}
}
创建URL class的实例,可以进行操作,但是在访问这个URL上的资源和内容之前,必须打开连接,通过用openConnection来完成这一操作。执行成功之后,返回一个URLConnection class实例,连接成功,就可以对这个URLConnection的输入和输出进行读写操作了/
运行结果:
既然用到流了,又找一篇博客去看,连接:https://www.cnblogs.com/biehongli/p/6074713.html
有兴趣的可以看看。
关于项目,修改的差不多了。如果demo提出问题,再修改把。
遇到的问题:
无
收获:
了解一些课外知识,再看一下基础
评论