【问题标题】:Java JCIFS how right to copy file from Samba to Windows local?Java JCIFS 如何将文件从 Samba 复制到 Windows 本地?
【发布时间】:2016-07-21 13:47:06
【问题描述】:

我正在尝试创建一个 Java 应用程序,它可以将文件从 Unix Samba 共享复制到 Windows 文件夹。为了实现这一点,我正在使用 JCIFS 库。

我有以下代码:

SmbFile smbFromFile = new SmbFile("smb:////192.168.10.1//data", auth);
smbFromFile.copyTo(destinationFolder);

我将其修改为:

SmbFile smbFromFile = new SmbFile("smb:////192.168.10.1//data", auth);
SmbFile destinationFolder = new SmbFile("C:\\Temp\\IN\\");
smbFromFile.copyTo(destinationFolder);

但它给了我以下错误:

Exception in thread "main" jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password.
    at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:546)
    at jcifs.smb.SmbTransport.send(SmbTransport.java:663)
    at jcifs.smb.SmbSession.sessionSetup(SmbSession.java:390)
    at jcifs.smb.SmbSession.send(SmbSession.java:218)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176)
    at jcifs.smb.SmbFile.doConnect(SmbFile.java:911)
    at jcifs.smb.SmbFile.connect(SmbFile.java:957)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:880)
    at jcifs.smb.SmbFile.copyTo(SmbFile.java:2303)
    at RU.Tasks.Task3_Load_MedioSCP_Tekelek_file_To_DB_Oracle_BMCDB.main(Task3.java:203)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

如果我尝试在 Samba 共享上创建一个新文件,它会按预期工作:

String user = "usersamba";
String pass ="1234";
String hostname = "192.168.10.1";
String sharedFolder = "data/new";
String path = "smb://"+hostname+"/"+sharedFolder+"/test.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
SmbFile smbFile = new SmbFile(path,auth);
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
smbfos.write("testing....and writing to a file".getBytes());
System.out.println("completed ...nice !");

请帮助解决这个问题。

【问题讨论】:

    标签: java jsp filesystems samba jcifs


    【解决方案1】:

    选项解决问题

    InputStream in = null; 输出流输出 = null; 尝试{ 字符串 SambaURL="smb://usersamba:1234@192.168.1.110/data/1b.csv"; 文件destinationFolder = new File("C:\\Temp\\IN\\"); SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS_"); 文件子 = 新文件 (destinationFolder+ "/" + fmt.format(new Date()) +"1b.csv"); SmbFile 目录 = 新 SmbFile(SambaURL); SmbFile fileToGet=new SmbFile(SambaURL); fileToGet.connect(); in = new BufferedInputStream(new SmbFileInputStream(fileToGet)); out = new BufferedOutputStream(new FileOutputStream(child)); 字节[]缓冲区=新字节[4096]; 国际长度 = 0; //读取长度 while ((len = in.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); //刷新缓冲区输出流 } 捕获(异常 e){ String msg = "发生错误:" + e.getLocalizedMessage(); System.out.println(msg); } 最后 { 尝试 { 如果(出!= null){ out.close(); } 如果(在!= null){ 附寄(); } } 捕获(异常 e){} }

    source here

    【讨论】:

      【解决方案2】:

      您需要适当的身份验证机制。例如:

      private static void GetFiles() throws IOException {
          jcifs.Config.registerSmbURLHandler();
      
          NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
                  prop.getProperty("smbDomain"), prop.getProperty("smbUser"),
                  prop.getProperty("smbPass"));
      
      
          StaticUserAuthenticator authS = new StaticUserAuthenticator(
                  prop.getProperty("smbDomain"), prop.getProperty("smbUser"),
                  prop.getProperty("smbPass"));
      
          FileSystemOptions opts = new FileSystemOptions();
      
          DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts,
                  authS);
      
          SmbFile smbFile = new SmbFile(prop.getProperty("smbURL"),auth);
          FileSystemManager fs = VFS.getManager();
          String[] files = smbFile.list();
      
          for(String file:files) {
              SmbFile remFile = new SmbFile(prop.getProperty("smbURL") + file, auth);
              SmbFileInputStream smbfos = new SmbFileInputStream(remFile);
              OutputStream out = new FileOutputStream(file);
              byte[] b = new byte[8192];  
              int n;  
              while ((n = smbfos.read(b)) > 0) {  
                  out.write(b, 0, n);  
              }  
              smbfos.close();
              out.close(); 
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2019-08-23
        • 2014-05-21
        • 2012-11-01
        • 2023-03-29
        • 2013-01-22
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        相关资源
        最近更新 更多