将SmbFile转换为Java IO文件 [英] Convert SmbFile to Java IO File
问题描述
我的Java应用程序需要访问保存在远程共享文件夹中的大型excel文件(大小为1GB +)。我正在使用 SmbFile 来获取带有身份验证的文件。
My Java application requires access to a large excel file (1GB+ in size) saved on remote shared folder. I'm using SmbFile to get the file with authentication.
注意:下载文件不是主要考虑尺寸原因。
Note: Downloading of the file is not an option mainly for size reasons.
问题是我需要excel文件作为Java IO文件而不是SmbFile,因为其他我用来解析excel的库只接受Java IO文件。
The problem is that I need the excel file to be a Java IO File and not SmbFile since the other libraries that I'm using to parse the excel only accepts Java IO File.
- 有没有办法转换这个SmbFile到兼容Java的文件?
推荐答案
参见实施细节:
该库将采用提供的InputStream并将其输出到文件系统。 (...)创建文件后,它将从文件系统流入内存。
This library will take a provided InputStream and output it to the file system. (...) Once the file is created, it is then streamed into memory from the file system.
需要以这种方式输出流的原因必须如何处理ZIP文件的工作原理。由于XLSX文件格式基本上是一个ZIP文件,因此无法在不读取整个InputStream的情况下找到所有条目。
The reason for needing the stream being outputted in this manner has to do with how ZIP files work. Because the XLSX file format is basically a ZIP file, it's not possible to find all of the entries without reading the entire InputStream.
(...)此库的工作原理将流读出到临时文件中。作为自动关闭操作的一部分,临时文件将被删除。
(...) This library works by reading out the stream into a temporary file. As part of the auto-close action, the temporary file is deleted.
如果您需要更多地控制文件的创建/处理方式,可以选择使用 java.io.File
初始化库。此文件不会被写入或删除
If you need more control over how the file is created/disposed of, there is an option to initialize the library with a java.io.File
. This file will not be written to or removed
因此,如果您使用文件<无关紧要/ code>或
InputStream
API - 无论如何都需要下载整个文件。
So it doesn't matter if you use the File
or InputStream
API - the whole file will need to be downloaded anyhow.
最简单解决方案是将 SmbFile.getInputStream()
传递给
The simplest solution is to pass the SmbFile.getInputStream()
to
StreamingReader.builder().read(smbFile.getInputStream())
但也可以先下载文件,例如。通过 IOUtils.copy()
或 Files.copy()
but alternatively you can first download the file eg. by means of IOUtils.copy()
or Files.copy()
File file = new File("...");
try (
in = smbFile.getInputStream();
out = new FileOutputStream(file)
) {
IOUtils.copy(in, out);
}
或
try (in = smbFile.getInputStream()) {
Files.copy(smbFile.getInputStream(), file.toPath());
}
并传递文件
to
StreamingReader.builder().read(file)
这篇关于将SmbFile转换为Java IO文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!