Archive for the 'FTP' Category

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.




Follow

Get every new post delivered to your Inbox.

Join 49 other followers