Convert SmbFile to Java IO File

后端 未结 5 1267
隐瞒了意图╮
隐瞒了意图╮ 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.<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, 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.<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("\\", "/");
    }
    

    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!

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-02 16:51

    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.

    0 讨论(0)
  • 2021-01-02 16:53
        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.

    0 讨论(0)
  • 2021-01-02 17:02

    It's just a matter of structure I guess, with SmbFile we have two arguments while with File we have just one argument. So, my Idea is to declare a File with the same path of the SmbFile and try to handle your file. For example, in my I want to delete recursively the content 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 peace of code help you, and sorry for my english !

    0 讨论(0)
提交回复
热议问题