Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

a big doubt.....

CSTechno
2005-03-21
2013-02-24
  • CSTechno
    CSTechno
    2005-03-21

    I tried the following program
    import net.sf.jftp.net.*;
    import java.io.*;

    public class Download {
      private FtpConnection fc;

      public Download() {
        try {
          System.out.println("Downloading.....");
          fc = new FtpConnection("xxx.xxx.xxx.xxx");
          fc.login("user...", "password...");
          fc.download("file to download....");
          fc.disconnect();
         
        } catch (Exception exp) {
        }
      }

    It gives the following error

    ---------- JAVA ----------
    Downloading.....
    Connecting to 193.254.187.103

    > Connecting to 193.254.187.103
    java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:364)
        at java.net.Socket.connect(Socket.java:507)
        at java.net.Socket.connect(Socket.java:457)
        at java.net.Socket.<init>(Socket.java:365)
        at java.net.Socket.<init>(Socket.java:178)
        at net.sf.jftp.net.JConnection.run(JConnection.java:131)
        at java.lang.Thread.run(Thread.java:595)
    FTP not available!

    > FTP not available!
    java.lang.NullPointerException
        at net.sf.jftp.net.JConnection.send(JConnection.java:166)
        at net.sf.jftp.net.FtpConnection.mode(FtpConnection.java:2090)
        at net.sf.jftp.net.FtpConnection.modeStream(FtpConnection.java:2054)
        at net.sf.jftp.net.FtpConnection.rawDownload(FtpConnection.java:946)
        at net.sf.jftp.net.FtpConnection.download(FtpConnection.java:880)
        at Download.<init>(Download.java:12)
        at Download.main(Download.java:20)
    java.lang.NullPointerException @FtpConnection::getLine

    > java.lang.NullPointerException @FtpConnection::getLine
    java.lang.NullPointerException
        at net.sf.jftp.net.JConnection.send(JConnection.java:166)
        at net.sf.jftp.net.FtpConnection.negotiatePort(FtpConnection.java:1639)
        at net.sf.jftp.net.FtpConnection.rawDownload(FtpConnection.java:948)
        at net.sf.jftp.net.FtpConnection.download(FtpConnection.java:880)
        at Download.<init>(Download.java:12)
        at Download.main(Download.java:20)
    java.lang.NullPointerException @FtpConnection::getLine

    > java.lang.NullPointerException @FtpConnection::getLine
    java.lang.NullPointerException
        at net.sf.jftp.net.FtpConnection.negotiatePort(FtpConnection.java:1641)
        at net.sf.jftp.net.FtpConnection.rawDownload(FtpConnection.java:948)
        at net.sf.jftp.net.FtpConnection.download(FtpConnection.java:880)
        at Download.<init>(Download.java:12)
        at Download.main(Download.java:20)
    java.lang.NullPointerException @FtpConnection::download

    > java.lang.NullPointerException @FtpConnection::download
    java.lang.NullPointerException
        at net.sf.jftp.net.JConnection.send(JConnection.java:166)
        at net.sf.jftp.net.FtpConnection.disconnect(FtpConnection.java:1449)
        at Download.<init>(Download.java:13)
        at Download.main(Download.java:20)
    java.lang.NullPointerException @FtpConnection::getLine

    > java.lang.NullPointerException @FtpConnection::getLine

    Output completed (8 sec consumed) - Normal Termination 
      public static void main(String args[]) {
        new Download();
      }
    }

     
    • CSTechno
      CSTechno
      2005-03-21

      test

       
    • Jake Kasprzak
      Jake Kasprzak
      2005-03-25

      Hello.

      Well, with the code you have posted here, it will successfully download the file you specify as long as it logs in correctly and the file you want is there (and you have permission to download it, etc.). But you will get that first exception thrown whenever logging in is unsuccessful. The other exceptions were thrown as a result of trying to download a file then disconnecting from the server after an unsuccessful login attempt. If you check the FtpConnection API, you will see that when running the login() method, it'll return an integer value indicating the result of the login attempt. And the best way to handle this would be to check if it was successful.

      So what you could do is have an integer variable that stores the value returned by the login() method. For example, you could declare an integer variable called "status" then modify your code for logging in to this:

      status =  fc.login("user...", "password...");

      And what you could do is have some code like this after the code for logging in:

      if (status == FtpConnection.LOGIN_OK) {
              fc.download("(file to download)");
              fc.disconnect();
          }
          else {
              System.out.println("Could not log in.");
          }

      Something you could also do is check the integer value returned by the call to the download() method to see if it returns a value of  FtpConnection.TRANSFER_SUCCESSFUL. And if you have any other questions or comments regarding this or anything else about the API, please do not hesitate to ask. It would be good to hear any feedback about this.

      Thanks,

      J.K.

       
    • Cyberdemon
      Cyberdemon
      2005-07-24

      Are you 100 percent sure that the is a ftp server open at the specified address, port 21 and that the connection isn't blocked by a firewall?