简体   繁体   English

从JAR获取文件

[英]get File from JAR

I'm using Spring's Resource abstraction to work with resources (files) in the filesystem. 我正在使用Spring的Resource抽象来处理文件系统中的资源(文件)。 One of the resources is a file inside a JAR file. 其中一个资源是JAR文件中的文件。 According to the following code, it appears the reference is valid 根据以下代码,似乎引用有效

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 Resource to 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 File reference to a Resource that exists inside a JAR file? 获取对JAR文件中存在的ResourceFile引用的正确方法是什么?

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 File represents an actual file on a file system, which a JAR entry is not, unless you have a special file system for that. File表示文件系统上的实际文件,而JAR条目不是,除非您有特殊的文件系统。

If you just need the data, use getInputStream() . 如果您只需要数据,请使用getInputStream() If you have to satisfy an API that demands a File object, 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. 如果您必须满足需要File对象的API,那么我担心您唯一能做的就是创建一个临时文件并将输入流中的数据复制到它。

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 File instance. 异常消息非常清楚 - 文件不驻留在文件系统上,因此您不能拥有File实例。 Besides - what will do do with that File , apart from reading its content? 此外 - 除了阅读其内容外,该File将如何处理?

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. 如果您需要从JAR中获取File (而不仅仅是其中的内容),则需要首先从资源创建临时文件。 (The below is written in Groovy): (以下是用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()
    }

A quick look at the link you provided for Resource documentation, says the following: 快速浏览一下您为资源文档提供的链接 ,说明如下:

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? 也许文本文件在jar里面? In that case you will have to use getInputStream() to read its contents. 在这种情况下,您将不得不使用getInputStream()来读取其内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM