Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将文件从windows服务器路径复制/传输到linux服务器路径?_Java_Linux_Smb - Fatal编程技术网

Java 如何将文件从windows服务器路径复制/传输到linux服务器路径?

Java 如何将文件从windows服务器路径复制/传输到linux服务器路径?,java,linux,smb,Java,Linux,Smb,我尝试了以下代码: String url = "smb://remotehost/SharedPath/Comp/NG/"; NtlmPasswordAuthentication auth2 = new NtlmPasswordAuthentication(null,"user", "password"); SmbFile dir = new SmbFile(url, auth); for (SmbFile f : dir.listFiles()) { if(f.getName().con

我尝试了以下代码:

String url = "smb://remotehost/SharedPath/Comp/NG/";
NtlmPasswordAuthentication auth2 = new 
NtlmPasswordAuthentication(null,"user", "password");
SmbFile dir = new SmbFile(url, auth);
for (SmbFile f : dir.listFiles())
{
  if(f.getName().contains("Test")) //successfully reads the file
  {
    System.out.println("test...."+f);
    filename= f.getUncPath();
    System.out.println("filename...."+filename);
    sftpChannel.put(filename, remoteDirectory); // throws exception
  }  
}
上述代码导致以下异常:
java.io.FileNotFoundException:\\remotehost\SharedPath\comp\NG\Test.txt(登录失败:未知用户名或错误密码)

请注意:

  • 我能够使用上述代码读取远程服务器中的文件,但无法将文件从远程服务器路径复制或传输到Linux服务器路径

  • 由于与远程服务器和linux服务器的连接成功,我尝试使用
    sftpchannel.put()
    将文件从远程路径直接复制到linux服务器路径,但它引发异常

  • 一旦使用smb连接,我们将无法使用下面的url直接连接到共享路径吗<代码>字符串url=“//remotehost/SharedPath/Comp/NG/”

请注意:我正在使用jsch库连接到Linux服务器,并且我能够使用sftpChannel.connect()成功连接到Linux服务器;还可以使用sftpChannel.put(localpath,linuxpath)将文件从本地机器放到Linux服务器上;要连接到windows服务器,我正在使用smbFile。我能够连接,但无法将文件从windows复制到Linux服务器路径。我尝试使用sftpChannel.put(文件名,remoteDirectory);对于相同的,但它导致了异常。在这个特定的步骤中,我假设当连接到windows server成功时,我也能够复制文件。我能读文件,但不能复印。不知道为什么会这样


有人能给我提供正确的步骤吗?

我想
sftpChannel
的类型是
com.jcraft.jsch.ChannelSftp
。然后,以下方法将为您进行复制。当然,您必须将正确初始化的
SmbFile
ChannelSftp
对象作为参数传递

public void copyFromSmbToSftp(SmbFile smbFile, ChannelSftp channelSftp, String destPath) throws IOException, SftpException {
    try(BufferedInputStream inputStream = new BufferedInputStream(smbFile.getInputStream());
        BufferedOutputStream outputStream = new BufferedOutputStream(channelSftp.put(destPath))){
      byte[] buffer = new byte[64*1024];
      int bytesRead;
      while((bytesRead=inputStream.read(buffer, 0, buffer.length))!=-1){
        outputStream.write(buffer, 0, bytesRead);
      }
    }
  }

读取目录时,您使用该
SmbFile
类并为其提供身份验证凭据,因此它可以工作。当试图复制时,您使用的是一个完全不同的类。它应该如何知道身份验证凭据?我不熟悉这些类,但您可能必须通过
SmbFile
类读取文件内容,并将其存储在内存或本地文件中,然后sftp将其从内存阵列或本地文件复制到目标方法,以便其InputStream可用于读取文件。在您的示例中,我看不出该频道的类型variable@Bal对不起,我没有提供完整的代码。我只是提供了导致异常的代码。@Balázsnees我在备注部分添加了更多信息。希望这能让你更好地理解我的目标。