java 从 JAR 获取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3447258/
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
get File from JAR
提问by Dónal
I'm using Spring's Resourceabstraction to work with resources (files) in the filesystem. One of the resources is a file inside a JAR file. According to the following code, it appears the reference is valid
我正在使用 Spring 的Resource抽象来处理文件系统中的资源(文件)。资源之一是 JAR 文件中的文件。根据下面的代码,看来引用是有效的
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
// The path to the resource from the root of the JAR file
Resource fileInJar = resourcePatternResolver.getResources("/META-INF/foo/file.txt");
templateResource.exists(); // returns true
templateResource.isReadable(); // returns true
At this point, all is well, but then when I try to convert the Resourceto a File
在这一点上,一切都很好,但后来当我尝试将转换Resource为File
templateResource.getFile();
I get the exception
我得到了例外
java.io.FileNotFoundException: class path resource [META-INF/foo/file.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/m2repo/uic-3.2.6-0.jar!/META-INF/foo/file.txt
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:198)
at org.springframework.core.io.ClassPathResource.getFile(ClassPathResource.java:174)
What is the correct way to get a Filereference to a Resourcethat exists inside a JAR file?
获取File对ResourceJAR 文件中存在的a 的引用的正确方法是什么?
回答by Michael Borgwardt
What is the correct way to get a File reference to a Resource that exists inside a JAR file?
获取对 JAR 文件中存在的资源的文件引用的正确方法是什么?
The correct way is not doing that at all because it's impossible. A Filerepresents an actual file on a file system, which a JAR entry is not, unless you have a special file system for that.
正确的方法是根本不这样做,因为这是不可能的。AFile代表文件系统上的实际文件,JAR 条目不是,除非您有一个特殊的文件系统。
If you just need the data, use getInputStream(). If you have to satisfy an API that demands a Fileobject, then I'm afraid the only thing you can do is to create a temp file and copy the data from the input stream to it.
如果您只需要数据,请使用getInputStream(). 如果你必须满足一个需要File对象的 API ,那么恐怕你唯一能做的就是创建一个临时文件并将数据从输入流复制到它。
回答by Bozho
If you want to read it, just call resource.getInputStream()
如果您想阅读它,只需致电 resource.getInputStream()
The exception message is pretty clear - the file does not reside on the file-system, so you can't have a Fileinstance. Besides - what will do do with that File, apart from reading its content?
异常消息非常清楚 - 该文件不驻留在文件系统上,因此您不能拥有File实例。此外 -File除了阅读它的内容之外,它还能做什么?
回答by naikus
A quick look at the linkyou provided for Resource documentation, says the following:
快速浏览您为 Resource 文档提供的链接,内容如下:
Throws: IOException if the resource cannot be resolved as absolute file path,
i.e. if the resource is not available in a file system
Maybe the text file is inside a jar? In that case you will have to use getInputStream()to read its contents.
也许文本文件在一个罐子里?在这种情况下,您将不得不使用getInputStream()阅读其内容。
回答by Igor
Just adding an example to the answers here. If you need a File(and not just the contents of it) from within your JAR, you need to create a temporary file from the resource first. (The below is written in Groovy):
只需在此处的答案中添加一个示例。如果您需要File从 JAR 中获取(而不仅仅是其中的内容),则需要先从资源创建一个临时文件。(以下是用Groovy写的):
InputStream inputStream = resourceLoader.getResource('/META-INF/foo/file.txt').inputStream
File tempFile = new File('file.txt')
OutputStream outputStream = new FileOutputStream(tempFile)
try {
IOUtils.copy(inputStream, outputStream)
} catch (IOException e) {
// Handle exception
} finally {
outputStream.close()
}

