java.net.UnknownHostException not able to connect to ftp

C Sharper picture C Sharper · Feb 24, 2014 · Viewed 8.5k times · Source

I have ftp port as: ftp://173.201.0.1/

I am trying to connect it through following:

String Ftp_Path = "ftp://173.201.0.1/";


    public  List<String>  GetFileList()

    {
         String ftpServerIP = Ftp_Path;
         String ftpUserID = Ftp_UserName;
         String ftpPassword = Ftp_Password;

         FTPFile[] downloadFiles = null;

         StringBuilder result = new StringBuilder();
         FTPClient ftp = new FTPClient();
         List<String> xlsFiles = null;

         try {
            ftp.connect(Ftp_Path);
            ftp.login(ftpUserID, ftpPassword);

            downloadFiles=ftp.listFiles();

            xlsFiles = new ArrayList<String>();
            for(FTPFile i : downloadFiles) {
                if(i.toString().endsWith(".xls")) {
                    xlsFiles.add(i.toString());
                }
            }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         return xlsFiles;

    }

But I am getting error on line:

ftp.connect(Ftp_Path);

Following is the error.

java.net.UnknownHostException: ftp://173.201.0.1/
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at org.apache.commons.net.DefaultSocketFactory.createSocket(DefaultSocketFactory.java:92)
    at org.apache.commons.net.SocketClient.connect(SocketClient.java:201)
    at org.apache.commons.net.SocketClient.connect(SocketClient.java:289)
    at com.amazonaws.mws.samples.ImportRulesPropertyClass.GetFileList(ImportRulesPropertyClass.java:33)
    at com.amazonaws.mws.samples.ManageReportScheduleSample.main(ManageReportScheduleSample.java:74)

Plase help me.

I am new with java.

Answer

Kumar Abhinav picture Kumar Abhinav · Feb 24, 2014

You just need to specify the IP.The FTPClient makes a ftp request.It is not similar to http request.Just change

String Ftp_Path = "ftp://173.201.0.1/";

to 

String Ftp_Path = "173.201.0.1";

Also check whether the ftp port is up and accessible through telnet