Search code examples
javafile-uploadsftpquarkusjsch

JSCH: Success uploading file but not in the right extension


So I uploaded my file to SFTP directory successfully. But here's the thing. The file uploaded is in "File format" and its name is the same as its directory.

enter image description here

But the actual file is in .jpg file. I also tried it on a .txt file but it's the same.

enter image description here

Although, when I manually add ".jpg" or ".txt" on SFTPRoot file, it reads my file and shows the cat picture.

Here's my code:

private final SFTPConfig sftpConfig;
private final String KNOWN_HOST = "/Users/xxx/.ssh/known_hosts";
private final String LOCAL_PATH = "C:/Users/xxx/Downloads/";

public void setupJsch(int configType, String fileName) throws JSchException, SftpException {
    log.info("+++++ CONNECTING TO SFTP +++++");
    JSch jsch = new JSch();
    jsch.setKnownHosts(KNOWN_HOST);
    Session jschSession = jsch.getSession(sftpConfig.username(), sftpConfig.remoteHost(), 22);
    jschSession.setPassword(sftpConfig.password());
    jschSession.setConfig("StrictHostKeyChecking", "no");
    jschSession.connect();

    Channel channel = jschSession.openChannel("sftp");
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    sftpChannel.connect(60000);
    log.info("+++++ ESTABLISHED CONNECTION +++++");

    try {
        if (configType == 1) {
            log.info("+++++ BEGIN SENDING {} TO SFTP", fileName);
            sendFile(sftpChannel, fileName);
        } else {
            log.info("+++++ BEGIN DOWNLOADING {} FROM SFTP", fileName);
            downloadFile(sftpChannel, fileName);
        }
    } catch (SftpException | FileNotFoundException e) {
        log.error("Error downloading/sending the file: {}", e.getMessage());
    }

    sftpChannel.disconnect();
    jschSession.disconnect();
    log.info("+++++ DISCONNECTED TO SFTP +++++");
}

public void sendFile(ChannelSftp sftpChannel, String fileName) throws SftpException, FileNotFoundException {
    sftpChannel.put(LOCAL_PATH + fileName, sftpConfig.sftpPath());
}

public void downloadFile(ChannelSftp sftpChannel, String fileName) throws SftpException, FileNotFoundException {
    sftpChannel.get(sftpConfig.sftpPath()+"/"+filename,LOCAL_PATH);
}
}

Also, here's my sftp config:

#---- SFTP CONFIGURATIONS
sftp.remote-host=localhost
sftp.username=root
sftp.password=root
sftp.path=/SFTPRoot

I also try the File and FileInputStream but it's giving me No Such File That's why I'm sticking to earlier approach. Below is my code using input stream.

//File file = new File(LOCAL_PATH + fileName);
//log.warn("File {}: Exist? {} : {}, Path = {} ", file.getName(), file.exists(), file.isFile(), file.getPath());
//sftpChannel.put(new FileInputStream(file), sftpConfig.sftpPath());

Did I forget something? Or do something wrong?


Solution

  • Lol, I just found out the answer by my own.

    Simply put the file name in dst. Sample:

    File file = new File(LOCAL_PATH + fileName);
    sftpChannel.put(new FileInputStream(file), file.getName());
    

    or simply

    sftpChannel.put(LOCAL_PATH + fileName, <whatever you want to name for your file + extension>);
    

    I just realize that JSCH automatically calls the SFTP server directory. So in my case. My default SFTP local server is "/SFTPRoot". So no need to call the SFTPRoot directory and just simply add the filename (or change it to something new. Example: cat.jpg to cutecat.jpg)

    Then If I create a folder inside of the server then put a file inside that folder, the code will be like this:

    sftpChannel.put(LOCAL_PATH + fileName, sftpConfig.homePath() + "/cutecat.jpg");
    

    Hope it helps. my home folder config is like this:

    sftp.home-path=/Home