Solved

Using "sun.net.ftp.FtpClient" to dowload multiple files from an FTP Site

Posted on 2004-04-26
53
672 Views
Last Modified: 2011-09-20
Hi,

Can somebody please help me with a sample code to just download 3 files from an FTP site using sun.net.ftp.FtpClient. And I need to generate the file names dynamically on the basis of yesterday's date in the following format :

For today the files names would be :-

customer.2004-04-25.data
question.2004-04-25.data
brochure.2004-04-25.data

Also, I would really appreciate if you could stick in some explanation/comments with in the code to give some idea what a particular line is doing.

Thanks for some urgent help!!

Regards,

PCMENIAC
0
Comment
Question by:PCMENIAC
  • 20
  • 14
  • 13
  • +2
53 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10918447
I wouldn't use the FtpClient if i were you. It's not really supported by Sun. You can do this instead:

final String credentialsStub = "ftp://username:password@"; // Use your real uname + p
final String serverDirectory = "xxx.yyy.com/somedir/";

URL url = new URL(credentialsStub + serverDirectory + getFileName("brochure");
InputStream in = url.openStream(); // Now read the stream

...

public String getFileName(String prefix) {
      Calendar cal = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      cal.add(Calendar.DATE, -1);
      return prefix + "." + sdf.format(cal.getTime()) + ".data";
}
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10918547
Hi PCMENIAC,

You can use finj: http://finj.sourceforge.net/

0
 

Author Comment

by:PCMENIAC
ID: 10919012
Hello CEHJ,

Can you please tell me which classes/packages to import for writting this programme. A complete structured layout of the programme would be greatly appreciated.

Thanks very much and Best Regards,

PCMENAIC
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10919116
Nothing special really. Just import:

import java.net.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;

That's about all the code you need (apart from the easy bit of course - reading the stream)
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10919199
API for sun.ftp... is here http://swig.stanford.edu/pub/java/javadoc/
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10919202
API document for sun.ftp... is here http://swig.stanford.edu/pub/java/javadoc/
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10919234
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10919254
That doco's interesting, but of course doesn't alter the basic point - it's not supported by Sun and can't be relied upon apart from the 'quick and dirty'. Doing it as i mentioned will avoid this problem
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 300 total points
ID: 10919325
Here is that 'easy part':


String brochure = getFileName("brochure");
URL url = new URL(credentialsStub + serverDirectory + brochure);
InputStream in = url.openStream(); // Now read the stream
FileOutputStream out = new FileOutputStream(brochure);
final int BUF_SIZE = 8192;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = in.read()) > -1) {
      out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
0
 

Author Comment

by:PCMENIAC
ID: 10919531
MaheshExp,

Thanks for the docu, but I already have gone through it before I posted the question.

CEHJ,

Can you please let me know what is the best way to read in the stream generated?? I am having problems reading it wothout specifying the filename in the brackets for OutputStream().

Thanks!!

PCMENAIC    
0
 

Author Comment

by:PCMENIAC
ID: 10919573
CEHJ,

you also would ahve to import java.IO.* if we are to use InputStream and OutputStream.

Regards,

Parag Chauhan
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10919583
Sorry, a blunder on my part. change

>>FileOutputStream out = new FileOutputStream(brochure);

to

File newFile = new File(brochure);
FileOutputStream out = new FileOutputStream(newFile);


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10919628
Yes

import java.io.*;
0
 

Author Comment

by:PCMENIAC
ID: 10919641
CEHJ,

Could you please explain why have you given brochure in brackets for OutputStream and nothing for InputStream:

InputStream in = url.openStream(); // Now read the stream
FileOutputStream out = new FileOutputStream(brochure);
0
 

Author Comment

by:PCMENIAC
ID: 10919676
Yup,

Thats exactly what I thought....

But still why brochure??? This is not the name of any file. The files after download would be stored with the same name. Please let me know!!

Cheers!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10919725
>>Could you please explain why have you given brochure in brackets for OutputStream and nothing for InputStream:

Yes, it HAS been used for the InputStream (in effect in the URL):

>>
String brochure = getFileName("brochure");
URL url = new URL(credentialsStub + serverDirectory + brochure);
>>

>>But still why brochure???

Passing "brochure" to the method getFileName (today) would return

brochure.2004-04-25.data

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10919742
If you use URL for the FTP protocol, you can also use sun.net.ftp.FtpClient because FtpURLConnection is also undocumented.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10919785
>>...because FtpURLConnection is also undocumented

It's not the lack of documentation that's the problem, it's the lack of support. Sun will always have to return the protocol handlers for the URL
0
 

Author Comment

by:PCMENIAC
ID: 10920322
Hello,

The program below is throwing the following error message: "non-static method getFileName(java.lang.String) cannot be referenced from a static context" at the following line  String broch = getFileName("brochure");

Here is the complete code:

import java.net.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.*;

public class Ftp2
{
   
    public static void main(String[] args)
    {
        final String credentialsStub = "ftp://develop:chillifunk@"; // Use your real uname + p
        final String serverDirectory = "10.150.4.222";
       
        String broch = getFileName("question");
       
               
        try
        {            
            URL url = new URL(credentialsStub + serverDirectory + broch);
            InputStream in = url.openStream(); // Now read the stream
           
            File newFile = new File(broch);
            FileOutputStream out = new FileOutputStream(newFile);
           
            final int BUF_SIZE = 8192;
            byte[] buffer = new byte[BUF_SIZE];
            int bytesRead = -1;
            while((bytesRead = in.read()) > -1)
            {
                out.write(buffer, 0, bytesRead);
            }
            in.close();
            out.close();            
        }              
       
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }    
   
    public String getFileName(String prefix)
    {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.add(Calendar.DATE, -1);        
        return prefix + "." + sdf.format(cal.getTime()) + ".data";
    }      
}

Thanks

PCMENIAC
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10920422
Don't put anything but initialization stuff in main. What you should have in main is something like:

Ftp2 ftp = new Ftp2();
ftp.download();

Put your logic in an instance method

public void download() {
    // logic in here
}
0
 

Author Comment

by:PCMENIAC
ID: 10920653
Hi,

I am able to compile the program alright but having the following error when executing the compiled program:

Exception in thread "main" java.lang.NoClassDefFoundError: ftp2 (Wrong name: ftp2)

at java.lang.ClassLoader.defineClass0(NativeMethod)
at java.lang.ClassLoader.defineClass(ClassLoader.java:509)
at java.security.SecureClassLoader.defineClass(SecureClassLoader3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:246)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)

Can somebody help plese??

Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10920669
Java is case-sensitive. You should compile

javac Ftp2.java

and run

java Ftp2
0
 

Author Comment

by:PCMENIAC
ID: 10921298
Hi,

Its throwing me the following error:

java.net.UnknownHostException: 10.150.4.222question.2004-04-25.data

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java.143)
at java.net.Socket.connect(Socket.java.425)
at java.net.Socket.connect(Socket.java.375)

at sun.net.NetworkClient.doConnect(NetworkClient.java:139)
at sun.net.NetworkClient.openServer(NetworkClient.java:118)

at sun.net.ftp.FtpClient.openServer(NetworkClient.java:387)
at sun.net.ftp.FtpClient.<init>(FtpClient.java:651)

at sun.net..www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:175)
at sun.net..www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection:257)

at java.net.URL.openStream(URL.java:955)
at Ftp2.getFiles(Ftp2.java:46)
at Ftp2.main(Ftp2.java:23)



ALSO, my code again is as follows:


import java.net.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.*;

/**
 *
 * @author  pchauh
 */

public class Ftp2
{
   
    public static void main(String[] args)
    {
        Ftp2 assist = new Ftp2();
        assist.getFiles();
    }  
   
    public String getFileName(String prefix)
    {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.add(Calendar.DATE, -1);        
        return prefix + "." + sdf.format(cal.getTime()) + ".data";
    }
       
    public void getFiles()
    {
        final String credentialsStub = "ftp://develop:chillifunk@"; // Use your real uname + p
        final String serverDirectory = "10.150.4.222";
       
        String broch = getFileName("question");
        String broch1 = getFileName("brochure");
        String broch2 = getFileName("customer");
               
        try
        {            
            URL url = new URL(credentialsStub + serverDirectory + "" + broch2);
            InputStream in = url.openStream(); // Now read the stream
           
            File newFile = new File(broch);
            FileOutputStream out = new FileOutputStream(newFile);
           
            final int BUF_SIZE = 8192;
            byte[] buffer = new byte[BUF_SIZE];
            int bytesRead = -1;
            while((bytesRead = in.read()) > -1)
            {
                out.write(buffer, 0, bytesRead);
            }
            in.close();
            out.close();            
        }              
       
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
   
}
           
Thx.

PCMENIAC
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10921354
serverDirectory should end with a forward slash *after* the name of the directory it's in (unless it's in the root directory)
0
 
LVL 92

Expert Comment

by:objects
ID: 10923280
> lack of support

What support are we talking about exactly???
0
 

Author Comment

by:PCMENIAC
ID: 10926388
Hello,

The files are present as soon as you five the FTP address so are not present in any folder. So would my ServerDirectory be somewhat like this??

final String serverDirectory = "10.150.4.222/";

If I put it like this, the program runs but do not return anything and just gives a blank line on the cmd such as D:\Java>

Thanks for your help!!

PCMENAIC
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:PCMENIAC
ID: 10926399
Apologies, its give rather than five in teh first line of my last comment "The files are present as soon as you five the FTP address so are not present in any folder."

Thx.

0
 
LVL 92

Expert Comment

by:objects
ID: 10926442
are u sure the files are available to ftp and you are ginving the correct details.
can u download them with a different ftp client (manually)?
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 10926511
that code also only grabs one file.
This will grab all three files:

import java.net.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.*;

/**
 *
 * @author  pchauh
 */

public class Ftp2
{
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private static final String credentialsStub = "ftp://develop:chillifunk@"; // Use your real uname + p
    private static final String serverDirectory = "10.150.4.222";
   
    public static void main(String[] args)
    {
        Ftp2 assist = new Ftp2();
        assist.getFiles();
    }  
   
    public String getFileName(String prefix)
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);        
        return prefix + "." + sdf.format(cal.getTime()) + ".data";
    }
       
    public void getFiles()
    {
        getFile("question");
        getFile("brochure");
        getFile("customer");
    }
   
    public void getFile(String name)
    {    
        String filename = getFileName(name);
               
        try
        {            
            URL url = new URL(credentialsStub + serverDirectory + "/" + filename);
            InputStream in = url.openStream(); // Now read the stream
           
            File newFile = new File(filename);
            FileOutputStream out = new FileOutputStream(newFile);
           
            final int BUF_SIZE = 8192;
            byte[] buffer = new byte[BUF_SIZE];
            int bytesRead = -1;
            int nread = 0;
            while((bytesRead = in.read()) > -1)
            {
                  nread += bytesRead;
                out.write(buffer, 0, bytesRead);
            }
            in.close();
            out.close();
            System.out.println(filename+": Bytes read="+nread);          
        }              
       
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
   
}
0
 

Author Comment

by:PCMENIAC
ID: 10926523
Hello Objects,

Thanks for your help in my previous question.

Yes, the files are present there and I can manually download them.

Can you help, please??

Thx..
0
 
LVL 92

Expert Comment

by:objects
ID: 10926548
No worries :)  Are the user/password correct, and are the files in the root directory of the ftp server?
0
 
LVL 92

Expert Comment

by:objects
ID: 10926563
this'll print out exactly what it is trying to download so you can check it:

import java.net.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.*;

/**
 *
 * @author  pchauh
 */

public class Ftp2
{
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private static final String credentialsStub = "ftp://develop:chillifunk@"; // Use your real uname + p
    private static final String serverDirectory = "10.150.4.222";
   
    public static void main(String[] args)
    {
        Ftp2 assist = new Ftp2();
        assist.getFiles();
    }  
   
    public String getFileName(String prefix)
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);        
        return prefix + "." + sdf.format(cal.getTime()) + ".data";
    }
       
    public void getFiles()
    {
        getFile("question");
        getFile("brochure");
        getFile("customer");
    }
   
    public void getFile(String name)
    {    
        String filename = getFileName(name);
               
        try
        {
              
            URL url = new URL(credentialsStub + serverDirectory + "/" + filename);
              System.out.println("Grabbing :"+url);
            InputStream in = url.openStream(); // Now read the stream
           
            File newFile = new File(filename);
            FileOutputStream out = new FileOutputStream(newFile);
           
            final int BUF_SIZE = 8192;
            byte[] buffer = new byte[BUF_SIZE];
            int bytesRead = -1;
            int nread = 0;
            while((bytesRead = in.read()) > -1)
            {
                  nread += bytesRead;
                out.write(buffer, 0, bytesRead);
            }
            in.close();
            out.close();
            System.out.println(filename+": Bytes read="+nread);          
        }              
       
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
   
}
0
 

Author Comment

by:PCMENIAC
ID: 10926609
Thanks,

I'll give it a shot straight away!!

Also, can you tell me whether it is possible to specify the destination for those files within the program as well. I mean if I need to download them from one server (\\server1\) directly to another server (\\server20\) rather than download them to my local machine??

Thanks for letting me know!!

Regards,

PCMENIAC
0
 
LVL 92

Expert Comment

by:objects
ID: 10926655
Just noticed another bug with the code you were given, try this instead:

import java.net.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.*;

/**
 *
 * @author  pchauh
 */

public class Ftp2
{
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private static final String credentialsStub = "ftp://develop:chillifunk@"; // Use your real uname + p
    private static final String serverDirectory = "10.150.4.222";
   
    public static void main(String[] args)
    {
        Ftp2 assist = new Ftp2();
        assist.getFiles();
    }  
   
    public String getFileName(String prefix)
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);        
        return prefix + "." + sdf.format(cal.getTime()) + ".data";
    }
       
    public void getFiles()
    {
        getFile("question");
        getFile("brochure");
        getFile("customer");
    }
   
    public void getFile(String name)
    {    
        String filename = getFileName(name);
               
        try
        {
              
            URL url = new URL(credentialsStub + serverDirectory + "/" + filename);
              System.out.println("Grabbing :"+url);
            InputStream in = url.openStream(); // Now read the stream
           
            File newFile = new File(filename);
            FileOutputStream out = new FileOutputStream(newFile);
           
            final int BUF_SIZE = 8192;
            byte[] buffer = new byte[BUF_SIZE];
            int bytesRead = -1;
            int nread = 0;
            while((bytesRead = in.read(buffer)) > -1)
            {
                  nread += bytesRead;
                out.write(buffer, 0, bytesRead);
            }
            in.close();
            out.close();
            System.out.println(filename+": Bytes read="+nread);          
        }              
       
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
   
}
0
 
LVL 92

Expert Comment

by:objects
ID: 10926662
regards specifying the location to save the file that is certainly possible. How would depend on the server you are copying to and what it supports eg. ftp
0
 

Author Comment

by:PCMENIAC
ID: 10926687
Oh yes, It supports FTP and I can Ping to it as well.

So I was wondering, is it possible to specify a destination location for the files downloaded within the program above?? Can you please tell me the code and where exactly to place it??

Thanks.

0
 
LVL 92

Expert Comment

by:objects
ID: 10926817
try something like:

import java.net.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.*;

/**
 *
 * @author  pchauh
 */

public class Ftp2
{
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private static final String fromServer = "ftp://develop:chillifunk@10.150.4.222/";
    private static final String toServer = "ftp://username:secret@99.99.99.99/";
   
    public static void main(String[] args)
    {
        Ftp2 assist = new Ftp2();
        assist.getFiles();
    }  
   
    public String getFileName(String prefix)
    {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);        
        return prefix + "." + sdf.format(cal.getTime()) + ".data";
    }
       
    public void getFiles()
    {
        getFile("question");
        getFile("brochure");
        getFile("customer");
    }
   
    public void getFile(String name)
    {    
        String filename = getFileName(name);
               
        try
        {
              
            URL from = new URL(fromServer + filename);
              System.out.println("Grabbing :"+from);
            InputStream in = from.openStream();
           
            URL to = new URL(toServer + filename);
            URLConnection conn = to.openConnection();
            conn.setDoOutput(true);
            OutputStream out = conn.getOutputStream();
                              
            final int BUF_SIZE = 8192;
            byte[] buffer = new byte[BUF_SIZE];
            int bytesRead = -1;
            int nread = 0;
            while((bytesRead = in.read(buffer)) > -1)
            {
                  nread += bytesRead;
                out.write(buffer, 0, bytesRead);
            }
            in.close();
            out.close();
            System.out.println(filename+": Bytes read="+nread);          
        }              
       
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
   
}
0
 

Author Comment

by:PCMENIAC
ID: 10927421
Objects,

How about if there are no username and password for toServer??

private static final String fromServer = "ftp://develop:chillifunk@10.150.4.222/";
private static final String toServer = "ftp://username:secret@99.99.99.99/";


Would that be:

private static final String toServer = "ftp://999.99.99.99/";

THx.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10927662
No, you would normally require an anonymous logon. Try something like

private static final String toServer = "ftp://anonymous:something@999.99.99.99/";
0
 

Author Comment

by:PCMENIAC
ID: 10930047
Thanks for the suggestion.

Can you tell me how to delete the files which are downloaded from the FTP site??

Regards.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10930075
String brochure = getFileName("brochure");
boolean deleted = new File(brochure).delete();
0
 

Author Comment

by:PCMENIAC
ID: 11015945
HEllo Cehj,

Can you please explain a bit further regarding the downloaded files to be deleted from the FTP site.

Thanks,

PCMENAIC
0
 
LVL 92

Expert Comment

by:objects
ID: 11019127
you'll need to use an ftp client to achieve that.
0
 

Author Comment

by:PCMENIAC
ID: 11031080
I'm not realy bothered abuot archiving those files. Just wanna delete those files after being downloaded. So was wondering a simple command to delete those files would be GR8!!

Thx..
0
 
LVL 92

Expert Comment

by:objects
ID: 11035540
so did u want to delete the locally downloaded copy, or delete the file from the ftp server?
0
 

Author Comment

by:PCMENIAC
ID: 11068381
Sorry, Object for the delay in getting this message across.

Anyways, YES, thats exactly what I wanna do:- to write a process which downloads the files from an ftp site and then deletes the files that have been downloaded from the server after the download has finished.
This is the last aspect of my question after which I can award the points!!

THanks for your help!!

Regards,

PCMENAIC
0
 
LVL 92

Expert Comment

by:objects
ID: 11072315
sorry still unclear whether you want to delete the local file or the one on the ftp server.
0
 

Author Comment

by:PCMENIAC
ID: 11087590
Sorry, For not being clear!! Delete files on FTP Server.

THx..
0
 
LVL 92

Expert Comment

by:objects
ID: 11087597
you'll need to use an ftp client api for that. eg. sun.net.ftp.FtpClient
0
 
LVL 92

Expert Comment

by:objects
ID: 11087601
/**
 * This class implements a more complete FTP client.
 *
 * @version 1.0, 01/19/97
 * @author  Elliotte Rusty Harold
 */

import java.io.IOException;
import sun.net.TransferProtocolClient;
import sun.net.ftp.FtpClient;
import java.util.Enumeration;
import java.util.Vector;


public class FullFtpClient extends FtpClient {

  /** New FullFtpClient connected to host <i>host</i>. */
  public FullFtpClient(String host) throws IOException {
    super(host);
  }

  /** New FullFtpClient connected to host <i>host</i>, port <i>port</i>. */
  public FullFtpClient(String host, int port) throws IOException {
    super(host, port);
  }

  /** Create an uninitialized FullFTP client. */
  public FullFtpClient() {}

  /** Move up one directory in the ftp file system */
  public void cdup() throws IOException {
    issueCommandCheck("CDUP");
  }

  /** Create a new directory named s in the ftp file system */
  public void mkdir(String s) throws IOException {
    issueCommandCheck("MKDIR " + s);
  }

  /** Delete the specified directory from the ftp file system */
  public void rmdir(String s) throws IOException {
    issueCommandCheck("RMD " + s);
  }

  /** Delete the file s from the ftp file system */
  public void delete(String s) throws IOException {
    issueCommandCheck("DELE " + s);
  }

  /** Get the name of the present working directory on the ftp file system */
  public String pwd() throws IOException {
    issueCommandCheck("PWD");
    StringBuffer result = new StringBuffer();
    for (Enumeration e = serverResponse.elements(); e.hasMoreElements();) {
      result.append((String) e.nextElement());
    }
    return result.toString();
   
  }

}
0
 

Author Comment

by:PCMENIAC
ID: 11234311
Hi Objects,

I still am not able to delete files from the FTP site.

It's throwing up the following error:

java.lang.NullPointerException
at sun.net.TransferProtocolClient.sendServer(TransferProtocolClient.java:96
at sun.net.ftp.FtpClient.issueCommand(FtpClient.java:185)
at TestATC.deleteFileName(TestATC.java:189)
at TestATC.main(TestATC.java.175)

Attached below si the coed that I am using:

************************************************************************************************************

import java.util.Calendar;
import java.io.*;
import java.text.SimpleDateFormat;
import java.net.*;
import sun.net.TransferProtocolClient;
import sun.net.ftp.FtpClient;
import java.util.Enumeration;


public class TestATC extends FtpClient
{

/*********************************************************************************************************************************
                USERNAME AND PASSWORDS for LOGON
**********************************************************************************************************************************/
     
        private static final String logonDetails = "ftp://abc:ck@";
        private static final String serverDetails = "123.45.5.666/";  
       
        private String nameString;
        private String nameString1;
        private String nameString2;
                 
   
    public static void main(String[] args)
    {        
       
        TestATC assist = new TestATC();
       
        for (int i=12; i<15; i++)
        {              
           
           
// ****************************************************************************************************************
//                  Generate filename for customer file                        
// *****************************************************************************************************************          
       
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  // Instance of SimpleDateFormat to generate date in desired format
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE, -i);        
            String nameString =  "customer." + sdf.format(cal.getTime()) + ".data" ;
            System.out.println("the date is:" + nameString);
           
            try  // FTP the customer.2004-04-15.data file            
            {
                URL url = new URL(logonDetails + serverDetails + nameString);
                System.out.println ("Getting URL:" + url);
           
                InputStream in = url.openStream();
           
                File newFile = new File(nameString);
                FileOutputStream out = new FileOutputStream(newFile);
           
                final int bufferSize = 8192;
                byte[] buffer = new byte[bufferSize];
           
                int bytesRead = -1;
                int nread = 0;
           
                while((bytesRead = in.read(buffer)) >-1)
                {
                    nread += bytesRead;
                    out.write(buffer, 0, bytesRead);
                }
           
                in.close();
                out.close();
                System.out.println(nameString + ": Bytes read are = " + nread);            
            }
       
            catch (Exception e)
            {
                e.printStackTrace();
            }
           
           
// ******************************************************************************************************************            
//                  Generate filename for brochure. file                                        
// ******************************************************************************************************************
                       
            Calendar calsec = Calendar.getInstance();
            calsec.add(Calendar.DATE, -i);        
            String nameString1 =  "brochure." + sdf.format(calsec.getTime()) + ".data" ;
            System.out.println("the date is:" + nameString1);
           
            try  // FTP the brochure.2004-04-15.data file            
            {
                URL url = new URL(logonDetails + serverDetails + nameString1);
                System.out.println ("Getting URL:" + url);
           
                InputStream in = url.openStream();
           
                File newFile1 = new File(nameString1);
                FileOutputStream out = new FileOutputStream(newFile1);
           
                final int bufferSize = 8192;
                byte[] buffer = new byte[bufferSize];
           
                int bytesRead = -1;
                int nread = 0;
           
                while((bytesRead = in.read(buffer)) >-1)
                {
                    nread += bytesRead;
                    out.write(buffer, 0, bytesRead);
                }
           
                in.close();
                out.close();
                System.out.println(nameString1 + ": Bytes read are = " + nread);            
            }
       
            catch (Exception e)
            {
                e.printStackTrace();
            }
           
           
// ***************************************************************************************************************            
//                      Generate filename for customer.file                                        
// ******************************************************************************************************************
           
           
            Calendar calthr = Calendar.getInstance();
            calthr.add(Calendar.DATE, -i);        
            String nameString2 =  "question." + sdf.format(calthr.getTime()) + ".data" ;
            System.out.println("the date is:" + nameString2);          
           
            try  // FTP the question.2004-04-15.data file            
            {
                URL url = new URL(logonDetails + serverDetails + nameString2);
                System.out.println ("Getting URL:" + url);
           
                InputStream in = url.openStream();
           
                File newFile2 = new File(nameString2);
                FileOutputStream out = new FileOutputStream(newFile2);
           
                final int bufferSize = 8192;
                byte[] buffer = new byte[bufferSize];
           
                int bytesRead = -1;
                int nread = 0;
           
                while((bytesRead = in.read(buffer)) >-1)
                {
                    nread += bytesRead;
                    out.write(buffer, 0, bytesRead);
                }
           
                in.close();
                out.close();
                System.out.println(nameString2 + ": Bytes read are = " + nread);            
            }
       
            catch (Exception e)
            {
                e.printStackTrace();
            }
           
// ******************************************************************************************************************
//                              DELETE THE FILES              
// *****************************************************************************************************************            
            assist.deleteFileName(nameString, nameString1, nameString2);      
        }
    }
   

// ******************************************************************************************************************
//                          METHOD TO DELETE THE FILES              
// *****************************************************************************************************************            
 
   
    public void deleteFileName(String fileName, String fileName1, String fileName2)
    {
       try
       {
           issueCommand("DELE" + fileName);
           issueCommand("DELE" + fileName1);
           issueCommand("DELE" + fileName2);
       }
       
      catch (Exception e)
            {
                e.printStackTrace();
            }
       
    }
   
}
       
THanks




0
 
LVL 92

Expert Comment

by:objects
ID: 11237469
you don't connectt your FtpClient. ie. you have not connected to ftp server.

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now