Convert SmoFile to Java IO File

My Java application requires access to a large excel file (1 GB + size) stored in a remote shared folder. I use SmbFile to get the authentication file.

Note. Downloading a file is not an option, mainly for size reasons.

The problem is that I need an excel file for the Java IO File, not an SmbFile, since the other libraries that I use for parse excel only accept Java IO File.

  • Is there a way to convert this SmbFile to a Java compatible file?
+5
source share
4 answers

:

InputStream . (...) , .

, , , ZIP . XLSX ZIP , InputStream.

(...) , . .

, /, java.io.File.

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

file

StreamingReader.builder().read(file)
+6

, , , , .

excel (Remote Directory) SmbFile (Local Directory) , excel (getCanonicalPath() ) SmbFile . File , .

JCIFS SMBFILE.

:

import java.io.File;
import java.io.IOException;
import jcifs.smb.SmbFile;

-, SmbFile :

/**
 * This method convert a directory path from SmbFile format to File format.<br />
 * <p><strong>Sintax:</strong> <br />&nbsp;&nbsp;&nbsp;&nbsp;convertSmbFileToFile("Canonical Path")</p>
 * <p><strong>Example:</strong> <br />&nbsp;&nbsp;&nbsp;&nbsp;convertSmbFileToFile("smb://localhost/D$/DOCUMENTOS/workspace/tests2/access")</p>
 * @param smbFileCanonicalPath String
 * @see String
*/
public static String convertSmbFileToFile(String smbFileCanonicalPath) {
    String[] tempVar = smbFileCanonicalPath.substring(6).replace("$", ":").split("/"); 
    String bar = "\\";
    String finalDirectory = "";
    for (int i = 1; i < tempVar.length; i++) {
        finalDirectory += tempVar[i] + bar;
        if (i == tempVar.length - 1) {
            finalDirectory = finalDirectory.substring(0,finalDirectory.length()-1);
        }
    }
    return finalDirectory;
}

Opcional, SmbFile:

/**
 * This method convert a directory path from File format to SmbFile format.<br />
 * <p><strong>Sintax:</strong> <br />&nbsp;&nbsp;&nbsp;&nbsp;convertFileToSmbFile("Canonical Path")</p>
 * <p><strong>Example:</strong> <br />&nbsp;&nbsp;&nbsp;&nbsp;convertFileToSmbFile("D:\DOCUMENTOS\workspace\tests2\access")</p>
 * @param fileCanonicalPath String
 * @see String
*/
public static String convertFileToSmbFile(String fileCanonicalPath) {
    return "smb://localhost/" + fileCanonicalPath.toString().replace(":", "$").replace("\\", "/");
}

, , :

String dirDest = "access/";

    try {

        File localDirFile = new File(dirDest);

        SmbFile localSmbDirFile = new SmbFile(convertFileToSmbFile(localDirFile.getCanonicalPath()));
        File localDirFile2 = new File(convertSmbFileToFile(localSmbDirFile.getCanonicalPath()));

        System.out.println("Original File Format: " + localDirFile.getCanonicalPath());
        System.out.println("Original File Format to SmbFile Format: " + localSmbDirFile.getCanonicalPath());
        System.out.println("Converted SmbFile Format to File Format: " + localDirFile2.getCanonicalPath());

    } catch (IOException e) {

        System.err.println("[ERR] IO Exception - " + e);

    }

:

Original File Format: D:\DOCUMENTOS\workspace\tests2\access
Original File Format to SmbFile Format: smb://localhost/D$/DOCUMENTOS/workspace/tests2/access
Converted SmbFile Format to File Format: D:\DOCUMENTOS\workspace\tests2\access

: getCanonicalPath()

, , , .

!

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

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

Javadoc smbFile.getUncPath()

UNC Windows .

jcifs-1.3.17.jar Windows 10.

0

I suppose this is just a matter of structure: in SmbFile we have two arguments, and in File - only one argument. So, my idea is to declare a file with the same SmbFile path and try to process your file. For example, I want to recursively delete the contents of my folder:

SmbFile sFile = new SmbFile(path, auth)

if (sFile.exists()) {
 File file = new File(path);
 deleteDirectory(file);

}

 boolean deleteDirectory(File directoryToBeDeleted) {
    File[] allContents = directoryToBeDeleted.listFiles();
    if (allContents != null) {
        for (File file : allContents) {
            deleteDirectory(file);
        }
    }
    return directoryToBeDeleted.delete();
}

I hope this code helps you and sorry for my english!

0
source

All Articles