commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Menahem <Jonathan.Mena...@olf.com>
Subject connecting to FTP site through proxy
Date Wed, 24 Nov 2010 16:21:49 GMT
Hello,

I am trying to run this simple code to connect to FTP site through a proxy.

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class MyTest {

  public static void main(String[] args) {
    String ftpHostName = "ftp.xxx.com";
    int ftpPort = 21;
    String ftpUserName = "myUserName";
    String ftpPassword = "myPassword";

    System.setProperty("socksProxyHost" ,"10.148.0.131");
    System.setProperty("socksProxyPort", "1080");

    FTPClient ftpClient = new FTPClient();

    try {
      System.out.println("connecting");
      ftpClient.connect(ftpHostName, ftpPort);
      System.out.println("connected");
      System.out.println("loging in");
      boolean successLogin = ftpClient.login(ftpUserName, ftpPassword);

      if(successLogin)
        System.out.println("success login");
      else
        System.out.println("fail login");
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      try {
        System.out.println("loging out");
        ftpClient.logout();
        System.out.println("disconecting");
        ftpClient.disconnect();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}

I am getting the following error:
C:\temp\ftp\test>java.exe -cp ./commons-net-ftp-2.0.jar;. MyTest
connecting
java.net.SocketException: Malformed reply from SOCKS server
        at java.net.SocksSocketImpl.readSocksReply(SocksSocketImpl.java:87)
        at java.net.SocksSocketImpl.connectV4(SocksSocketImpl.java:265)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:437)
        at java.net.Socket.connect(Socket.java:519)
        at org.apache.commons.net.SocketClient.connect(SocketClient.java:176)
        at MyTest.main(MyTest.java:23)
loging out
java.lang.NullPointerException
        at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:471<ftp://FTP.java:471>)
        at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:534<ftp://FTP.java:534>)
        at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:583<ftp://FTP.java:583>)
        at org.apache.commons.net.ftp.FTP.quit(FTP.java:794<ftp://FTP.java:794>)
        at org.apache.commons.net.ftp.FTPClient.logout(FTPClient.java:697)
        at MyTest.main(MyTest.java:39)


I am able to do this using a different FTP client library, ftp4j-1.5.1<ftp://ftp4j-1.5.1>
using the following code:

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.connectors.SOCKS4Connector;

public class MyTestFtp4J {

  public static void main(String[] args) {
    String ftpHostName = "ftp.xxx.com";
    int ftpPort = 21;
    String ftpUserName = " myUserName ";
    String ftpPassword = " myPassword";

    FTPClient ftpClient = new FTPClient();
    ftpClient.setConnector(new SOCKS4Connector("10.148.0.131", 1080));

    try {
      System.out.println("connecting");
      ftpClient.connect(ftpHostName, ftpPort);
      System.out.println("connected");
      System.out.println("loging in");
      ftpClient.login(ftpUserName, ftpPassword);
      System.out.println("success login");
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      try {
        System.out.println("disconecting");
        ftpClient.disconnect(true);
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}

So I know the proxy settings are correct.
The java version I used to compile and run my apps is 1.6.0_06
Does anyone can help figure out what is wrong when I use the apache commons net FTP client?

Thank you

Jon


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message