java URL java.net.URL类的使用 详细介绍 URL转义 解决URL中文字符

 类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,也就是我们常说的域名、浏览器访问地址。

在Java中就是使用URL访问网络资源,获取url对象的相关信息。

下面介绍具体是使用

1.构造方法,一般使用较多的就下面这两个,可以参照api

http://www.javaweb.cc/help/JavaAPI1.6/index.html?java/nio/ReadOnlyBufferException.html

URL(String spec) 
          根据 String 表示形式创建 URL 对象。
URL(String protocol, String host, int port, String file) 
          根据指定 protocolhostport 号和 file 创建 URL 对象。
public URL(String spec) throws MalformedURLException {
        this(null, spec);
    }

创建URL对象必须捕获MalformedURLException异常,可能URL有缺陷。

2.方法

 StringgetAuthority() 
          获取此 URL 的授权部分。
 ObjectgetContent() 
          获取此 URL 的内容。
 ObjectgetContent(Class[] classes) 
          获取此 URL 的内容。
 intgetDefaultPort() 
          获取与此 URL 关联协议的默认端口号。
 StringgetFile() 
          获取此 URL 的文件名。
 StringgetHost() 
          获取此 URL 的主机名(如果适用)。
 StringgetPath() 
          获取此 URL 的路径部分。
 intgetPort() 
          获取此 URL 的端口号。
 StringgetProtocol() 
          获取此 URL 的协议名称。
 StringgetQuery() 
          获取此 URL 的查询部分。
 StringgetRef() 
          获取此 URL 的锚点(也称为“引用”)。
 StringgetUserInfo() 
          获取此 URL 的 userInfo 部分。
 inthashCode() 
          创建一个适合哈希表索引的整数。
 URLConnectionopenConnection() 
          返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
 URLConnectionopenConnection(Proxy proxy) 
          与 openConnection() 类似,所不同是连接通过指定的代理建立;不支持代理方式的协议处理程序将忽略该代理参数并建立正常的连接。
 InputStreamopenStream() 
          打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream
 booleansameFile(URL other) 
          比较两个 URL,不包括片段部分。
protected  voidset(String protocol, String host, int port, String file, String ref) 
          设置 URL 的字段。
protected  voidset(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) 
          设置 URL 的指定的 8 个字段。
static voidsetURLStreamHandlerFactory(URLStreamHandlerFactory fac) 
          设置应用程序的 URLStreamHandlerFactory
 StringtoExternalForm() 
          构造此 URL 的字符串表示形式。
 StringtoString() 
          构造此 URL 的字符串表示形式。
 URItoURI() 
          返回与此 URL 等效的 URI

 

具体使用:

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class URLTest {
    public static void UrlTest(){
        try {
            // 创建一个URL实例
            URL url = new URL("http://www.imooc.com");
            System.out.println("协议:" + url.getProtocol());
            System.out.println("主机:" + url.getHost());
            System.out.println("授权:" + url.getAuthority());
            System.out.println("内容:" + url.getContent());
            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());
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}

 

3.URL转义 包含中文的URL转换

package i.publicclasslib;
/**
 * 汉字转换成URL码
 * @author CYZ
 *
 */
public class ChineseToUrl {
	/**
	 * 
	 * @param s 需要转换的中文URL
	 * @return 编译成功,返回URL码
	 */
	public static String ChineseToUrls(String s){
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;
				try {
                    //指定需要的编码类型
					b = String.valueOf(c).getBytes("utf-8");
				} catch (Exception ex) {
					System.out.println(ex);
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}
}

4.后续会继续更新其他内容。

  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值