title: connect to SFTP using java
body: <p>I need help with connection to SFTP server? Does anybody have working code?</p>
  
  <p>I found something like this</p>
  
  <pre><code>package test.JSch;
  
  import com.jcraft.jsch.*;
  
  public class TestJSch {
  public static void main(String args[]) {
      JSch jsch = new JSch();
      Session session = null;
      try {
          session = jsch.getSession("USSERNAME", "HOST", 22);
          //session.setConfig("StrictHostKeyChecking", "no");
          session.setPassword("PASSWORD");
          System.out.println("1");
          session.connect();
          System.out.println("2");
  
          Channel channel = session.openChannel("sftp");
          System.out.println("3");
          channel.connect();
          System.out.println("4");
          ChannelSftp sftpChannel = (ChannelSftp) channel;
          sftpChannel.get("remotefile.txt", "localfile.txt");
          sftpChannel.exit();
          session.disconnect();
      } catch (JSchException e) {
          e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
      } catch (SftpException e) {
          e.printStackTrace();
      }
      }
  }
  </code></pre>
  
  <p>but this was the output</p>
  
  <pre><code>com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
      at com.jcraft.jsch.Util.createSocket(Util.java:258)
      at com.jcraft.jsch.Session.connect(Session.java:186)
      at com.jcraft.jsch.Session.connect(Session.java:145)
      at test.JSch.TestJSch.main(TestJSch.java:17)
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(Unknown Source)
      at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.&lt;init&gt;(Unknown Source)
      at java.net.Socket.&lt;init&gt;(Unknown Source)
      at com.jcraft.jsch.Util.createSocket(Util.java:252)
      ... 3 more
  </code></pre>
  
  <p>what is wrong with this code?</p>