File transfer using SFTP from another server

617 Views Asked by At

I can able to upload local system file to SFTP client using below code,

        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).

How do we achieve this?

1

There are 1 best solutions below

0
Jokkeri On

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

Of course this requires that your Windows login account has sufficient rights to read the file from shared folder.

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. X:\\SFTPData\\file.txt

Third option would be to use an SMB Java library, eg. SMBJ, to connect to a shared folder and read the file.