---
title: "URL与URI"
author: "8366"
date: "2009-07-05T15:48:10Z"
tags: []
slug: "url-uri"
draft: false
lastmod: "2009-07-19T18:35:14Z"
id: 420412
url: "https://8366.iteye.com/blog/420412"
---

**\*** URL(Uniform Resource Locator )，通用资源定位符。[http://www.mybole.com.cn/index.asp](http://www.mybole.com.cn/index.asp)就是一个URL。

**\***URI(Uniform Resource Identifier)，通用资源标识符。

**\***URI纯粹是个符号结构，用于指定构成Web资源的字符串的各个不同部分。URL是一种特殊类型的URI，它包含了用于查找某个资源的足够信息。其它的URI，例如：[mailto:myoble@mybole.com.cn](mailto:myoble@mybole.com.cn)则不属于定位符，因为它里面不存在根据该标识符来查找的任何数据。这种URI称为URN(通用资源名)。

**\***在Java库中，URI类不包含用于访问通用资源标识符设定的任何方法，它的唯一作用是进行分析。相反，URL类则可以打开到达资源的一个字符串。

实验：通过URL 来下载服务器上的一个资源文件，需要Tomcat服务器支持

 

代码：

 

 

```
package cn.com.xinli.test.socket;

import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.*;

public class HttpDownLoad
{
	public static void main(String[] args)
	  {
	    JFrame jf=new JFrame("下载程序");
	    jf.setSize(600,400);
	    jf.setLocation(100,100);
	    JPanel p=new JPanel();
	    JLabel l=new JLabel("Please input URL:");
	    final JTextField tf=new JTextField(30);
	    p.add(l);
	    p.add(tf);
	    jf.getContentPane().add(p,"North");
	    final JTextArea ta=new JTextArea();
	    jf.getContentPane().add(ta,"Center");
	    JButton btn=new JButton("Download");
	    jf.getContentPane().add(btn,"South");
	    btn.addActionListener(new ActionListener() {
	          public void actionPerformed(ActionEvent e) {
	            String str=tf.getText();
	            System.out.println("输入URI的内容："+str);
	            String fileName=str.substring(str.lastIndexOf("/")+1);
	            /*下载文件的名字*/
	            System.out.println("fileName:"+fileName);
	            
	            try {
	              URL url=new URL(str);
	              URLConnection urlConn=url.openConnection();
	              /**
	               * 由于windos 系统的换行符是\r\n,linux 系统的换行符是\n,
	               * 为了通用我们可以使用系统属性的方式得到换行符
	               * 
	               */
	              String line=System.getProperty("line.separator");
	              ta.append("Host: "+url.getHost());
	              ta.append(line);
	              ta.append("Port: "+url.getDefaultPort());
	              ta.append(line);
	              ta.append("ContentType: "+urlConn.getContentType());
	              ta.append(line);
	              ta.append("ContentLength: "+urlConn.getContentLength());
	              InputStream is=urlConn.getInputStream();
	              
	              /**
	               * 以下注释的部分为按行读取 关键代码：strLine=br.readLine())!=null
	               * 为注释的部分为按字节读取，每次读一个字节 关键代码：(data=is.read())!=-1
	               * 
	               * 文本和html等格式文件可以按行读取
	               * 图像声音文件必须按字节读取,因为这个时候已经没有 行 的概念
	               * 其实所有的文件都可以按字节读取，建议使用按字节读取 
	               */
	              //InputStreamReader isr=new InputStreamReader(is);
	              //BufferedReader br=new BufferedReader(isr);
	              
	              FileOutputStream fos=new FileOutputStream(fileName);
	              //String strLine;
	              //while((strLine=br.readLine())!=null)
	              int data;
	              while((data=is.read())!=-1)
	              {
//	                fos.write(strLine.getBytes());
	            	/**
	            	 * 按行读取的时候必须有此句,因为按行读取的时候，每次对一行，并不会
	            	 * 把行分割符号写入输出流，需要我们手动添加
	            	 * 
	            	 */
	  //            fos.write(line.getBytes());
	                fos.write(data);
	              }
	              //br.close();
	              is.close();
	              fos.close();
	            }
	            catch (Exception ex) {
	              ex.printStackTrace();
	            }

	          }
	        });
	    jf.addWindowListener(new WindowAdapter() {
	         public void windowClosing(WindowEvent e) {
	           System.exit(0);
	         }
	       });
	    jf.show();
	  }
}
```

 

 

 

启动TOMCAT，启动应用程序，下载 /bz/images 应用下的图片文件到 测试程序的根路径下

 

![](/upload/picture/pic/40018/8f7ddd06-e394-3874-81d7-fb5d1f6f2002.bmp) 

 

 

成功将服务器上/bz/images/下的一个图片文件下载到本程序的 根路径下！

 

 

**发挥：**

 

此程序以后可以应用到 从另外一个远端的服务器上下载文件，而不需要走web层，从response对象中得到输出流