Results 1 to 5 of 5

Thread: FTP in console app

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375

    FTP in console app

    Anyone know where I can find some examples of how to FTP (pull from a server only, no uploading) a file via java in a console app?

    Thanks!

    Morgan
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    This is not hard at all. Check out sun's ftp client. It is not documented by sun at all by you can find some info on it at the following:

    http://www.informatik.tu-freiberg.de...colClient.html

    http://www.aoindustries.com/docs/aoc...FtpClient.html


    here is an example that shows how to upload and download.

    Code:
    import sun.net.*;
    import sun.net.ftp.*;
    import java.io.*;
    
    
    public class ftp_utility {
    
    public ftp_utility () {
    FtpClient fcMyFtp = new FtpClient();
    
    try {
    int ch;
    fcMyFtp.openServer("myftpsite");
    fcMyFtp.login("myname","mypassword");
    
    TelnetInputStream tisList =
    fcMyFtp.list();
    while( (ch = tisList.read()) != -1 )
    System.out.print((char)ch);
    // ... other stuff.
    fcMyFtp.cd("temp");
    fcMyFtp.binary();
    
    // writing a file:
    FileInputStream fis = new
    FileInputStream( "/home/gadio/temp/tempfile" );
    TelnetOutputStream  tos =
    fcMyFtp.put("tempfile.new");
    byte buffer[] = new byte[1000];
    int len;
    while( (len = fis.read(buffer)) != -1 ) {
    tos.write(buffer,0,len);
    }
    fis.close();
    tos.close();
    
    // reading a file:
    FileOutputStream fos = new
    FileOutputStream( "/home/gadio/temp/tempfi
    le.read" );
    TelnetInputStream   tis =
    fcMyFtp.get("tempfile.new");
    while( (len = tis.read(buffer)) != -1 ) {
    fos.write(buffer,0,len);
    }
    tis.close();
    fos.close();
    } catch( IOException e ) {
    e.printStackTrace();
    }
    }
    
    public static void main(String args[]) {
    ftp_utility f = new ftp_utility();
    }
    }
    Last edited by billrogers; Oct 10th, 2002 at 01:28 PM.

  3. #3
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    another thing you could do is execute a runtime to use say the microsoft ftp client, but you will lose platform independence doing this which may not be bad if you know what platform you are always going to run on.
    Last edited by billrogers; Oct 10th, 2002 at 02:24 PM.

  4. #4

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    That worked beautifully, thank you!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  5. #5
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    your welcome, glad to be of help

Posting Permissions

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



Click Here to Expand Forum to Full Width