CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Download directory using FTP

    Hi everyone. I am looking for an easy way to download the contents of a whole directory from an FTP server using Java. Any ideas?

    Regards,
    Theodore
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  2. #2
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Download directory using FTP

    Ok, I have another question. I am trying to simply download a file (known) URL from an FTP server I have. Here's the code:
    Code:
        try{
            URL url = new URL("ftp://USERNAME:PASSWORD@IP:21/DIR/FILE");
            try{
              URLConnection con = url.openConnection();
    
              BufferedInputStream in = new BufferedInputStream(con.getInputStream());
              FileOutputStream out = new FileOutputStream("C:\\File");
    
              int i = 0;
              byte[] bytesIn = new byte[1024];
              while ( (i = in.read(bytesIn)) >= 0) {
                out.write(bytesIn, 0, i);
                System.out.println(i);
              }
              out.close();
              in.close();
            }
            catch (IOException mal) {System.out.println(mal);}
          }
          catch (MalformedURLException mal) {}
      }
    So, when I try to run this code inside an applet, I get the following exception:

    sun.net.ftp.FtpProtocolException: PORT :501 PORT not allowed after EPSV ALL
    Any ideas?
    Thanx,
    Theodore
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  3. #3
    Join Date
    Jul 2003
    Posts
    108

    Smile Re: Download directory using FTP

    Good Luck
    HackmanC

  4. #4
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Download directory using FTP

    Thanx for the reply but I have already read this archive and that didnt help me.
    Any other ideas?
    Regards,
    Theodore
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  5. #5
    Join Date
    Jul 2003
    Posts
    108

    Smile Re: Download directory using FTP

    The whole point was ... use apache commons.
    Like you saw, in the googling, it's a feature which gives a lot of trouble, with SUN libraries.
    Good Luck
    HackmanC

  6. #6
    Join Date
    Jun 2007
    Posts
    8

    Re: Download directory using FTP

    Look at the following java api. The method you are wanting to use is Ftp.downloadDir

    http://www.jscape.com/sftp/

  7. #7
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Download directory using FTP

    Quote Originally Posted by vglass@jscape.com
    Look at the following java api. The method you are wanting to use is Ftp.downloadDir

    http://www.jscape.com/sftp/
    Thanx alot, but this is not a free javaftp client...
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  8. #8
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Download directory using FTP

    Ok guys, as HackmanC suggested I used apache common jar files and I managed to open ftp connections. Now I have another question. The sample code I found downloads a file and its sth like this:

    Code:
        ftp.connect(server);
    ...
        reply = ftp.getReplyCode();
    ...
              if (!ftp.login(username, password)) {
                ftp.logout();
              }
              else
              {
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
                ftp.enterLocalPassiveMode();
                OutputStream output;
                output = new FileOutputStream(fileNameTarget);
                if (ftp.retrieveFile(pathToDownload, output))
                  System.out.println("FILE DOWNLOADED!");
                else
                  System.out.println("A PROBLEM OCCURED WHILE TRYING TO DOWNLOAD THE FILE!");
                output.close();
                ftp.logout();
              }
    ...
    OK this works fine, but I was wondering if I can change the port on which the ftp client is connected. I suppose the above code uses by default, port 21. Can I change the port?
    Thanx in advance,
    THeodore
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  9. #9
    Join Date
    Apr 2007
    Posts
    425

    Re: Download directory using FTP

    I believe since the FTPClient class you are using extends from SocketClient you can call connect() with this prototype:
    Code:
    connect(InetAddress host, int port)
    Dont take my word for it I just gave it a quick look.

  10. #10
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Re: Download directory using FTP

    Thanx alot. I used the connect() method with the port argument (and even tried setDefaultPort(). The problem is that I want to use port 22, in order to connect to a sftp server. And I get a java exception which is like:

    org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
    Server Reply: Protocol mismatch.
    .....

    Can I not achieve sftp connection using the apache class?
    Thanx in advance,
    Theodore
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured