javaURL编程

1. URL

  1. URL(Uniform Resource Locator):统一资源定位符,它表示 Internet 上某一资源的地址。通过 URL 我们可以访问 Internet 上的各种网络资源,比如最常见的 www,ftp 站点。浏览器通过解析给定的 URL 可以在网络上查找相应的文件或其他资源。

  2. URL的基本结构由5部分组成:
    <传输协议>://<主机名>:<端口号>/<文件名>
    例如: http://192.168.1.100:8080/helloworld/index.jsp

  3. 为了表示URL,java.net 中实现了类 URL。我们可以通过下面的构造器来初始化一个 URL 对象:

    1. public URL (String spec):通过一个表示URL地址的字符串可以构造一个URL对象。例如:URL url = new URL (“http://www. baidu.com/”);
    2. public URL(URL context, String spec):通过基 URL 和相对 URL 构造一个 URL 对象。例如:URL downloadUrl = new URL(url, “download.html")
    3. public URL(String protocol, String host, String file); 例如:new URL(“http”, “www.baidu.com”, “download. html");
    4. public URL(String protocol, String host, int port, String file); 例如: URL gamelan = new URL(“http”, “www.csdn.net”, 80, “download.html");
  4. 类URL的构造方法都声明抛出非运行时异常,必须要对这一异常进行处理,通常是用 try-catch 语句进行捕获。

  5. 一个URL对象生成后,其属性是不能被改变的,但可以通过它给定的方法来获取这些属性:

    1. public String getProtocol( ) 获取该URL的协议名
    2. public String getHost( ) 获取该URL的主机名
    3. public String getPort( ) 获取该URL的端口号
    4. public String getPath( ) 获取该URL的文件路径
    5. public String getFile( ) 获取该URL的文件名
    6. public String getRef( ) 获取该URL在文件中的相对位置
    7. public String getQuery( ) 获取该URL的查询名
  6. URL的方法 openStream():能从网络上读取数据

2. 针对HTTP协议的URLConnection类

  1. 若希望输出数据,例如向服务器端的 CGI (公共网关接口-Common Gateway Interface-的简称,是用户浏览器和服务器端的应用程序进行连接的接口)程序发送一些数据,则必须先与URL建立连接,然后才能对其进行读写,此时需要使用 URLConnection 。
  2. URLConnection:表示到URL所引用的远程对象的连接。当与一个URL建立连接时,首先要在一个 URL 对象上通过方法== openConnection()== 生成对应的 URLConnection 对象。如果连接过程失败,将产生IOException.
    1. URL netchinaren = new URL (“http://www.csdn.net/index.shtml”);
    2. URLConnectonn u = netchinaren.openConnection( );
  3. 通过URLConnection对象获取的输入流和输出流,即可以与现有的CGI程序进行交互。
    1. public Object getContent( ) throws IOException
    2. public int getContentLength( )
    3. public String getContentType( )
    4. public long getDate( )
    5. public long getLastModified( )
    6. public InputStream getInputStream( )throws IOException
    7. public OutputSteram getOutputStream( )throws IOException

3.URL编程的例子

注:正确的异常处理方式应该是try-catch-finally,保证流的关闭,例子为了方便,直接抛出

//URL:统一资源定位符,一个URL的对象,对应着互联网上一个资源。
//我们可以通过调用URL对象调用相应的方法,将此资源读取"下载"
public class TestURL {
	public static void main(String[] args) throws Exception {
		//1.创建一个URL对象
		URL url = new URL("http://127.0.0.1:8080/examples/HelloWorld.txt?a=b");//File file = new File("文件的路径");
		/*
		 	public String getProtocol(  )     获取该URL的协议名
			public String getHost(  )           获取该URL的主机名
			public String getPort(  )            获取该URL的端口号
			public String getPath(  )           获取该URL的文件路径
			public String getFile(  )             获取该URL的文件名
			public String getRef(  )             获取该URL在文件中的相对位置
			public String getQuery(   )        获取该URL的查询名
		 */
		System.out.println(url.getProtocol());
		System.out.println(url.getHost());
		System.out.println(url.getPort());
		System.out.println(url.getPath());
		System.out.println(url.getFile());
		System.out.println(url.getRef());
		System.out.println(url.getQuery());
		
		//如何将服务端的资源读取进来:openStream
		InputStream is = url.openStream();
		byte[] b = new byte[1024];
		int len;
		while((len = is.read(b)) != -1) {
			String str = new String(b, 0, len);
			System.out.println(str);
		}
		is.close();
		
		//如果既有数据的输入,又有数据的输出,则考虑使用URLConnection
		URLConnection urlConn = url.openConnection();
		InputStream is1 = urlConn.getInputStream();
		FileOutputStream fos = new FileOutputStream(new File("abc.txt"));
		byte [] b1 = new byte[20];
		int len1;
		while((len1 = is1.read(b1)) != -1) {
			fos.write(b1,0,len1);
		}
		fos.close();
		is.close();
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值