java实现ftp下载文件

来源:互联网 发布:淘宝充qb软件 编辑:程序博客网 时间:2024/05/16 06:47

比如要下载ftp://ftp.xx.com/index.html则:

import sun.net.ftp.FtpClient;
import java.io.*;
import sun.net.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author  petehero
 * @version 1.0
 */

public class ftpDown
{

    public ftpDown()
    {

    }
    public static void main(String[] args)
    {
        try
        {
            FtpClient fc=new FtpClient("ftp.xx.com");
            fc.login("username","888888");
            int ch;
            File fi = new File("c://index.html");
            RandomAccessFile getFile = new RandomAccessFile(fi,"rw");
            getFile.seek(0);
            TelnetInputStream fget=fc.get("index.html");
            DataInputStream puts = new DataInputStream(fget);
            while ((ch = puts.read()) >= 0) {
                getFile.write(ch);
            }
            fget.close();
            getFile.close();
            fc.closeServer();
        }
        catch (IOException ex)
        {

            ex.printStackTrace();
        }

    }
}

 

如果文件在某个目录下,则加入fc.cd("foodir");