java - Applet 的 getCodeBase 返回 null

标签 java file applet

我有一个小程序,需要打开文件流。该文件是一个本地文件,位于小程序和 HTML 文件所在的位置:

    URL localURL = new URL(getCodeBase(), "pixs/icons.zip");
    InputStream localInputStream =localURL.openStream();

它以前工作正常,但升级到 java 1.7 build 25 后,getCodeBase() 总是返回 null。 这实际上已记录在案!唉 - 没有关于如何克服它的建议。

有用的一件事是使用完整路径:

   URL localURL = new URL("file:c:/myFolder/pixs/icons.zip");

是否有其他选项可以在不使用完整路径的情况下解决该问题?

最佳答案

也许你可以使用 getDocumentBase 反而。取决于你的设置结构,代码库和文档之间是否有密切的关系。 没有永久的解决方案:getDocumentBase根据 bug #8019177 在 7u40 中以相同的方式进行了修改.

如果没有,那么您可以尝试使用 getResource 从您的 JAR 中获取 URL,例如applet 的类文件,然后反汇编该 URL 以获取 JAR 的位置,从而获取代码库。这是未经测试,所以如果您尝试过,请随时编辑这篇文章。

最后但同样重要的是,由于该更改仅影响本地小程序,您可以运行(本地或公共(public))网络服务器来为该小程序提供服务。

如果你想要更官方的声明,我会引用 the #8017250 bug report :

If applet need to load resource:

  • if the resource is in applet JAR(s), they should be able to load it with ClassLoader.getResoruceAsStream directly, without needing the codebase information.
  • if the resource is in arbitary location, not inside applet JAR, they should have other ways to get to that location, since it's not part of the applet resource anyway. (e.g. user.home java system property, provided that their applet has all-permissions)

http://www.duckware.com/tech/java-security-clusterfuck.html (感谢 this post )提到了一些其他的选择。受 future Oracle 修改应用程序影响的可能性最小的是使用 location.href在包含的 HTML 页面中,例如写 <applet>来自 JavaScript 的标记。

关于java - Applet 的 getCodeBase 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18568158/

相关文章:

java - Android 应用程序内存使用率高

java - 不将数据保存到数据库中。 Hibernate和Spring的连接

java - 尝试使用 "getNextAlarmClock()"获取下一个闹钟时间,但没有得到正确的答案

读取 .csv 文件时出现 java.io.FileNotFoundException

java - Chrome下数字签名小程序的替代品

java - 小程序无法从 jar 加载类

java - 初始化变量的正确方法

c++ - 从 C++ 文件中读取特定行

python - 检查文件中是否有与输入匹配的文本

C++ - 将 FILE* 转换为 CHAR*