Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SmbFile to Java IO File

Tags:

java

smb

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.

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.

  1. Is there any way to convert this SmbFile into a Java compatible File?
like image 431
arash moeen Avatar asked Mar 31 '16 17:03

arash moeen


3 Answers

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)
like image 139
Adam Michalik Avatar answered Nov 06 '22 22:11

Adam Michalik


    jcifs.smb.SmbFile smbFile = new SmbFile("smb://host/fileShare/.../file");
    java.io.File javaFile = new File(smbFile.getUncPath());

    System.out.println(smbFile);
    System.out.println(javaFile);

Output

smb://host/fileShare/.../file
\\host\fileShare\...\file

javadoc of smbFile.getUncPath() says

Retuns the Windows UNC style path with backslashs intead of forward slashes.

I am using jcifs-1.3.17.jar on Windows 10.

like image 32
rsinha Avatar answered Nov 06 '22 22:11

rsinha


Using Apache Commons IO library

https://mvnrepository.com/artifact/commons-io/commons-io

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", "user", "key");

SmbFile smbFile = new SmbFile("smb://IP/pitoka.tmp", auth)

InputStream initialStream = smbFile.getInputStream();

File targetFile = new File("/tmp/pitoka.tmp");

FileUtils.copyInputStreamToFile(initialStream, targetFile);

I hope help you.

like image 1
Marcelo Rebouças Avatar answered Nov 06 '22 22:11

Marcelo Rebouças