2 url与URLConnection使用

例子1

package add.socket.url;

import java.net.MalformedURLException;
import java.net.URL;

public class URLTest01 {

	public static void main(String[] args) {
		try {
			URL myURL = new URL(
					"http://java.sun.com:80/docs/books/tutorial/index.html#DOWN");
			/*
			 * URL myURL = new URL( "http://www.baidu.com");
			 */

			String protocal = myURL.getProtocol();
			System.out.println("protocal = " + protocal); // protocal = http

			String host = myURL.getHost();
			System.out.println("host = " + host); // host = java.sun.com

			int port = myURL.getPort();
			System.out.println("port = " + port); // port = 80

			String file = myURL.getFile();
			System.out.println("file = " + file); // file =
													// /docs/books/tutorial/index.html

			String ref = myURL.getRef();
			System.out.println("ref = " + ref); // ref = DOWN

		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

	}

}

例子2:

package add.socket.url;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnection01 {

	/**
	 * 为获得URL的实际比特或内容信息,用它的openConnection()方法从它创建一个URLConnection对象,
	 * 与调用URL对象相关,它返回一个URLConnection对象。它可能引发IOException异常。
	 * URLConnection是访问远程资源属性的一般用途的类。
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		URL url = new URL("http://localhost:8080/day09/testParam.html");
        
/*		// 打开连接
        URLConnection conn = url.openConnection();
        // 得到输入流
        InputStream is = conn.getInputStream();*/
        
        //上面两句等价于下面的一句,
		InputStream is = url.openStream();
        
        // 关于IO流的用法和写法一定要熟悉
        OutputStream os = new FileOutputStream("e:\\baidu.txt");  //如果没有可创建文件

        byte[] buffer = new byte[2048];
        int length = 0;
        
        //将访问的网页写入文件
        while (-1 != (length = is.read(buffer, 0, buffer.length)))
        {
            os.write(buffer, 0, length);
        }
        is.close();
        os.close();

	}

}

例子3

package add.socket.url;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class UrlConnection03 {


	public static void main(String[] args) throws IOException {
		URL url = new URL("http://localhost:8080/day09/testParam.html");

		BufferedReader br = new BufferedReader(new InputStreamReader(
				url.openStream()));
		
		/* 如果需要转码
		 * BufferedReader br = new BufferedReader(new InputStreamReader(
				url.openStream(), "utf-8")); */
		
		String line = null;
		
		while(null != (line = br.readLine())){
			System.out.println(line);
		}
		
		br.close();
		
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值