01
Feb
10

Browse and Download from FTP using JAVA


This page describes the process of browsing and downloading data from an FTP site using Apache Commons Net FTP library.

We will create a java main application to login into the Bureau of Labor and Statistics to retrieve an index of directories via FTP. The Bureau offers a free ftp site where individuals can retrieve CPI data.

Requirements

  • Maven – installed and configured

Generate a Project using Maven

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
groupId: com.test
artifactId: ftpTest

Answer leave the rest of the questions default.

My pom.xml looks like this.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>ftpTest</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ftpTest</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>commons-net</groupId>
      <artifactId>commons-net</artifactId>
      <version>2.0</version>
    </dependency>
  </dependencies>
</project>

The Java Test case should look like this…

src/test/java/com/test/AppTest.java

package com.test;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.IOException;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
			String server = "ftp.bls.gov";
			String username = "anonymous";
			String password = "anonymous@anonymous.com";
			String directory ="/pub/time.series/cu/";
	
			try {
				FTPClient f=new FTPClient();
			    f.connect(server);
			    f.login(username, password);
			    FTPFile[] files = f.listFiles(directory);
			    for (int i = 0; i < files.length; i++) {
					FTPFile ftpFile = files[i];
					System.out.println(ftpFile.getName());
				}
			    System.out.println("total files: " + files.length);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
}

Run the test case to execute the ftp routine.

mvn test

you should see some output at your console. It wont download anything but show you what is available in the remote ftp directory.

Advertisements

0 Responses to “Browse and Download from FTP using JAVA”



  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

February 2010
S M T W T F S
« Jan   Mar »
 123456
78910111213
14151617181920
21222324252627
28  

Blog Stats

  • 846,577 hits

%d bloggers like this: