title: file uploading successfully, but 0kb file is uploading on remote server using jsch sftp java
body: <p>I am uploading a file from my local computer to remote server in a simple java application using Jsch, and Sftp protocol. </p>
  
  <p>My code is getting no error or no exception and it runs successfully, but when I look at the remote location, the file is uploaded as 0kb with no extension and named as 'D'. </p>
  
  <p>I have tried many ways but I am not able to figure out the mistake. </p>
  
  <p>Here is my code..</p>
  
  <pre><code>        String remoteDirectory = "D:\\Datastores\\RootStore\\Test\\";
          String localDirectory = "C:\\pdffiles\\";
  
  
          String fileToFTP = "demo.pdf";
          String SFTPHOST = "hostIPaddress";
          int SFTPPORT = 22;
          String SFTPUSER = "username";
          String SFTPPASS = "password";
  
  
          String local_filename = fileToFTP;
          String local_pathname = localDirectory;
          String remote_filename = fileToFTP;
  
  
          JSch jsch = null;
          Session session = null;
          ChannelSftp sftpChannel = null;
  
  
          try
          {
            jsch = new JSch();
            session = jsch.getSession(SFTPUSER, SFTPHOST);
            session.setPassword(SFTPPASS);
            session.setPort(SFTPPORT);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setConfig("cipher.s2c", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes256-ctr");
            session.setConfig("cipher.c2s", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes256-ctr");
            session.connect();
  
            sftpChannel = (ChannelSftp)session.openChannel("sftp");
            sftpChannel.connect();
  
            System.out.println(sftpChannel);
            System.out.println(sftpChannel.getSession().isConnected());
  
            FileInputStream fileBodyIns = new FileInputStream(local_pathname + local_filename);
  
            System.out.println(local_pathname + local_filename);
            System.out.println(remoteDirectory + remote_filename);
  
            sftpChannel.put(fileBodyIns, remoteDirectory + remote_filename);
            fileBodyIns.close();
  
            sftpChannel.exit();
            session.disconnect();
  
            System.out.println("File uploaded successfully");
            return "File uploaded successfully";
          }
          catch (Exception e)
          {
              e.printStackTrace();
              return e.getMessage();
          }
  </code></pre>
  
  <p>My connection makes successfully, the line</p>
  
  <pre><code>System.out.println(sftpChannel.getSession().isConnected());
  </code></pre>
  
  <p>gives <strong>true</strong></p>
  
  <p>and the following line prints successfully</p>
  
  <pre><code>System.out.println("File uploaded successfully");
  </code></pre>