A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王训印 中级黑马   /  2015-8-4 23:34  /  226 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

URL&URLConnection
URI:统一资源标示符。
URL:统一资源定位符,也就是说根据URL能够定位到网络上的某个资源,它是指向互联网“资
源”的指针。
每个URL都是URI,但不一定每个URI都是URL。这是因为URI还包括一个子类,即统一资源名称
(URN),它命名资源但不指定如何定位资源。
示例
  1. import java.net.URL;
  2. 02. import java.net.MalformedURLException;
  3. 03. import java.io.InputStream;
  4. 04. import java.net.URLConnection;
  5. 05. import java.io.IOException;
  6. 06.
  7. 07. public class URLDemo
  8. 08. {
  9. 09. public static void main(String[] args) throws MalformedURLException,IOException {
  10. 10.
  11. 11. String str_url = "http://192.168.1.100:8080/myweb/1.html?name=lisi";
  12. 12.
  13. 13. URL url = new URL(str_url);
  14. 14.
  15. 15. System.out.println("getProtocol:" + url.getProtocol());
  16. 16. System.out.println("getHost:" + url.getHost());
  17. 17. System.out.println("getPort:" + url.getPort());
  18. 18. System.out.println("getFile:" + url.getFile());
  19. 19. System.out.println("getPath:" + url.getPath());
  20. 20. System.out.println("getQuery:" + url.getQuery());
  21. 21.
  22. 22. InputStream in = url.openStream();//相当于
  23. url.openConnection().getInputStream();
  24. 23.
  25. 24. byte[] buf = new byte[1024];
  26. 25. int len = in.read(buf);
  27. 26.
  28. 27. String text = new String(buf,0,len);
  29. 28.
  30. 29. System.out.println(text);
  31. 30.
  32. 31. in.close();
  33. 32. }
  34. 33. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马