954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java & ftp collaboration

i want to download a file from ftp to my local machine using java code. Can anyone help me out pls?? n provide the jar file used in the code.

sazad1
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This is funny - you actually expect someone to build an ftp client for you and send you the jar file. Here you go .

A simple search and a little bit of effort: http://www.jibble.org/simpleftp/

apines
Practically a Master Poster
633 posts since Apr 2007
Reputation Points: 129
Solved Threads: 55
 
import java.io.File;

import com.jscape.inet.ftp.*;

public class FtpDownloadDemo2 {

	public static void main(String[] args) {

		Ftp ftp = new Ftp("ftp.myserver.com", "jsmith", "secret");

		try {
			// establish connection
			ftp.connect();

			// perform directory listing
			String listing = ftp.getDirListingAsString();

			// print directory listing to console
			System.out.println(listing);

			// change local directory
			ftp.setLocalDir(new File("C:/"));

			// Change remote directory
			// ftp.setDir("temp");

			// change transfer mode to ASCII
			// ftp.setAscii();

			// change transfer mode to binary
			ftp.setBinary();

			ftp.download("test.txt");

			// ftp.upload(new File("c:/tmp/test.txt"));
		} catch (FtpException e) {
			e.printStackTrace();
		}

		finally {

			ftp.disconnect();

		}
	}
}
sazad1
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

I've already written code n i got the jar files. But thats not working as it was supposed to work. I dont expect anybody to build the whole code. Just help me out with the best possible code. There are several ways to do this. To select one was kind of difficult. I've tried with jscape,sinetfactory & commons-net.jar

sazad1
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

I think using java.io is the simplest.

public class FtpConnectDemo {
	public static void main (String[] args) {
		FTPClient client = new FTPClient();
		FileInputStream fis = null;
		FileOutputStream fos = null;
		InputStream is = null;
		OutputStream os = null;
		
		try {
			client.connect("ftp.host");
			
			boolean login = client.login("Anonymous", "");
			
			if (login) {
				System.out.println("login sucess....");
				
				
								
				client.setFileType(FTP.BINARY_FILE_TYPE);
 /* download to client 	*/			
			    
			    String fname = "a.txt";
			    fos = new FileOutputStream(fname);
			    String dlPath = "C:/ftp/dl/";
			    
			    boolean ret = client.retrieveFile("/" + fname, fos);
			    
			    boolean logout = client.logout();
				
				if(logout) {
					System.out.println("Logout from FTP Server...");
					
					fos.close();
										
				
				}
			}
			else {
				System.out.println("Login fail....");
			
			}
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
		finally {
			try {
				client.disconnect();
			}
			catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}
}
Eric Cute
Posting Whiz in Training
252 posts since Jun 2010
Reputation Points: 55
Solved Threads: 20
 

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version n umber in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12 4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

sazad1
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

@eric cute...
this is the error. I tried executing ur code.

sazad1
Newbie Poster
5 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: