Question: Ftp Download File Using Java Codes
4
gravatar for User 4686
8.9 years ago by
User 4686220
User 4686220 wrote:

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 • 9.8k views
ADD COMMENTlink modified 9 months ago by RamRS21k • written 8.9 years ago by User 4686220
7
gravatar for Neilfws
8.9 years ago by
Neilfws48k
Sydney, Australia
Neilfws48k wrote:

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 COMMENTlink modified 9 months ago by RamRS21k • written 8.9 years ago by Neilfws48k

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 REPLYlink written 8.9 years ago by User 4686220

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

ADD REPLYlink modified 9 months ago by RamRS21k • written 8.9 years ago by Jussi180

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

ADD REPLYlink written 8.8 years ago by hurfdurf460
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 665 users visited in the last hour