【发布时间】:2016-04-09 13:44:41
【问题描述】:
使用 JarURLConnection,我可以使用以下代码结构从托管在 Dropbox 上的 JAR 中读取文件(例如 version.txt):
public static void checkForUpdates() {
JarURLConnection jarConn = null;
try {
System.out.println("Checking for updates..");
URL updateURL = new URL("jar:https://www.dropbox.com/s/.../foo.jar?dl=1!/version.txt");
jarConn = (JarURLConnection) updateURL.openConnection();
JarFile jarFile = jarConn.getJarFile();
InputStream inputStream = jarFile.getInputStream(jarConn.getJarEntry());
BufferedReader versionTXT = new BufferedReader(new InputStreamReader(inputStream));
/* Version comparing left out */
// If there is an update:
System.out.println("Update found!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (jarConn != null) {
// This doesn't seem to work
jarConn.getInputStream().close();
}
}
}
当我第一次调用这个方法时它可以正常工作;您会看到“检查更新”消息和“结果”消息之间存在延迟。
当我在 Dropbox 上上传新的 foo.jar 并再次运行 checkForUpdates() 方法时(没有重新启动 JVM),它将使用“旧”jar,并且没有延迟检查和结果消息之间。当我确实重新启动 JVM 时,它将使用“新”jar 并显示消息之间的延迟。
除了关闭 InputStream(这似乎不起作用)之外,还有什么方法可以关闭 JarURLConnection?
我尝试了以下方法:
- 关闭 JarURLConnection 的 OutputStream -> 抛出一个错误,指出连接没有 OutputStream。
- 关闭 URLConnections Input- & OutputStream(通过在将其转换为 JarURLConnection 之前创建一个新变量)-> 关闭 InputStream 似乎没有任何作用,而关闭 OutputStream 会引发相同的错误。
- 关闭 BufferedReader -> 无效。
如果无法关闭 JarURLConnection,是否可以创建一个 重新连接的新 JarURLConnection?重启 JVM 显然做了一些它重新连接的事情,是否可以在不重启 JVM 的情况下模拟 ?
提前致谢。
【问题讨论】:
-
我刚刚在 InputStream 和 BufferedReader 上测试了一个 try-resource,但没有区别。
标签: java jar urlconnection