如何copy/transfer一个文件从windows服务器路径到linux服务器路径?
How to copy/transfer a file from windows server path to linux server path?
我试过下面的代码:
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 (Logon failure: unknown user name or bad password)
请注意:
我可以使用上述代码读取远程服务器中存在的文件,但无法将文件从远程服务器路径复制或传输到 Linux 服务器路径。
由于与远程服务器和linux服务器的连接成功,我尝试使用sftpchannel.put()
直接将文件从远程路径复制到linux服务器路径但它抛出异常。
使用smb 连接后,是否无法使用下面的url 直接连接到共享路径? String url = "//remotehost/SharedPath/Comp/NG/";
请注意:我正在使用 jsch 库连接到 Linux 服务器,并且我能够使用 sftpChannel.connect() 成功连接到 Linux 服务器;并且还能够使用 sftpChannel.put(localpath, linuxpath); 将文件从我的本地机器放到 Linux 服务器上;并且要连接到 windows 服务器,我正在使用 smbFile。我能够连接但无法将文件从 windows 复制到 Linux 服务器路径。我尝试使用 sftpChannel.put(filename, remoteDirectory);同样,但它导致异常。在此特定步骤中,我假设当与 windows 服务器的连接成功时,我也将能够复制文件。我能够读取文件但不能复制。不确定为什么会这样。
任何人都可以为我提供正确的步骤吗?
我猜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);
}
}
}
我试过下面的代码:
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 (Logon failure: unknown user name or bad password)
请注意:
我可以使用上述代码读取远程服务器中存在的文件,但无法将文件从远程服务器路径复制或传输到 Linux 服务器路径。
由于与远程服务器和linux服务器的连接成功,我尝试使用
sftpchannel.put()
直接将文件从远程路径复制到linux服务器路径但它抛出异常。使用smb 连接后,是否无法使用下面的url 直接连接到共享路径?
String url = "//remotehost/SharedPath/Comp/NG/";
请注意:我正在使用 jsch 库连接到 Linux 服务器,并且我能够使用 sftpChannel.connect() 成功连接到 Linux 服务器;并且还能够使用 sftpChannel.put(localpath, linuxpath); 将文件从我的本地机器放到 Linux 服务器上;并且要连接到 windows 服务器,我正在使用 smbFile。我能够连接但无法将文件从 windows 复制到 Linux 服务器路径。我尝试使用 sftpChannel.put(filename, remoteDirectory);同样,但它导致异常。在此特定步骤中,我假设当与 windows 服务器的连接成功时,我也将能够复制文件。我能够读取文件但不能复制。不确定为什么会这样。
任何人都可以为我提供正确的步骤吗?
我猜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);
}
}
}