JavaTM Platform
Standard Ed. 6

java.net
类 JarURLConnection

java.lang.Object
  继承者 java.net.URLConnection
      继承者 java.net.JarURLConnection

public abstract class JarURLConnection
extends URLConnection

连接到 Java ARchive (JAR) 文件或 JAR 文件中条目的 URL Connection。

JAR URL 的语法为:

 jar:<url>!/{entry}
 

例如:

jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class

应该使用 Jar URL 来引用 JAR 文件或 JAR 文件中的条目。上述示例是一个引用 JAR 条目的 JAR URL。如果省略条目名,则 URL 引用整个 JAR 文件: jar:http://www.foo.com/bar/baz.jar!/

当用户知道他们创建的 URL 为 JAR URL 并且需要特定于 JAR 的功能时,应该将一般 URLConnection 强制转换为 JarURLConnection。例如:

 URL url = new URL("jar:file:/home/duke/duke.jar!/");
 JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
 Manifest manifest = jarConnection.getManifest();
 

JarURLConnection 实例只能用于从 JAR 文件读取内容。使用此类获取 OutputStream 从而修改或写入基础 JAR 文件是不可能的。

示例:

Jar 条目
jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class
Jar 文件
jar:http://www.foo.com/bar/baz.jar!/
Jar 目录
jar:http://www.foo.com/bar/baz.jar!/COM/foo/

!/ 被视为分隔符

通过 new URL(context, spec) 构造 JAR url 时,将应用以下规则:

从以下版本开始:
1.2
另请参见:
URL, URLConnection, JarFile, JarInputStream, Manifest, ZipEntry

字段摘要
protected  URLConnection jarFileURLConnection
          到 JAR 文件 URL 的连接,如果已启动连接。
 
从类 java.net.URLConnection 继承的字段
allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches
 
构造方法摘要
protected JarURLConnection(URL url)
          创建到指定 URL 的新 JarURLConnection。
 
方法摘要
 Attributes getAttributes()
          如果此连接的 URL 指向 JAR 文件条目,则返回其 Attribute 对象;否则返回 null。
 Certificate[] getCertificates()
          如果此连接的 URL 指向 JAR 文件条目,则返回其 Certificate 对象;否则返回 null。
 String getEntryName()
          返回此连接的条目名称。
 JarEntry getJarEntry()
          返回此连接的 JAR 条目对象(如果有)。
abstract  JarFile getJarFile()
          返回此连接的 JAR 文件。
 URL getJarFileURL()
          返回此连接的 Jar 文件的 URL。
 Attributes getMainAttributes()
          返回此连接的 JAR 文件的主要 Attribute。
 Manifest getManifest()
          返回此连接的 Manifest;如果没有,则返回 null。
 
从类 java.net.URLConnection 继承的方法
addRequestProperty, connect, getAllowUserInteraction, getConnectTimeout, getContent, getContent, getContentEncoding, getContentLength, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderField, getHeaderField, getHeaderFieldDate, getHeaderFieldInt, getHeaderFieldKey, getHeaderFields, getIfModifiedSince, getInputStream, getLastModified, getOutputStream, getPermission, getReadTimeout, getRequestProperties, getRequestProperty, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setConnectTimeout, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setIfModifiedSince, setReadTimeout, setRequestProperty, setUseCaches, toString
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

jarFileURLConnection

protected URLConnection jarFileURLConnection
到 JAR 文件 URL 的连接,如果已启动连接。此方法应该由连接设置。

构造方法详细信息

JarURLConnection

protected JarURLConnection(URL url)
                    throws MalformedURLException
创建到指定 URL 的新 JarURLConnection。

参数:
url - URL
抛出:
MalformedURLException - 如果在规范字符串中找不到合法协议或者无法解析该字符串。
方法详细信息

getJarFileURL

public URL getJarFileURL()
返回此连接的 Jar 文件的 URL。

返回:
此连接的 Jar 文件的 URL。

getEntryName

public String getEntryName()
返回此连接的条目名称。如果对应于此连接的 JAR 文件 URL 指向 JAR 文件而不是 JAR 文件条目,则此方法返回 null。

返回:
此连接的条目名称(如果有)。

getJarFile

public abstract JarFile getJarFile()
                            throws IOException
返回此连接的 JAR 文件。

返回:
此连接的 JAR 文件。如果连接是到 JAR 文件的条目的连接,则返回 JAR 文件对象
抛出:
IOException - 如果在试图连接到此连接的 JAR 文件时发生 IOException。
另请参见:
URLConnection.connect()

getManifest

public Manifest getManifest()
                     throws IOException
返回此连接的 Manifest;如果没有,则返回 null。

返回:
与此连接的 JAR 文件对象相对应的清单对象。
抛出:
IOException - 如果获取此连接的 JAR 文件导致抛出 IOException。
另请参见:
getJarFile()

getJarEntry

public JarEntry getJarEntry()
                     throws IOException
返回此连接的 JAR 条目对象(如果有)。如果对应于此连接的 JAR 文件 URL 指向 JAR 文件而不是 JAR 文件条目,则此方法返回 null。

返回:
此连接的 JAR 条目对象;如果此连接的 JAR URL 指向 JAR 文件,则返回 null。
抛出:
IOException - 如果获取此连接的 JAR 文件导致抛出 IOException。
另请参见:
getJarFile(), getJarEntry()

getAttributes

public Attributes getAttributes()
                         throws IOException
如果此连接的 URL 指向 JAR 文件条目,则返回其 Attribute 对象;否则返回 null。

返回:
如果此连接的 URL 指向 JAR 文件条目,则返回其 Attribute 对象;否则返回 null。
抛出:
IOException - 如果获取 JAR 条目导致抛出 IOException。
另请参见:
getJarEntry()

getMainAttributes

public Attributes getMainAttributes()
                             throws IOException
返回此连接的 JAR 文件的主要 Attribute。

返回:
此连接的 JAR 文件的主要 Attribute。
抛出:
IOException - 如果获取清单导致抛出 IOException。
另请参见:
getJarFile(), getManifest()

getCertificates

public Certificate[] getCertificates()
                              throws IOException
如果此连接的 URL 指向 JAR 文件条目,则返回其 Certificate 对象;否则返回 null。只有通过从条目输入流一直读取到流末尾的方式完全验证了连接之后,才能调用此方法。否则,此方法将返回 null

返回:
如果此连接的 URL 指向 JAR 文件条目,则返回其 Certificate 对象;否则返回 null。
抛出:
IOException - 如果获取 JAR 条目导致抛出 IOException。
另请参见:
getJarEntry()

JavaTM Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策