• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

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

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
PCMENIAC
Asked:
PCMENIAC
  • 20
  • 14
  • 13
  • +2
2 Solutions
 
CEHJCommented:
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
 
WebstormCommented:
Hi PCMENIAC,

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

0
 
PCMENIACAuthor Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CEHJCommented:
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
 
maheshexpCommented:
API for sun.ftp... is here http://swig.stanford.edu/pub/java/javadoc/
0
 
maheshexpCommented:
API document for sun.ftp... is here http://swig.stanford.edu/pub/java/javadoc/
0
 
CEHJCommented:
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
 
CEHJCommented:
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
 
PCMENIACAuthor Commented:
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
 
PCMENIACAuthor Commented:
CEHJ,

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

Regards,

Parag Chauhan
0
 
CEHJCommented:
Sorry, a blunder on my part. change

>>FileOutputStream out = new FileOutputStream(brochure);

to

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


0
 
CEHJCommented:
Yes

import java.io.*;
0
 
PCMENIACAuthor Commented:
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
 
PCMENIACAuthor Commented:
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
 
CEHJCommented:
>>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
 
WebstormCommented:
If you use URL for the FTP protocol, you can also use sun.net.ftp.FtpClient because FtpURLConnection is also undocumented.

0
 
CEHJCommented:
>>...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
 
PCMENIACAuthor Commented:
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
 
CEHJCommented:
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
 
PCMENIACAuthor Commented:
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
 
CEHJCommented:
Java is case-sensitive. You should compile

javac Ftp2.java

and run

java Ftp2
0
 
PCMENIACAuthor Commented:
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
 
CEHJCommented:
serverDirectory should end with a forward slash *after* the name of the directory it's in (unless it's in the root directory)
0
 
objectsCommented:
> lack of support

What support are we talking about exactly???
0
 
PCMENIACAuthor Commented:
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
 
PCMENIACAuthor Commented:
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
 
objectsCommented:
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
 
objectsCommented:
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
 
PCMENIACAuthor Commented:
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
 
objectsCommented:
No worries :)  Are the user/password correct, and are the files in the root directory of the ftp server?
0
 
objectsCommented:
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
 
PCMENIACAuthor Commented:
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
 
objectsCommented:
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
 
objectsCommented:
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
 
PCMENIACAuthor Commented:
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
 
objectsCommented:
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
 
PCMENIACAuthor Commented:
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
 
CEHJCommented:
No, you would normally require an anonymous logon. Try something like

private static final String toServer = "ftp://anonymous:something@999.99.99.99/";
0
 
PCMENIACAuthor Commented:
Thanks for the suggestion.

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

Regards.
0
 
CEHJCommented:
String brochure = getFileName("brochure");
boolean deleted = new File(brochure).delete();
0
 
PCMENIACAuthor Commented:
HEllo Cehj,

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

Thanks,

PCMENAIC
0
 
objectsCommented:
you'll need to use an ftp client to achieve that.
0
 
PCMENIACAuthor Commented:
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
 
objectsCommented:
so did u want to delete the locally downloaded copy, or delete the file from the ftp server?
0
 
PCMENIACAuthor Commented:
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
 
objectsCommented:
sorry still unclear whether you want to delete the local file or the one on the ftp server.
0
 
PCMENIACAuthor Commented:
Sorry, For not being clear!! Delete files on FTP Server.

THx..
0
 
objectsCommented:
you'll need to use an ftp client api for that. eg. sun.net.ftp.FtpClient
0
 
objectsCommented:
/**
 * 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
 
PCMENIACAuthor Commented:
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
 
objectsCommented:
you don't connectt your FtpClient. ie. you have not connected to ftp server.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 20
  • 14
  • 13
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now