简体   繁体   English

在OSGi环境中使用OPCPackage从类路径加载文件

[英]Load file from classpath using OPCPackage in OSGi environment

I'm trying to load a rather large .xslx file (29MB) to process in Java using POI. 我正在尝试加载一个相当大的.xslx文件(29MB)来使用POI在Java中进行处理。

Because of the uncompressed file size when reading the file in memory using an InputStream , I run into heap space issues. 由于使用InputStream在内存中读取文件时未压缩的文件大小,我遇到了堆空间问题。

As recommended on Stackoverflow, I make use of an OPCPackage to not have to load the entire file in memory. 正如Stackoverflow上的建议,我使用OPCPackage不必将整个文件加载到内存中。

I try to load the file using the OPCPackage open method. 我尝试使用OPCPackage open方法加载文件。 This method accepts: 此方法接受:

  • File 文件
  • InputStream 的InputStream (Cannot load file in inputstream - OutOfMemory) (无法在inputstream中加载文件 - OutOfMemory)
  • Path 路径

File option 文件选项

Problem is that we are working in an OSGi environment, so when trying to create a File with the asset path. 问题是我们正在OSGi环境中工作,因此在尝试使用资产路径创建文件时。 The asset path is in fact a link to the bundle 资产路径实际上是捆绑的链接

URL url = getClass().getClassLoader().getResource("/excel/file.xslx");
File file = new File(url.toURI()); // URI = bundle://449.124:/excel/file.xlsx

So following Exception occurs: java.lang.IllegalArgumentException: URI scheme is not "file" 因此发生以下异常: java.lang.IllegalArgumentException: URI scheme is not "file"

Trying to use the URL path instead also is not successful: 尝试使用URL路径也不成功:

File file = new File(url.getPath()); // Path= /excel/file.xslx
if(!file.exists()){
    // FILE DOES NOT EXIST
}

Path option 路径选项

When I try to use the path open method, it cannot seem to resolve the path, since I always get following Exception: java.lang.IllegalStateException: Zip File is closed 当我尝试使用路径打开方法时,它似乎无法解析路径,因为我总是得到以下异常: java.lang.IllegalStateException: Zip File is closed

opcPackage = OPCPackage.open(url.toURI().getPath()); // Path= /excel/file.xslx

Any ideas on how I can reference my file from within a bundle environment in order for it to load? 关于如何从捆绑环境中引用我的文件以便加载它的任何想法? Or am I missing something obvious here? 或者我错过了一些明显的东西?

If you cannot process the input stream, then you could copy the input stream to a File in the bundle data area and then process that file. 如果无法处理输入流,则可以将输入流复制到捆绑数据区域中的文件,然后处理该文件。

Resources in bundles do not stand alone on the file system, so you would need to "extract" them to a file if necessary. 捆绑包中的资源不会独立于文件系统,因此如有必要,您需要将它们“提取”到文件中。

如果您只需要Excel文件中的一些部分,您还可以使用Apache POI的流式API ,这样您就不需要将文件存储在临时磁盘空间中,并且可以处理任意大型文件。

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

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