gpt4 book ai didi

java - 将 SmbFile 转换为 Java IO 文件

转载 作者:搜寻专家 更新时间:2023-10-31 20:00:42 29 4
gpt4 key购买 nike

我的 Java 应用程序需要访问保存在远程共享文件夹中的大型 Excel 文件(大小超过 1GB)。我正在使用 SmbFile 获取带有身份验证的文件。

注意:下载文件主要是出于大小原因而不是一种选择。

问题是我需要 excel 文件是 Java IO 文件而不是 SmbFile,因为 other libraries我用来解析 excel 的那个只接受 Java IO 文件。

  1. 有什么方法可以将此 SmbFile 转换为 Java 兼容文件吗?

最佳答案

参见 implementation details您的图书馆:

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.

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.

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

所以无论您使用 File 还是 InputStream API 都没有关系 - 无论如何都需要下载整个文件。

最简单的解决方案是将 SmbFile.getInputStream() 传递给

StreamingReader.builder().read(smbFile.getInputStream())

但您也可以先下载文件,例如。通过 IOUtils.copy()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());
}

并将文件传递给

StreamingReader.builder().read(file)

关于java - 将 SmbFile 转换为 Java IO 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36339504/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com