admin管理员组

文章数量:1122846

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";
    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();
}

below are the logs :

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)

My Question : I really want to upload the file as it is without URL decoding to my remote SFTP server but server is automatically URL encoding those special characters and throwing the above exception. Before reaching out to my sftp server team, I just wanted to confirm is there is any configurations we can modify from java end (client side) and is it possible to achive my requirement.

My file name looks like this : myFile%2Eto%2Fsftp%2Euploaded.zip But SFTP server automatically taking as myFile.to/sftp.uploaded

My expectation is to uplaod the file as it is myFile%2Eto%2Fsftp%2Euploaded.zip Thanks in advance.

本文标签: javaHow to upload file to SFTP without URL decodingStack Overflow