Lalchand
Lalchand

Reputation: 7827

Java code for FTP client

I have wrote a simple code to download a file from a FTP server using the following connection string:

  URL url = new URL("ftp://test:[email protected]/sample.txt;type=i");
    URLConnection urlc = url.openConnection();

But while I tried to read the content from the above connection and write on my local drive I got only 61KB. When I opened thesample.txt it has the following content:

06-16-2011  02:47PM           1228317425 sample.txt

But the original size of the sample.txt in FTP is 1.14GB.

urlc.getContentLength() returned -1 for me.

here is my entire code

public void download( String ftpServer, String user, String password,
             String fileName, File destination ) throws MalformedURLException,
             IOException
       {
          if (ftpServer != null && fileName != null && destination != null)
          {
             StringBuffer sb = new StringBuffer( "ftp://" );
             // check for authentication else assume its anonymous access.
             if (user != null && password != null)
             {
                sb.append( user );
                sb.append( ':' );
                sb.append( password );
                sb.append( '@' );
             }
             sb.append( ftpServer );
             sb.append( '/' );
             sb.append( fileName );
             /*
              * type ==> a=ASCII mode, i=image (binary) mode, d= file directory
              * listing
              */
             sb.append( ";type=i" );
             BufferedInputStream bis = null;
             BufferedOutputStream bos = null;
             System.out.println(sb);
             try
             {
                URL url = new URL( sb.toString() );
                URLConnection urlc = url.openConnection();
                System.out.println(urlc.getContentType());
                System.out.println(urlc.toString());
                System.out.println(urlc.getContentLength());
                bis = new BufferedInputStream( urlc.getInputStream() );
                bos = new BufferedOutputStream( new FileOutputStream("D://FTP/"+
                      destination.getName() ) );

                int i;
                while ((i = bis.read()) != -1)
                {
                   bos.write( i );
                }
             }
             finally
             {
                if (bis != null)
                   try
                   {
                      bis.close();
                   }
                   catch (IOException ioe)
                   {
                      ioe.printStackTrace();
                   }
                if (bos != null)
                   try
                   {
                      bos.close();
                   }
                   catch (IOException ioe)
                   {
                      ioe.printStackTrace();
                   }
             }
          }
          else
          {
             System.out.println( "Input not available" );
          }
       }

Upvotes: 1

Views: 11534

Answers (3)

cjstehno
cjstehno

Reputation: 13984

You might want to use an existing library rather than rolling your own, try Apache Commons Net.

Upvotes: 0

sbrattla
sbrattla

Reputation: 5386

No need to reinvent the wheel. I'd recommend using FTP4J which I have used in several projects for file upload/download. http://www.sauronsoftware.it/projects/ftp4j/. I assume the library should be able to take care of the issues for you.

Upvotes: 1

planetjones
planetjones

Reputation: 12633

You'll need to open the connection and start streaming the result e.g.

URL url = new URL("ftp://test:[email protected]/sample.txt;type=i");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream(); // To download
etc. etc.

Have you done that? The basic JDK support is limited to streaming the data. You might want to look at a Java FTP client which allows you to interact more richly with the FTP Protocol. I don't have much experience in this area but Apache Commons Net is one such example. With this you would read files as org.apache.commons.net.ftp.FtpFile Objects, which has a getter for the file size.

BTW: -1 for the getContentLength() call is just the default if it can't find a header named "content-length". So I think in your case, because this is FTP the method is not relevant. This adds further weight to try and use a dedicated FTP Library.

Upvotes: 2

Related Questions