我尝试了以下代码:
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)
请注意:
sftpchannel.put()将文件从远程路径直接复制到linux服务器路径,但它会引发异常。String url = "//remotehost/SharedPath/Comp/NG/";请注意:我正在使用jsch库连接到Linux服务器,并且能够使用sftpChannel.connect()成功地连接到Linux服务器;还能够使用sftpChannel.put(localpath,linuxpath)将文件从本地机器放到Linux服务器;以及连接到我正在使用smbFile的windows服务器。我能够连接,但不能将文件从windows复制到Linux服务器路径。我尝试使用sftpChannel.put(文件名,remoteDirectory);相同,但结果是异常。在这个特定的步骤中,我假设由于与windows服务器的连接是成功的,所以我也可以复制文件。我能读到文件,但不能复制。不知道为什么会发生这种事。
有人能给我提供适当的步骤吗?
发布于 2017-06-29 06:26:24
我想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);
}
}
}https://stackoverflow.com/questions/44799583
复制相似问题