4 Replies - 55056 Views - Last Post: 09 August 2010 - 04:52 PM

#1 dragon-slayer  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 06-May 07

FTP in java using Apache Commons Net

Posted 21 August 2007 - 12:31 PM

This Tutorial is aimed at users who wish to learn how to interact with an FTP server in Java.For this we will be using the Apache Commons Net library.


Head over too http://commons.apach...ownload_net.cgi
Then download the latest Binary version.

It is assumed you already know how to the install library.

Create a new java file.

We must import:
import org.apache.commons.net.ftp.*;
and the standered java io (import java.io.*;)

First we must connect to the ftp server. apache commons net makes this a simple task.

We must delcare ftp as an FTPclient so:
FTPClient ftp = new FTPClient();


 ftp.connect(Destination); 

Where String Destination is the ftp server address eg. "127.0.0.1"

If we need to log in we should then use
 ftp.login(username,password); 


But we are forced to declare java.io.IOException and SocketException so they can be thrown so we must surround the code with a try caluse that catches IOEXception and SocketException.
Like so:
  try {
			
			ftp.connect(serverAdd);
			   ftp.login(username,password);
		} catch (SocketException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}




Know me must see the reply from the FTP server so lets Declare reply as an int.
and do
reply = ftp.getReplyCode(); 


then make an if statement to see if the ReplyCode has a positive completion which means the connection was a success
eg:
	if(FTPReply.isPositiveCompletion(reply)){
				   System.out.println("Connected Success");
			   }else {
				   System.out.println("Connection Failed");
				   ftp.disconnect();
			   }



End result might be:
 FTPClient ftp = new FTPClient();
	int reply;
	public void Connect(String username,String password,String serverAdd){
		try {
			
			ftp.connect(serverAdd);
			   ftp.login(username,password);
			   reply = ftp.getReplyCode();
			   
			   if(FTPReply.isPositiveCompletion(reply)){
				   System.out.println("Connected Success");
			   }else {
				   System.out.println("Connection Failed");
				   ftp.disconnect();
			   }
			   
		} catch (SocketException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	 
		
		
	}





Thats the basics of how to connect to an Ftp server.

Downloading

Download from a server is also simple with this library..
We must initialize the File variable for the file to be download Also we must throw the IOException possibily by using a try clause, It is also a good idea to check if the file exists to prevent loss of data.Also we must create an output stream to save the file.

try {
File file = new file("download.txt");
if(file.exists()){
System.out.println("File alrdey exists closing...");
System.exit(1);
}
dfile = new FileOutputStream(file);

ftp.retrieveFile("downloadServer.txt",dfile);

}catch(IOException e){
System.out.println(e)
}


easy right.

Upload

To upload the file we must create a FileInputStream of the file thats going to be put onto the server.

Example:


 
	FileInputStream in = null;
	
	public void upload(String fileDir){
		try {
			 
			in = new FileInputStream(fileDir);
			

		   
	   ftp.storeFile(fileDir,in);
	  
	   
		}catch(IOException e){
			System.out.println(e);
		}
	
	}



Other:
To delete a file just use ftp.deleteFile("file.txt");



Make sure to call
 ftp.disconnect();


in your application when the application is going to exit.

Is This A Good Question/Topic? 0
  • +

Replies To: FTP in java using Apache Commons Net

#2 PrettyHateMachine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-April 07

Re: FTP in java using Apache Commons Net

Posted 24 September 2007 - 09:25 AM

Wicked :ph34r:
Was This Post Helpful? 0
  • +
  • -

#3 michcio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-July 09

Re: FTP in java using Apache Commons Net

Posted 15 July 2009 - 01:41 PM

nice tutorial ... but I have some problem with uploading a file :( I've tried:

FileInputStream fis = new FileInputStream("programfiler/movies.txt");
ftp.storeFile("programfiler/movies.txt",fis);


but I get the SocketTimeOutException ... what am I doing wrong? :(

michcio
Was This Post Helpful? 0
  • +
  • -

#4 jayasree.it  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-August 09

Re: FTP in java using Apache Commons Net

Posted 27 August 2009 - 09:58 PM

FTPFile[] ftpFiles = ftp.listFiles(inputFile);


hi i got a error in this line. The error was

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/oro/text/regex/MalformedPatternException
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createUnixFTPEntryParser(DefaultFTPFileEntryParserFactory.java:169)
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:94)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2358)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2141)
at FTPDownload.copyFile(FTPDownload.java:73)
at FTPDownload.main(FTPDownload.java:275)
what i have to do
Was This Post Helpful? 0
  • +
  • -

#5 Guest_shru*


Reputation:

Re: FTP in java using Apache Commons Net

Posted 09 August 2010 - 04:52 PM

View Postjayasree.it, on 27 August 2009 - 08:58 PM, said:

FTPFile[] ftpFiles = ftp.listFiles(inputFile);


----

use it like
FTPFile[] files = ftp.listFiles();
listfiles does not take input parameter for file - if present - it should only be a PATHname..
like FTPFile[] files = ftp.listFiles("/WEBDATA//MYDIRECOTry");
Was This Post Helpful? 0

Page 1 of 1