栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java URL java.net.URL的介绍

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java URL java.net.URL的介绍

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

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

 

1.构造方法

URL(String spec) 
          根据 String 表示形式创建 URL 对象。
URL(String protocol, String host, int port, String file) 
          根据指定 protocol、host、port 号和 file 创建 URL 对象。
 
  1. public URL(String spec) throws MalformedURLException {

  2. this(null, spec);

  3. }

创建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;

public class ChineseToUrl {
    
    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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911359.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号