如何使用Java中的jcifs将文件从smb共享复制到本地驱动器? [英] How to copy file from smb share to local drive using jcifs in Java?

查看:485
本文介绍了如何使用Java中的jcifs将文件从smb共享复制到本地驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

File file is not copied. I received a message "Failed to connect to server", but the programm shows dir.getDate() (and file name, and lenght) of source file. So I think the problem with destination folder (C:/SQLRESTORESTAGE/). Also I have proviledges only for reading source file. Could you help me to cirrect the code or advise something? Thank you.

推荐答案

可能会在第二个文件中添加auth:

maybe adding auth to the second file:

SmbFile dest = new SmbFile ("C:/SQLRESTORESTAGE/v2.bak",**auth**);

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

using SmbFile dest = new SmbFile ("C:/SQLRESTORESTAGE",auth).canWrite you know if you have write permissions on the parent directory

这篇关于如何使用Java中的jcifs将文件从smb共享复制到本地驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆