Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need example for getting text file content from an FTP Server 1

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I am looking for a code example for retreving a content of a text file located on an FTP Server. I would like to know how to open a connection with the FTP and how to open a stream with a certain file located on that FTP.
 
The JDK does not have any official FTP APIs, however there is an API from Sun that is undocumented.

There are also quite a few 3rd party APIs out there - try a google for "FTP java" - Apache Commons has one for a start.

Alternately, here is some code I knocked up years ago to do FTP that uses the undocumented Sun code. It doesn't seem work with all FTP servers, but it does with mine !

Code:
import sun.net.ftp.*;
import sun.net.*;
import java.io.*;


public class FTP {
    public static void main(String args[]) throws Exception {
        // Conatct server and log in
		String server = "192.168.1.4";
        String user = "joe";
        String passwd = "bloggs";


        FtpClient client = new FtpClient();
        System.err.println("Opening server ...");
        client.openServer(server);
        System.err.println("Logging in...");
        client.login(user, passwd);
        System.err.println("Changing to binary ...");
        client.binary();//

        // change dir
        //client.cd("transfer");

        // pwd
        String pwd = client.pwd();
        System.err.println("cwd : " +pwd);

		TelnetInputStream in;
		BufferedOutputStream bos;
		int buffer = 0;

        // list dir
        System.err.println("Listing dir ...");
        in = client.list();
        bos = new BufferedOutputStream(System.out);
        while ((buffer = in.read()) != -1) {
            bos.write(buffer);
        }

        bos.flush();
        bos.close();
        in.close();

        // Get a file
        System.err.println("Getting file ...");
        in = client.get("gddemo_jpg.c");
        bos = new BufferedOutputStream(new FileOutputStream("C:/ftp_downloaded.txt"));
        buffer = 0;
        while ((buffer = in.read()) != -1) {
            bos.write(buffer);
        }

        bos.flush();
        bos.close();
        in.close();

        // Send a file
        TelnetOutputStream out = client.put("floodmap.png");
        File file = new File("C:/floodmap.png");
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        byte[] b = new byte[(int)file.length()];
        bis.read(b);
        out.write(b);

        out.flush();
        out.close();
        bis.close();


        client.closeServer();


    }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top