如何使用Java中的jcifs将文件从smb共享复制到本地驱动器?

May*_*y12 6 java smb jcifs file-copying

有人可以帮我将文件从共享文件夹复制到本地驱动器吗?我的代码是:

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;;


public class smb {

      /**
      * @param args
      * @throws IOException
       */
      public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub


          String urlToBackUpFile = "smb://ip/backup$/test.txt"; 
          System.out.println("smb folder of source file" + urlToBackUpFile);
          NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "login", "pass");


            SmbFile dir = new SmbFile(urlToBackUpFile, auth);
            System.out.println(dir.getDate());
            SmbFile dest = new SmbFile ("C:/SQLRESTORESTAGE/v2.bak");
            dir.copyTo(dest);
      }
}
Run Code Online (Sandbox Code Playgroud)

不复制文件文件.我收到一条消息"无法连接到服务器",但程序显示源文件的dir.getDate()(以及文件名和长度).所以我认为目标文件夹的问题(C:/ SQLRESTORESTAGE /).另外,我只能阅读源文件.你能帮我修改代码或建议吗?谢谢.

小智 8

也许将auth添加到第二个文件:

SmbFile dest = new SmbFile ("C:/SQLRESTORESTAGE/v2.bak",**auth**);
Run Code Online (Sandbox Code Playgroud)

使用SmbFile dest = new SmbFile ("C:/SQLRESTORESTAGE",auth).canWrite 你知道你是否对父目录有写权限


Man*_*ney 7

经过多次试验和失败后,唯一可靠的方法就是老去学校并使用FileInputStream和FileOutputStream,如下所示:

   `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;
        }
    }`
Run Code Online (Sandbox Code Playgroud)

  • 只需要注意一点,最好在finally块中关闭流. (3认同)