Convert SmbFile to Java IO File

后端 未结 5 1270
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 16:37

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 authenticati

5条回答
  •  礼貌的吻别
    2021-01-02 16:46

    See implementation details of your library:

    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

    So it doesn't matter if you use the File or InputStream API - the whole file will need to be downloaded anyhow.

    The simplest solution is to pass the SmbFile.getInputStream() to

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

    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);
    }
    

    or

    try (in = smbFile.getInputStream()) {
        Files.copy(smbFile.getInputStream(), file.toPath());
    }
    

    and pass file to

    StreamingReader.builder().read(file)
    

提交回复
热议问题