【问题标题】:Getting a file from WAR file从 WAR 文件中获取文件
【发布时间】:2013-08-06 19:16:16
【问题描述】:

我正在尝试从我的 WAR 文件中获取一个文件。

URL url2 = getClass().getClassLoader().getResource("/package.xsd");

当我打印getPath() 时,输出如下:

/C:/workspacesFresh2/.metadata/.plugins/org.eclipse.wst.core/tmp1/wtpwebapps/GServer/WEB-INF/lib/GLibrary.jar!/package.xsd

所以,它找到了package.xsd,但我无法使用该路径打开它。

关于如何访问该文件的任何想法?

错误:

org.xml.sax.SAXParseException; schema_reference.4: Failed to read schema document 'file:/C:/workspacesFresh2/.metadata/.plugins/org.eclipse.server.core/tmp1/wtpwebapps/Server/WEB-INF/lib/GLibrary.jar!/package.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

谢谢

【问题讨论】:

  • 你能定义“打不开”吗?任何错误?堆栈跟踪?
  • 我已经收录了

标签: java url path classloader war


【解决方案1】:

你不能使用它的路径打开它,因为它不是一个普通的文件,它是一个 jar 中的文件。

访问它的一种方法是使用输入流

InputStream stream = this.getClass().
    getClassLoader().getResourceAsStream("/package.xsd");

你可以从输入流中读取它们

【讨论】:

    【解决方案2】:

    URL 的“路径”部分不一定与文件系统中的路径相同。实际上,您的 URL 不是文件 URL,因为它指向 JAR 文件。您无法获取路径并在文件系统中查找它。

    如果你想在你的应用程序中访问它,你必须使用流。

    另请参阅:URL on Wikipedia

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-30
      • 2015-09-08
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 2011-08-09
      相关资源
      最近更新 更多