This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Ftp Download File Using Java Codes

This is my current code. I can compile it but the output is and error. The purpose of this is to download pdb files from PDB's ftp site. Can anyone enlighten me on this please? Your help is much appreciated(:

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.io.FileOutputStream;


public class ftp {

    public static void main(String[] args) {

        try{

    URL url =     new URL("ftp://ftp.ww.pdb.org//pub//pdb//data//structures//all//pdb//pdb100d.ent.gz;type=i");
    URLConnection con = url.openConnection();

    BufferedInputStream in =     new BufferedInputStream(con.getInputStream());
    FileOutputStream out =     new FileOutputStream("C:\\Users\\Royston\\Documents\\FTP downloads\\pdb100d.ent.gz");

    int i = 0;
    byte[] bytesIn = new byte[1024];

    while ((i = in.read(bytesIn)) >= 0)
     {
    out.write(bytesIn, 0, i);
     }
    out.close();
    in.close();
        } catch(Exception e)
        {
            e.printStackTrace();
        }    
    }   
}

The error I'm getting is this :

java.net.UnknownHostException: ftp.ww.pdb.org
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:525)
    at java.net.Socket.connect(Socket.java:475)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.NetworkClient.openServer(NetworkClient.java:118)
    at sun.net.ftp.FtpClient.openServer(FtpClient.java:488)
    at sun.net.ftp.FtpClient.openServer(FtpClient.java:475)
    at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:270)
    at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:352)
    at ftp.main(ftp.java:25)

Process completed.
java pdb

1 answer

I know almost nothing about Java, but I think the key error is right there in the first line:

java.net.UnknownHostException: ftp.ww.pdb.org at

The correct host name for the PDB FTP site is:

ftp.wwpdb.org

with no period between ww and pdb.

thanks! just saw my mistake there as well.now I've managed to download a file from the ftp however, how do i download all the files using java codes? is using for loop possible?

Use some ftp library, for example Jakarta Commons Net contains FTPClient class

Agreed with Jussi. You shouldn't need to program your own FTP recurser in this day and age - find a library.

Log in to answer this question.