Ftp Download File Using Java Codes
1
4
Entering edit mode
13.8 years ago
User 4686 ▴ 220

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 • 11k views
ADD COMMENT
7
Entering edit mode
13.8 years ago
Neilfws 49k

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.

ADD COMMENT
0
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2394 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6