Results 1 to 7 of 7
  1. #1
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default listing files at the FTP

    hi, i've just begun the java. And i want to file information (name, date or md5) on FTP server. I succeed to upload to FTP server.
    but, i can't download and get file information.
    i use sun.net.ftp.FtpClient library for upload file.
    how i can list file and get information (date, name) ?

    Thanks.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    you can use the method list(), that returns a TelnetInputStream.
    You could print the return e.g. with the Scanner class
    Java Code:
    		Scanner sc = new Scanner(ftpClient.list());
    		while(sc.hasNextLine()){
    			System.out.println(sc.nextLine());
    		}
    you will receive the date, size, name etc.
    with cd(String s) you can change the directory
    with get(String s) you will get a TelnetInputStream too and you can download the file

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    sun.net.ftp.FtpClient

    Does this class still exist? Back when it used to the advice commonly given about using *any* of the sun. packages was: "Don't. They aren't portable and they aren't supported."

    You might want to consider Apache's FTP client which is part of Apache Commons (Commons Net - Jakarta Commons Net) or some other Java ftp client.

  4. #4
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    the codes are follows. werite "abc" on terminal and nothing anything.
    i check on filezilla and i see uploaded the file , but not listed in the directory.

    Java Code:
     public void uploadDumpFiles2Server(){
            String server="ftp.domain.tr";
            String user="username";
            String password="password";
            String path="/httpdocs";
    String filename="user.props";
            try {
                FtpClient ftpClient=new FtpClient();
                ftpClient.openServer(server);
                ftpClient.login(user, password);
                if (path.length()!=0) ftpClient.cd(path);
                ftpClient.binary();
                TelnetOutputStream os=ftpClient.put(filename);
                
                File file_in=new File(filename);
                FileInputStream is=new FileInputStream(file_in);
                byte[] bytes=new byte[1024];
                int c;
                while ((c=is.read(bytes))!=-1){
                os.write(bytes,0,c);}
                is.close();
               Scanner sc = new Scanner(ftpClient.list());
    		while(sc.hasNextLine()){
    			System.out.println(sc.nextLine());
    		}
                os.close();
                ftpClient.closeServer();
            } catch (IOException ex) {;
                System.out.println(ex.toString());
            }

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Quote Originally Posted by oulutas View Post
    the codes are follows. werite "abc" on terminal and nothing anything.
    i check on filezilla and i see uploaded the file , but not listed in the directory.

    Java Code:
     public void uploadDumpFiles2Server(){
            String server="ftp.domain.tr";
            String user="username";
            String password="password";
            String path="/httpdocs";
    String filename="user.props";
            try {
                FtpClient ftpClient=new FtpClient();
                ftpClient.openServer(server);
                ftpClient.login(user, password);
                if (path.length()!=0) ftpClient.cd(path);
                ftpClient.binary();
                TelnetOutputStream os=ftpClient.put(filename);
                
                File file_in=new File(filename);
                FileInputStream is=new FileInputStream(file_in);
                byte[] bytes=new byte[1024];
                int c;
                while ((c=is.read(bytes))!=-1){
                os.write(bytes,0,c);}
                is.close();
               Scanner sc = new Scanner(ftpClient.list());
    		while(sc.hasNextLine()){
    			System.out.println(sc.nextLine());
    		}
                os.close();
                ftpClient.closeServer();
            } catch (IOException ex) {;
                System.out.println(ex.toString());
            }

    mhm..I have tested your posted code and it works fine when I put the line os.close(); before the line Scanner sc = new Scanner(ftpClient.list());

    But I agree with pbrockway2 you should not use the sun packages if possible(see:FAQ - Sun Packages)

  6. #6
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks. But 've a problem. download apache coomon file.
    but, how to i import this library ?
    I tried something but could not succeed.

  7. #7
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Listing Installed DSA
    By anoopasta in forum XML
    Replies: 1
    Last Post: 05-06-2010, 04:10 PM
  2. Replies: 8
    Last Post: 11-20-2009, 09:33 PM
  3. Listing subdirectories/files with filter
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:20 AM
  4. Listing subdirectories/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:19 AM
  5. Listing all available Locales
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 04:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •