title: Java - How to determine if server is FTP or SFTP?
body: <p>I'm making a class in Java that downloads a specific file from a server. I have a method that can download directly from an FTP server and one from an SFTP server. </p>
  
  <p>Without any assumptions being made on the hostname (without checking if it starts with ftp:// or sftp://, as sometimes the server may be local), is there any way to determine if a server is FTP or SFTP, and therefore which method to use?</p>
  
  <p>Ideally, I'd like to determine programmatically, and not just to try the alternative if it fails. Thanks for any help in advance!</p>
  
  <p>EDIT:
  For anyone interested, my very basic and definitely not perfect solution is somethign like this:</p>
  
  <pre><code>private int determineServerProtocol(String host, String port) {
          PrintWriter out = null;
          BufferedReader in = null;
          String result = "";
          try (Socket socket = new Socket(host, Integer.parseInt(port))) {
              out = new PrintWriter(socket.getOutputStream(), true);
              in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              result = in.readLine();
              out.close();
              in.close();
          } catch (NumberFormatException | IOException e) {
              e.printStackTrace();
          }
          if (result.contains("SSH")) {
              System.out.println("Server is SFTP");
              // do things...
          } else {
              System.out.println("Server is FTP");
              // do things...
          }
      }
  </code></pre>