简体   繁体   English

使用 SFTP 从另一台服务器传输文件

[英]File transfer using SFTP from another server

I can able to upload local system file to SFTP client using below code,我可以使用以下代码将本地系统文件上传到 SFTP 客户端,

        try {
            final SSHClient ssh = new SSHClient();
            ssh.addHostKeyVerifier(new PromiscuousVerifier());
            ssh.connect("100.XX.XX.XX");
            ssh.authPassword("username", "password");
//        ssh.authPublickey(null, arg);
            final String src = "C:\\LocalFolder\\SFTPData\\file.txt";
            File localFile = new File(src);
            System.out.println(localFile.getName());
            SFTPClient sftp = ssh.newSFTPClient();

            sftp.put(localFile.getAbsolutePath(), "/home/user/test");
        } catch (Exception e) {
        }

But the problem is, I need to transfer file from windows network shared path which requires username, password(\\100.xx.24.55\\SFTPData\\file.txt).但问题是,我需要从需要用户名、密码(\\100.xx.24.55\\SFTPData\\file.txt)的windows网络共享路径传输文件。

How do we achieve this?我们如何实现这一目标?

Try this:尝试这个:

final String src = "\\\\100.xx.24.55\\SFTPData\\file.txt";

This is equivalent of you open file explorer with \\\\100.xx.24.55\\SFTPData\\file.txt这相当于您使用\\\\100.xx.24.55\\SFTPData\\file.txt打开文件资源管理器

Of course this requires that your Windows login account has sufficient rights to read the file from shared folder.当然,这需要您的 Windows 登录帐户具有从共享文件夹读取文件的足够权限。

If you need to give different credentials than your Windows login credentials, you may give it a try to "map network drive using different credentials" and then refer with selected drive letter in your code, eg.如果您需要提供与 Windows 登录凭据不同的凭据,您可以尝试“使用不同的凭据映射网络驱动器”,然后在代码中使用选定的驱动器号进行引用,例如。 X:\\\\SFTPData\\\\file.txt

Third option would be to use an SMB Java library, eg.第三个选择是使用 SMB Java 库,例如。 SMBJ, to connect to a shared folder and read the file. SMBJ,连接到共享文件夹并读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM