Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Download 0 byte file from SFTP

I am not able to download proper file from SFTP, Download file size is 0 byte.

How can I resolve problrm?

Below is my java code

public void downloadFile(String path, String filename) {
        String SFTPHOST = "XXXX";
        int SFTPPORT = XX;
        String SFTPUSER = "XX";
        String SFTPPASS = "XXXX";
        String SFTPWORKINGDIR = "/root/XXXX/";
        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
            session.setPassword(SFTPPASS);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp) channel;
            int indx = path.lastIndexOf("/");
            String cdpath = path.substring(0, indx);
            channelSftp.cd(SFTPWORKINGDIR + cdpath + "/");
            System.out.println("Path = " + channelSftp.pwd() + " : " + filename);


            OutputStream os = new FileOutputStream("filename,false);

            channelSftp.get(filename, os);

            os.flush();os.close();


            session.disconnect();
            channel.disconnect();
            channelSftp.quit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

How can I download proper file in my local using SFTP ?

Help me to resolve this problem

Comments