Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Problem trying to retrieve a file from ftp server

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem trying to retrieve a file from ftp server

    Hello, I'm a new member of the community, I hope that you can help me with my issue. I'm trying to retrieve a file from a ftp server (located on my notebook), but I can't do it and I can't figure out why. I can access to the server, list the files on it but when I try to download a test file it just doesn't do it. I'm using commons-net 2.2 library. Here's the code:

    import org.apache.commons.net.ftp.FTPClient;
     
    import java.io.*;
     
    public class ClientFtp {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		String localpath = "/Users/usr_name/Desktop";
    		String remotepath = "/Users/usr_name/ftpfile";
    		FTPClient client = new FTPClient();
    		FileOutputStream fout = null;
     
    		try{
    			fout = new FileOutputStream(localpath + "newfile.txt");
    			client.connect("192.168.1.6", 21);
    			System.out.println(client.getReplyString());
    			client.login("user", "pwd");
    			System.out.println(client.getReplyString());
     
    			if (client.retrieveFile(remotepath + "testfile.txt", fout))
    				System.out.println("download complete");
    			else 
    				System.out.println("error while downloading");
    			fout.flush();
    			fout.close();
    			client.disconnect();
     
    		}
    		catch (IOException e){
    			System.out.println(e.toString());
    		}
     
    	}
     
     
    }

    Can someone give me a hint on what's wrong?
    Thanks
    Last edited by alexdd1987; December 16th, 2010 at 03:23 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem trying to retrieve a file from ftp server

    Can you define what you mean by 'doesn't do it'? Any exceptions thrown?

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem trying to retrieve a file from ftp server

    Quote Originally Posted by copeg View Post
    Can you define what you mean by 'doesn't do it'? Any exceptions thrown?
    no, no exceptions thrown; the method returns false, which means the transfer was not successful, but it gives me no explanation whatsoever..

    EDIT
    This is the output :

    220 192.168.1.6 FTP server (tnftpd 20080929) ready.

    230 User usr logged in.

    error while downloading
    Last edited by alexdd1987; December 16th, 2010 at 03:39 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem trying to retrieve a file from ftp server

    Are you sure you're looking at the correct FTP path? use listFiles() to see if that file exists and is at the correct path.

  5. #5
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Problem trying to retrieve a file from ftp server

    String remotepath = "/Users/usr_name/ftpfile";

    so this line: if (client.retrieveFile(remotepath + "testfile.txt", fout))

    is actually: if (client.retrieveFile("/Users/usr_name/ftpfiletestfile.txt", fout))

    You need a "/" between remotePath and testfile.txt, because /Users/usr_name/ftpfiletestfile.txt is not the file you want to download.

    Should be: if (client.retrieveFile(remotepath + "/testfile.txt", fout))...... Or leave it as is and change remotepath to "/Users/usr_name/ftpfile/"
    Last edited by DavidFongs; December 16th, 2010 at 05:20 PM.

  6. The Following User Says Thank You to DavidFongs For This Useful Post:

    alexdd1987 (December 16th, 2010)

  7. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem trying to retrieve a file from ftp server

    Quote Originally Posted by DavidFongs View Post
    String remotepath = "/Users/usr_name/ftpfile";

    so this line: if (client.retrieveFile(remotepath + "testfile.txt", fout))

    is actually: if (client.retrieveFile("/Users/usr_name/ftpfiletestfile.txt", fout))

    You need a "/" between remotePath and testfile.txt, because /Users/usr_name/ftpfiletestfile.txt is not the file you want to download.

    Should be: if (client.retrieveFile(remotepath + "/testfile.txt", fout))...... Or leave it as is and change remotepath to "/Users/usr_name/ftpfile/"
    Wow, that was it! It was really easy, thank you

  8. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Problem trying to retrieve a file from ftp server

    I'm glad that helped.... It should be noted you will have the same problem when setting up your FileOutputStream

  9. #8
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem trying to retrieve a file from ftp server

    Quote Originally Posted by DavidFongs View Post
    I'm glad that helped.... It should be noted you will have the same problem when setting up your FileOutputStream
    yes, I fixed that too, thanks again

Similar Threads

  1. problem chatting with smack api with openfire server
    By calicratis19 in forum Java SE APIs
    Replies: 2
    Last Post: November 14th, 2013, 12:24 PM
  2. [SOLVED] client-server-router problem
    By KrisTheSavage in forum Java Networking
    Replies: 2
    Last Post: August 23rd, 2010, 05:49 AM
  3. retrieve the video stream from an Axis camera
    By nesrine18 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 3rd, 2010, 03:39 AM
  4. Error in validation.xml file while deploying in server
    By venkat.ravala in forum Web Frameworks
    Replies: 0
    Last Post: January 16th, 2010, 08:56 AM
  5. Download a file to client from server
    By mvittalreddy in forum Java Networking
    Replies: 4
    Last Post: October 5th, 2009, 04:23 AM