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?
Try this:
This is equivalent of you open file explorer with
\\100.xx.24.55\SFTPData\file.txtOf 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.txtThird option would be to use an SMB Java library, eg. SMBJ, to connect to a shared folder and read the file.