Convert SmbFile to Java IO File

后端 未结 5 1306
隐瞒了意图╮
隐瞒了意图╮ 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:41

    Recently i had a similar situation, however, I hadn't found a good solution in the internet, but I wrote a basic code that did what I need easily.

    In your case, you will need to copy the excel file from the source (Remote Directory) using SmbFile with authentication to the destination (Local Directory) and only after, convert the excel file path of the destination (getCanonicalPath() function) and convert it from SmbFile format to File format with the code below. After, create your File object with the file destination path and do what you want.

    I use JCIFS to work with remote shared directories using the SMBFILE class.

    First, you need to import the main libraries:

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

    Second, you need to create a static method to convert from SmbFile format to File format:

    /**
     * This method convert a directory path from SmbFile format to File format.
    *

    Sintax:
        convertSmbFileToFile("Canonical Path")

    *

    Example:
        convertSmbFileToFile("smb://localhost/D$/DOCUMENTOS/workspace/tests2/access")

    * @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, you could also create a static method to convert from File format to SmbFile format:

    /**
     * This method convert a directory path from File format to SmbFile format.
    *

    Sintax:
        convertFileToSmbFile("Canonical Path")

    *

    Example:
        convertFileToSmbFile("D:\DOCUMENTOS\workspace\tests2\access")

    * @param fileCanonicalPath String * @see String */ public static String convertFileToSmbFile(String fileCanonicalPath) { return "smb://localhost/" + fileCanonicalPath.toString().replace(":", "$").replace("\\", "/"); }

    Finally, you can call the methods like the below example:

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

    Result of previous code run:

    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
    

    Extra Information: getCanonicalPath()

    Maybe this code will help you and I am available to talk about if you want.

    Good Luck!

提交回复
热议问题