Subscribe via E-mail

Your email:
Download Now
Free Consultation
Request Demo

Blog

Managed File Transfer and Network Solutions

Current Articles | RSS Feed RSS Feed

Transferring Files with Java FTP Library

  
  
  
  
  

java ftpHere's some philosophy to go with the Secure FTP Factory library. There are some things in life that you should never do unless you feel completely secure. One of them is withdrawing money from an ATM.  You should do this only in a safe, well lit and preferably high traffic environment where you are unlikely to be robbed. Another is uploading or downloading files using plain FTP (File Transfer Protocol).

The problem is that the FTP protocol was defined back in 1971 in the Age of Aquarius, when we were all mellow. It transmits your user name and password in clear text. These days, the Internet is full of bad guys. You should never use plain FTP unless you're feeling completely secure, which means that it won't bother you if somebody gets into your FTP account, and that you are using a password you have never used anywhere else.

Given that, Secure FTP Factory makes it easy to transfer files using FTP. Once you have imported the library, you create an Ftp object using the hostname, username and password. Then you can use self-explanatory methods such as connect() (establish an FTP connection), setBinary() (use binary mode rather than ASCII text mode) and mupload() (multiple file upload using a regular expression).

Here's an example to upload some batch files:

Uploading files with Java FTP

import com.jscape.inet.ftp.*;
import java.io.*;
import java.util.Enumeration;

public class FTPUpload extends FtpAdapter {
    private String hostname;
    private String username;
    private String password;
    
    // perform single file upload
    public void doUpload(String hostname, String username, String password, String filter) throws FtpException {
        Ftp ftp = new Ftp(hostname,username,password);
        
        //capture Ftp related events
        ftp.addFtpListener(this);
        ftp.connect();
        ftp.setBinary();
        ftp.mupload(filter);
        ftp.disconnect();
    }
    
    public static void main(String[] args) {
        String hostname = "ftp.somewebsite.com";
        String username = "trusting_puppy";
        String password = "12345678";
        String filter = ".*\\.bat";
        try {
            FTPUpload theUploader = new FTPUpload();
            theUploader.doUpload(hostname,username,password,filter);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Since "trusting_puppy" has chosen the password "12345678", we can agree that he deserves whatever happens to him. (You can search the Web on "bad passwords" to find more, and even worse, examples.) However, plain FTP sends even well-chosen passwords in clear text, and anyone sniffing your network can see them.

Downloading is similar. This example sucks down GIF images. In this case, we have used separate methods to set each of the parameters, just for demonstration.

Downloading files with Java FTP

import com.jscape.inet.ftp.*;
import java.io.*;
import java.util.Enumeration;

public class FTPDownload extends FtpAdapter {
    private String hostname;
    private String username;
    private String password;
    private String filter;
    
    // perform multiple file download
    public void doDownload(String hostname, String username, String password, String filter) throws FtpException {
        Ftp ftp = new Ftp();
        
        //capture Ftp related events
        ftp.addFtpListener(this);
        ftp.setHostname(hostname);
        ftp.setUsername(username);
        ftp.setPassword(password);
        ftp.connect();
        ftp.setBinary();
        ftp.mdownload(filter);
        ftp.disconnect();
    }
    
    public static void main(String[] args) {
		String hostname = "ftp.somewebsite.com";
		String username = "trusting_puppy";
		String password = "12345678";
		String filter = ".*\\.gif";
        try {
            FTPDownload theDownloader = new FTPDownload();
            theDownloader.doDownload(hostname,username,password,filter);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Of course, this article has been something like the button labelled "Never Press This Button!". In the following parts, we'll show you how to transfer files safely using a variety of secure file transfer techniques.

Would you like to try Secure FTP Factory for FREE? 

 

Download a FREE edition ofSecure FTP Factory

 

Comments

There are no comments on this article.
Comments have been closed for this article.