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.

Recommended Answers

All 6 Replies

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/

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();

		}
	}
}

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

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();
			}
		}
	}
}

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)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.