java 在 EAR 中的 JAR 中获取文件的绝对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/791897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the absolute path of a file within a JAR within an EAR?
提问by Andrew Swan
I have a J2EE app deployed as an EAR file, which in turn contains a JAR file for the business layer code (including some EJBs) and a WAR file for the web layer code. The EAR file is deployed to JBoss 3.2.5, which unpacks the EAR and WAR files, but not the JAR file (this is not the problem, it's just FYI).
我有一个部署为 EAR 文件的 J2EE 应用程序,它又包含一个用于业务层代码(包括一些 EJB)的 JAR 文件和一个用于 Web 层代码的 WAR 文件。EAR 文件部署到 JBoss 3.2.5,它会解压 EAR 和 WAR 文件,但不会解压 JAR 文件(这不是问题,仅供参考)。
One of the files within the JAR file is an MS Word template whose absolute path needs to be passed to some native MS Word code (using Jacob, FWIW).
JAR 文件中的文件之一是 MS Word 模板,其绝对路径需要传递给某些本机 MS Word 代码(使用Jacob、FWIW)。
The problem is that if I try to obtain the File like this (from within some code in the JAR file):
问题是,如果我尝试获取这样的文件(从 JAR 文件中的某些代码中):
URL url = getClass().getResource("myTemplate.dot");
File file = new File(url.toURI()); // <= fails!
String absolutePath = file.getAbsolutePath();
// Pass the absolutePath to MS Word to be opened as a document
... then the java.io.Fileconstructor throws the IllegalArgumentException "URI is not hierarchical". The URL and URI both have the same toString() output, namely:
...然后java.io.File构造函数抛出 IllegalArgumentException “URI 不是分层的”。URL 和 URI 都有相同的 toString() 输出,即:
jar:file:/G:/jboss/myapp/jboss/server/default/tmp/deploy/tmp29269myapp.ear-contents/myapp.jar!/my/package/myTemplate.dot
This much of the path is valid on the file system, but the rest is not (being internal to the JAR file):
大部分路径在文件系统上是有效的,但其余的不是(在 JAR 文件内部):
G:/jboss/myapp/jboss/server/default/tmp/deploy/tmp29269myapp.ear-contents
What's the easiest way of getting the absolute path to this file?
获取此文件的绝对路径的最简单方法是什么?
采纳答案by Andrew Swan
My current solution is to copy the file to the server's temporary directory, then use the absolute path of the copy:
我目前的解决方法是将文件复制到服务器的临时目录,然后使用复制的绝对路径:
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File temporaryFile = new File(tempDir, "templateCopy.dot");
InputStream templateStream = getClass().getResourceAsStream("myTemplate.dot");
IOUtils.copy(templateStream, new FileOutputStream(temporaryFile));
String absolutePath = temporaryFile.getAbsolutePath();
I'd prefer a solution that doesn't involve copying the file.
我更喜欢不涉及复制文件的解决方案。
回答by Chris Thornhill
Unless the code or application you are passing the URI String to accepts a format that specifies a location within a jar/zip file, your solution of copying the file to a temporary location is probably the best one.
除非您将 URI 字符串传递给的代码或应用程序接受指定 jar/zip 文件中位置的格式,否则将文件复制到临时位置的解决方案可能是最好的解决方案。
If these files are referenced often, you may want to cache the locations of the extract files and just verify their existance each time they are needed.
如果经常引用这些文件,您可能希望缓存提取文件的位置,并在每次需要时验证它们的存在。
回答by Thilo
You should copy the contents to a temporary file (potentially with a cache), because trying to get to internal files of the application container is a dependency you want to avoid. There may not even be an extracted file at all (it can load from the JAR directly).
您应该将内容复制到一个临时文件(可能带有缓存),因为尝试访问应用程序容器的内部文件是您想要避免的依赖项。甚至可能根本没有提取的文件(它可以直接从 JAR 加载)。

