Код: Выделить всё
public static void main(String[] args) throws IOException {
// Local file path and remote directory
String localFile = "C:\\my\\local\\dir\\myFile%2Eto%2Fsftp%2Euploaded.zip";
String remoteDir = "/sftp/remote/dir/";
// Call method to upload file
uploadFileToSftp(localFile, remoteDir);
}
private static SSHClient setupSshj() throws IOException {
// SSH connection details
String remoteHost = "sftp.host.com";
String username = "myUsername";
String password = "myDftppassword";
SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
client.connect(remoteHost);
client.useCompression();
client.authPassword(username, password);
return client;
}
public static void uploadFileToSftp(String localFile, String remoteDir) throws IOException {
// Set up SSH client
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
// Debugging: print the remote file path you're uploading to
// Upload the file with special characters in the name, without URL encoding
sftpClient.put(localFile, remoteDir);
// Close the connection
sftpClient.close();
sshClient.disconnect();
}
Код: Выделить всё
2025-01-06 16:22:30 [main] INFO n.s.sshj.transport.random.JCERandom - - - - Creating new SecureRandom.
2025-01-06 16:22:30 [main] INFO n.s.sshj.transport.TransportImpl - - - - Client identity string: SSH-2.0-SSHJ_0.38.0
2025-01-06 16:22:30 [main] INFO n.s.sshj.transport.TransportImpl - - - - Server identity string: SSH-2.0-CrushFTPSSHD
2025-01-06 16:22:32 [main] INFO n.s.s.c.c.direct.SessionChannel - - - - Will request `sftp` subsystem
Exception in thread "main" java.io.IOException: Trying to upload file myFile%2Eto%2Fsftp%2Euploaded.zip to path /sftp/remote/dir/myFile%2Eto%2Fsftp%2Euploaded.zip but that is a directory
at net.schmizz.sshj.sftp.SFTPFileTransfer$Uploader.prepareFile(SFTPFileTransfer.java:349)
at net.schmizz.sshj.sftp.SFTPFileTransfer$Uploader.uploadFile(SFTPFileTransfer.java:256)
at net.schmizz.sshj.sftp.SFTPFileTransfer$Uploader.upload(SFTPFileTransfer.java:209)
at net.schmizz.sshj.sftp.SFTPFileTransfer$Uploader.access$100(SFTPFileTransfer.java:192)
at net.schmizz.sshj.sftp.SFTPFileTransfer.upload(SFTPFileTransfer.java:81)
at net.schmizz.sshj.sftp.SFTPFileTransfer.upload(SFTPFileTransfer.java:59)
at net.schmizz.sshj.sftp.SFTPFileTransfer.upload(SFTPFileTransfer.java:53)
at net.schmizz.sshj.sftp.SFTPClient.put(SFTPClient.java:251)
Мое имя файла выглядит следующим образом: myFile%2Eto%2Fsftp%2Euploaded.zip
Но SFTP-сервер автоматически принимает значение myFile.to/sftp.uploaded
Мой ожидается, что файл будет загружен в виде myFile%2Eto%2Fsftp%2Euploaded.zip
Заранее спасибо.
Подробнее здесь: https://stackoverflow.com/questions/793 ... l-decoding
Мобильная версия