创建和使用URL访问网络资源
2011-08-17 21:43
495 查看
public class URLTest { public static void main(String[] args) throws Exception { URL url = new URL( "http://java.sun.com:80/docs/books/tutorial/index.html#DOWN"); String protocal = url.getProtocol(); String path = url.getPath(); String host = url.getHost(); String file = url.getFile(); int port = url.getPort(); String ref = url.getRef(); System.out.println(protocal + ", " + host + ", " + port + ", " + file + ", " + ref); System.out.println(path); } } |
public class UrlConnectionTest { public static void main(String[] args) throws Exception { URL url = new URL("http://localhost"); /*URLConnection con = url.openConnection(); InputStream is = con.getInputStream();*/ InputStream is = url.openStream(); OutputStream os = new FileOutputStream("infoq.html"); byte[] buffer = new byte[2048]; int len = 0; while(-1 != (len = is.read(buffer,0,buffer.length))) { os.write(buffer,0,buffer.length); } is.close(); os.close(); } } |
public class UrlConnectionTest2 { public static void main(String[] args) throws Exception { URL url = new URL("http://localhost"); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String line = null; while(null != (line = br.readLine())) { System.out.println(line); } br.close(); } } |
public class InetAddressTest { public static void main(String[] args) throws Exception { InetAddress address = InetAddress.getLocalHost(); System.out.println(address); address = InetAddress.getByName("www.sohu.com"); System.out.println(address); } } |
相关文章推荐
- 创建和使用URL访问网络上的资源
- Java 网络编程(三) 创建和使用URL访问网络上的资源
- 创建和使用URL访问网络上的资源
- Java 网络编程(三) 创建和使用URL访问网络上的资源
- Java 网络编程(三) 创建和使用URL访问网络上的资源
- 安卓个人学习笔记---使用URL访问网络资源
- 使用HttpURLConnection访问网络资源,返回字符串内容
- Android---52---使用URl访问网络资源
- Android开发之使用URL访问网络资源
- Android学习--使用url访问网络资源
- Android 网络:使用URL访问网络资源,ImageView,下载到本地
- Android上Java网络资源访问(二)使用HttpURLConnection多线程下载
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- 使用URL访问网络资源
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- 使用URL访问网络资源
- 使用URL访问网络资源
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- 简单使用URLConnection、HttpURLConnection和HttpClient访问网络资源
- 使用URL访问网络资源