How to copy file from smb share to local drive using jcifs in Java?

前端 未结 5 1716
后悔当初
后悔当初 2020-12-06 19:07

Could anybody help me to copy file from shared folder to local drive? My code is:

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
impo         


        
5条回答
  •  孤城傲影
    2020-12-06 19:56

    After many trials and failures the only method that worked for me reliably was to go old school and use FileInputStream and FileOutputStream like so:

       `SmbFile[] files = getSMBListOfFiles(sb, logger, domain, userName, password, sourcePath, sourcePattern);
    
        if (files == null)
            return false;
        output(sb, logger, "      Source file count: " + files.length);
        String destFilename;
        FileOutputStream fileOutputStream;
        InputStream fileInputStream;
        byte[] buf;
        int len;
        for (SmbFile smbFile: files) {
            destFilename = destinationPath + smbFile.getName();
            output(sb, logger, "         copying " + smbFile.getName());
            try {
                fileOutputStream = new FileOutputStream(destFilename);
                fileInputStream = smbFile.getInputStream();
                buf = new byte[16 * 1024 * 1024];
                while ((len = fileInputStream.read(buf)) > 0) {
                    fileOutputStream.write(buf, 0, len);
                }
                fileInputStream.close();
                fileOutputStream.close();
            } catch (SmbException e) {
                OutputHandler.output(sb, logger, "Exception during copyNetworkFilesToLocal stream to output, SMP issue: " + e.getMessage(), e);
                e.printStackTrace();
                return false;
            } catch (FileNotFoundException e) {
                OutputHandler.output(sb, logger, "Exception during copyNetworkFilesToLocal stream to output, file not found: " + e.getMessage(), e);
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                OutputHandler.output(sb, logger, "Exception during copyNetworkFilesToLocal stream to output, IO problem: " + e.getMessage(), e);
                e.printStackTrace();
                return false;
            }
        }`
    

提交回复
热议问题