title: Connection reset with FTP/S java
body: <p>I am trying to connect to my FTP server with FTP/S using simple java program. I am using JDK7u79 and Apache Commons Net 3.3.</p>
  
  <p>I already had a look at similar questions here:
  <a href="https://serverfault.com/questions/85027/how-do-i-setup-an-ftp-server-on-windows-7">https://serverfault.com/questions/85027/how-do-i-setup-an-ftp-server-on-windows-7</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8333797/ftpclient-uploading-file-socketexception-connection-reset">FTPClient Uploading File = SocketException: Connection reset</a></p>
  
  <p>Here is my connect() method:</p>
  
  <pre><code>/**
       * Connects and login to the FTP server
       */
      public void connect() throws IOException {
          int reply;
          FTPSClient ftpsclient = null;
  
          if(getFlavor().equalsIgnoreCase(FLAVOR_FTP)) {
              client = new FTPClient();   
          } else if(getFlavor().equalsIgnoreCase(FLAVOR_FTPS)) {
              client = new FTPSClient();
              //client = ftpsclient;
              ((FTPSClient) client).setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
          }
  
          try {
              client.connect(getHostname(), this.getPortInt());
          } catch (IOException ioe) {
              ioe.printStackTrace();
              String errMsg = "Could not connect to the FTP site. Reason: " + ioe.getMessage();
              throw new IOException(errMsg);
          }
  
          if(getFlavor().equalsIgnoreCase(FLAVOR_FTPS)) {
              ((FTPSClient) client).execPBSZ(0);  // Set protection buffer size
              ((FTPSClient) client).execPROT("P");    // Set data channel protection to private
          }
  
          if (ACTIVE_MODE.equalsIgnoreCase(getConnectionMode())){
              client.enterLocalActiveMode();
          }else if (PASSIVE_MODE.equalsIgnoreCase(getConnectionMode())){
              client.enterLocalPassiveMode();
          }
  
          // After connection attempt, you should check the reply code to verify success.
          reply = client.getReplyCode();
  
          if (!FTPReply.isPositiveCompletion(reply)) {
              error("connect", "FTP server " + getHostname() + " refused connection.");
              throw new IOException("Failed to connect to FTP Server '" + getHostname() + "', Reply code '" + client.getReplyCode()+"'");
          }
  
          // login
          client.login(this.getLoginname(), this.getPassword());
          reply = client.getReplyCode();
          if (!FTPReply.isPositiveCompletion(reply)){
              throw new IOException("Failed to login to FTP Server '" + getHostname() + "' using '"
                      + getLoginname() + "', Reply code '" + client.getReplyCode() + "'");
          }
  
          // mode change should be done only at connect()
          this.changeMode(this.getFileMode());
  
          // the same for server directory
          this.changeServerDirectory(this.getServerDirectory());
          reply = client.getReplyCode();
          if (!FTPReply.isPositiveCompletion(reply)){
              disconnect();
              throw new IOException("Failed to change directory to [" + getServerDirectory() + "] on FTP Server [" + getHostname() + "]; Reply code=[" + client.getReplyCode() + "].");
          }
  
          message("Connect", "Successfully connected to FTP Server [" + getHostname() + "] using file mode=[" + getFileMode()+"]; Connection mode=[" + getConnectionModeString()+ "].");
      }
  </code></pre>
  
  <p>I get below exception always:</p>
  
  <pre><code>java.net.SocketException: Connection reset
      at java.net.SocketInputStream.read(Unknown Source)
      at java.net.SocketInputStream.read(Unknown Source)
      at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
      at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
      at sun.nio.cs.StreamDecoder.read(Unknown Source)
      at java.io.InputStreamReader.read(Unknown Source)
      at java.io.BufferedReader.fill(Unknown Source)
      at java.io.BufferedReader.read(Unknown Source)
      at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
      at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
      at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
      at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:400)
      at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:924)
      at org.apache.commons.net.ftp.FTPSClient._connectAction_(FTPSClient.java:207)
      at org.apache.commons.net.SocketClient.connect(SocketClient.java:183)
      at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
      at com.mycompany.FTPAdapter.connect(FTPAdapter.java:250)
  </code></pre>
  
  <p>I have tried out the executing following command as an adminstrator, but no luck:</p>
  
  <pre><code>netsh advfirewall set global StatefulFTP disable
  </code></pre>
  
  <p>I have created self-signed certificate using IIS on Windows 7. I can connect to the site using FTP/S through FileZilla. It asks me a prompt to trust the certificate and then it can connect to the server.</p>
  
  <p>Can someone guide what am I doing wrong?</p>