当前位置: 首页 > news >正文

美容养生连锁东莞网站建设泰安集团

美容养生连锁东莞网站建设,泰安集团,快手秒赞秒评网站推广,济南中京网站建设公司转载自 对Java的URL类支持的协议进行扩展的方法JAVA默认提供了对file,ftp,gopher,http,https,jar,mailto,netdoc协议的支持。当我们要利用这些协议来创建应用时#xff0c;主要会涉及到如下几个类#xff1a;java.net.URL、java.net.URLConnection、InputStream。URL类默认…转载自   对Java的URL类支持的协议进行扩展的方法JAVA默认提供了对file,ftp,gopher,http,https,jar,mailto,netdoc协议的支持。当我们要利用这些协议来创建应用时主要会涉及到如下几个类java.net.URL、java.net.URLConnection、InputStream。URL类默认支持上述协议但是有时候我们想自定义协议怎么办呢?Java提供了三种方法可以支持这个扩展 1、URL.setURLStreamHandlerFactory(URLStreamHandlerFactory)URLStreamHandlerFactory(java.net.URLStreamHandlerFactory)这是一个接口定义如下 package java.net; /** * This interface defines a factory for {code URL} stream * protocol handlers. * p * It is used by the {code URL} class to create a * {code URLStreamHandler} for a specific protocol. * * author Arthur van Hoff * see java.net.URL * see java.net.URLStreamHandler * since JDK1.0 */ public interface URLStreamHandlerFactory { /** * Creates a new {code URLStreamHandler} instance with the specified * protocol. * * param protocol the protocol ({code ftp}, * {code http}, {code nntp}, etc.). * return a {code URLStreamHandler} for the specific protocol. * see java.net.URLStreamHandler */ URLStreamHandler createURLStreamHandler(String protocol); } 此接口需要实现createURLStreamHandler(String protocol)方法参数protocol为协议名称返回URLStreamHandler( java.net.URLStreamHandler )抽象类抽象方法定义如下 abstract protected URLConnection openConnection(URL u) throws IOException; 参数u为URL类型URL.openConnection间接调用这个方法返回URLConnection然后可以获取InputStream进而获取相应的数据(资源) 示例如下URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { Override public URLStreamHandler createURLStreamHandler(String protocol) { if(json.equals(protocol)){ return new URLStreamHandler(){ Override protected URLConnection openConnection(URL url) throws IOException { return new URLConnection(url){ public InputStream getInputStream() throws IOException { return new FileInputStream(d:/aaaa.txt); } Override public void connect() throws IOException { //建立连接 } }; } }; } else return null; } }); URL url new URL(json://json.url.com); InputStream in url.openConnection().getInputStream(); System.out.println(in.read()); 上述代码判断如果协议(protocal)为json则返回一个自定义的URLStreamHandler否则返回null对应其他Java本身已经支持的协议会不会造成影响呢 我们且看URL的一个构造方法(URL(String protocol, String host, int port, String file,URLStreamHandler handler) 中类似) public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException { String original spec; int i, limit, c; int start 0; String newProtocol null; boolean aReffalse; boolean isRelative false; // Check for permission to specify a handler if (handler ! null) { SecurityManager sm System.getSecurityManager(); if (sm ! null) { checkSpecifyHandler(sm); } } try { limit spec.length(); while ((limit 0) (spec.charAt(limit - 1) )) { limit--; //eliminate trailing whitespace } while ((start limit) (spec.charAt(start) )) { start; // eliminate leading whitespace } if (spec.regionMatches(true, start, url:, 0, 4)) { start 4; } if (start spec.length() spec.charAt(start) #) { /* were assuming this is a ref relative to the context URL. * This means protocols cannot start w/ #, but we must parse * ref URLs like: hello:there w/ a : in them. */ aReftrue; } for (i start ; !aRef (i limit) ((c spec.charAt(i)) ! /) ; i) { if (c :) { String s spec.substring(start, i).toLowerCase(); if (isValidProtocol(s)) { newProtocol s; start i 1; } break; } } // Only use our context if the protocols match. protocol newProtocol; if ((context ! null) ((newProtocol null) || newProtocol.equalsIgnoreCase(context.protocol))) { // inherit the protocol handler from the context // if not specified to the constructor if (handler null) { handler context.handler; } // If the context is a hierarchical URL scheme and the spec // contains a matching scheme then maintain backwards // compatibility and treat it as if the spec didnt contain // the scheme; see 5.2.3 of RFC2396 if (context.path ! null context.path.startsWith(/)) newProtocol null; if (newProtocol null) { protocol context.protocol; authority context.authority; userInfo context.userInfo; host context.host; port context.port; file context.file; path context.path; isRelative true; } } if (protocol null) { throw new MalformedURLException(no protocol: original); } // Get the protocol handler if not specified or the protocol // of the context could not be used if (handler null (handler getURLStreamHandler(protocol)) null) { throw new MalformedURLException(unknown protocol: protocol); } this.handler handler; i spec.indexOf(#, start); if (i 0) { ref spec.substring(i 1, limit); limit i; } /* * Handle special case inheritance of query and fragment * implied by RFC2396 section 5.2.2. */ if (isRelative start limit) { query context.query; if (ref null) { ref context.ref; } } handler.parseURL(this, spec, start, limit); } catch(MalformedURLException e) { throw e; } catch(Exception e) { MalformedURLException exception new MalformedURLException(e.getMessage()); exception.initCause(e); throw exception; } } 代码87行调用了getURLStreamHandler(protocol)此方法 static URLStreamHandler getURLStreamHandler(String protocol) { URLStreamHandler handler handlers.get(protocol); if (handler null) { boolean checkedWithFactory false; // Use the factory (if any) if (factory ! null) { handler factory.createURLStreamHandler(protocol); checkedWithFactory true; } // Try java protocol handler if (handler null) { String packagePrefixList null; packagePrefixList java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction( protocolPathProp,)); if (packagePrefixList ! ) { packagePrefixList |; } // REMIND: decide whether to allow the null class prefix // or not. packagePrefixList sun.net.www.protocol; StringTokenizer packagePrefixIter new StringTokenizer(packagePrefixList, |); while (handler null packagePrefixIter.hasMoreTokens()) { String packagePrefix packagePrefixIter.nextToken().trim(); try { String clsName packagePrefix . protocol .Handler; Class? cls null; try { cls Class.forName(clsName); } catch (ClassNotFoundException e) { ClassLoader cl ClassLoader.getSystemClassLoader(); if (cl ! null) { cls cl.loadClass(clsName); } } if (cls ! null) { handler (URLStreamHandler)cls.newInstance(); } } catch (Exception e) { // any number of exceptions can get thrown here } } } synchronized (streamHandlerLock) { URLStreamHandler handler2 null; // Check again with hashtable just in case another // thread created a handler since we last checked handler2 handlers.get(protocol); if (handler2 ! null) { return handler2; } // Check with factory if another thread set a // factory since our last check if (!checkedWithFactory factory ! null) { handler2 factory.createURLStreamHandler(protocol); } if (handler2 ! null) { // The handler from the factory must be given more // importance. Discard the default handler that // this thread created. handler handler2; } // Insert this handler into the hashtable if (handler ! null) { handlers.put(protocol, handler); } } } return handler; } 代码段 if (factory ! null) { handler factory.createURLStreamHandler(protocol);checkedWithFactory true;}这一段是从factory中获取相应协议的URLStreamHandler如果获取不到则从另一渠道获得(即不会对java已经支持的协议造成影响)就是我们讲的第2种方法 各个构造方法的调用关系代码如下 //#1 public URL(String spec) throws MalformedURLException { this(null, spec);//调用#2 URL(URL context, String spec) } //#2 public URL(URL context, String spec) throws MalformedURLException { this(context, spec, null);调用#6 URL(URL context, String spec, URLStreamHandler handler) } //#3 public URL(String protocol, String host, int port, String file) throws MalformedURLException { this(protocol, host, port, file, null);//调用#6 RL(String protocol, String host, int port, String file,URLStreamHandler handler) } //#4 public URL(String protocol, String host, String file) throws MalformedURLException { this(protocol, host, -1, file);//调用#3 URL(String protocol, String host, int port, String file) } //#5 public URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException{ //.... } //#6 public URL(URL context, String spec, URLStreamHandler handler) throws MalformedURLException{ //.... } //可以看出实质性逻辑都在#5和#6方法 2、 通过 JVM 启动参数 -Djava.protocol.handler.pkgs来设置 URLStreamHandler 实现类的包路径 比如-D java.protocol.handler.pkgscom.myprotocol.pkgs0|com.myprotocol.pkgs1多个用|分割java默认的包为sun.net.www.protocol设置了这个参数会拼接在默认的包之后即sun.net.www.protocol|com.myprotocol.pkgs0|com.myprotocol.pkgs1 看这段代码 if (handler null) { String packagePrefixList null; packagePrefixList java.security.AccessController.doPrivileged( new sun.security.action.GetPropertyAction( protocolPathProp,));//protocolPathProp的值为java.protocol.handler.pkgs if (packagePrefixList ! ) { packagePrefixList |; } // REMIND: decide whether to allow the null class prefix // or not. packagePrefixList sun.net.www.protocol;//拼接默认的pkgs StringTokenizer packagePrefixIter new StringTokenizer(packagePrefixList, |); while (handler null packagePrefixIter.hasMoreTokens()) {//遍历pkgs String packagePrefix packagePrefixIter.nextToken().trim(); try { String clsName packagePrefix . protocol .Handler;//类全名为pkgs.protocal.Handler Class? cls null; try { cls Class.forName(clsName); } catch (ClassNotFoundException e) { ClassLoader cl ClassLoader.getSystemClassLoader(); if (cl ! null) { cls cl.loadClass(clsName); } } if (cls ! null) { handler (URLStreamHandler)cls.newInstance(); } } catch (Exception e) { // any number of exceptions can get thrown here } } } 类的命名模式为 [pkgs].[protocol].Handler比如默认实现” sun.net.www.protocol.[protocol].Handler”, 比如HTTP 协议的对应的处理类名为 -sun.net. www.protocol.http.Handler  自定义协议例子如下 package com.myprotocol.pkgs0.json; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; public class Handler extends URLStreamHandler { Override protected URLConnection openConnection(URL u) throws IOException { return new URLConnection(u) { public InputStream getInputStream() throws IOException { return new FileInputStream(d:/aaaa.txt); } Override public void connect() throws IOException { // 建立连接 } }; } } 启动时命令java -Djava.protocol.handler.pkgscom.myp rotocol.pkgs0 其他参数 主类3、构造方法URL((URL)null, json://www.google.com,new URLStreamHandler(){...}) 这种方法直接设置Handler比较简单,不在赘述代理Proxy URLStreamHandler 覆盖openConnection(URL) 和openConnection(URL,Proxy) 两个方法即可
http://www.pierceye.com/news/953048/

相关文章:

  • 作风建设提升年活动网站毕节公司做网站
  • access数据库网站广州建网站哪儿济南兴田德润简介
  • 上海网站建设seo抖音短剧推广怎么做
  • 京东网站建设策划书网站建设常用编程语言
  • 济南教育论坛网站建设page n wordpress
  • 网站域名在山东备案却在苏州产教融合信息门户网站建设方案
  • 南京网站网站建设传奇网页
  • 网站后台更新怎么做详情页怎么设计
  • 网站怎么做导航wordpress付费破解
  • 宁津网站建设国内免费设计素材网站
  • 泰安有口碑的企业建站公司二手汽车手机网站模板
  • 网站百度快照怎么做网站调用谷歌地图
  • 扫描二维码进入公司网站怎样做代做关键词收录排名
  • flash美食网站论文架设一个网站需要多少钱
  • 做教育视频网站用什么平台好wordpress文章 代码块
  • 网站 部署 域名深圳网站建设yuntianxia
  • 做调查的网站推荐移动端网站开发教程
  • 上海品牌网站建设公司排名女生学网络营销这个专业好吗
  • 优质的邵阳网站建设企业邮箱免费登录入口
  • 网站做seo多少钱wordpress点击分类目录空白
  • 黄埔网站建设 信科网络中国企业商铺网
  • 济南快速网站排名网站开发模板系统
  • 厦门市app开发网站建设公司亚马逊雨林在地图上的位置
  • qq空间个人网站网页设计作业个人简历代码怎么写
  • 宁波网站建设团队微信网页制作的软件
  • 社区网站推广方案百度直播推广
  • 上海网站seo诊断吉林网站优化
  • 玉田网站建设做重视频网站
  • 发放淘宝优惠券的网站怎么做网站建设理论依据
  • 信用渭南网站建设做网站实例