java网络学习之FileURLConnection类学习(5)

FileURLConnection 和jarUrlConnection 的区别
见示例:
public static void main(String[] args) throws IOException {
    String  TEST_JAR_PATH="C://1.jar";
    File jarFile = new File(TEST_JAR_PATH);
    URL jarFileURL = jarFile.toURI().toURL();//  返回file:/c://1.jar

    String jarEntryUrlString = "jar:" + jarFileURL.toString() + "!/org/apache/markt/leaks/test.txt";
    URL jarEntryUrl = new URL(jarEntryUrlString);// 返回jar:file:/c://1.jar

    // Disable caching by default for JAR URLs
    URLConnection.setDefaultUseCaches("JAR", false);

    // Check caching for a URL type other than JAR
    // File is the simplest
    FileURLConnection fileUrlConn = (FileURLConnection) jarFileURL.openConnection();
    System.out.println(fileUrlConn.getUseCaches());
    fileUrlConn.connect();
    System.out.println(fileUrlConn.getUseCaches());
    fileUrlConn.getInputStream().close();
    
    // Check JARs
    JarURLConnection jarEntryUrlConn = (JarURLConnection) jarEntryUrl.openConnection();
    System.out.println(jarEntryUrlConn.getUseCaches());
    jarEntryUrlConn.connect();
    System.out.println(jarEntryUrlConn.getUseCaches());
    jarEntryUrlConn.getInputStream().close();
}
 可以看出 FileURLConnection 是基于file 协议的url ,jarUrlConnection 是基于jar 协议的url

当一个jar 文件在本地系统存在时,应该使用什么连接,取决于Url 使用的协议。

 

FileURLConnection 常用方法见下表:

java网络学习之FileURLConnection类学习(5)_第1张图片

   

你可能感兴趣的:(java,网络)