0

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?

Alexandr Arhipov
  • 836
  • 9
  • 19
MMMMS
  • 2,179
  • 9
  • 43
  • 83
  • Are you able to put/send content as a stream? In that case you can read / write the content through a byte buffer and stream it through – gusto2 Oct 04 '21 at 11:32

1 Answers1

0

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.

Jokkeri
  • 1,001
  • 1
  • 13
  • 35