import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FtpTool {
    String ip;
    int port;
    String user;
    String pwd;
    String remotePath;
    String localPath;
    FtpClient ftpClient;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getRemotePath() {
        return remotePath;
    }

    public void setRemotePath(String remotePath) {
        this.remotePath = remotePath;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public FtpClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FtpClient ftpClient) {
        this.ftpClient = ftpClient;
    }

    public boolean connectServer(String ip, int port, String user, String pwd)
            throws Exception {
        boolean isSuccess = false;
        try {
            ftpClient = new FtpClient();
            ftpClient.openServer(ip, port);
            ftpClient.login(user, pwd);
            isSuccess = true;
        } catch (Exception ex) {
            throw new Exception("Connect ftp server error:" + ex.getMessage());
        }
        return isSuccess;
    }
    public void downloadFile(String remotePath,String localPath, String filename) throws Exception {
        try {
            if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
                if (remotePath.length() != 0)
                    ftpClient.cd(remotePath);
                ftpClient.binary();
                TelnetInputStream is = ftpClient.get(filename);
                File file_out = new File(localPath + File.separator + filename);
                FileOutputStream os = new FileOutputStream(file_out);
                byte[] bytes = new byte[1024];
                int c;
                while ((c = is.read(bytes)) != -1) {
                    os.write(bytes, 0, c);
                }
                is.close();
                os.close();
                ftpClient.closeServer();
            }
        } catch (Exception ex) {
            throw new Exception("ftp download file error:" + ex.getMessage());
        }
    }
    public void uploadFile(String remotePath,String localPath, String filename) throws Exception {
        try {
            if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
                if (remotePath.length() != 0)
                    ftpClient.cd(remotePath);
                ftpClient.binary();
                TelnetOutputStream os = ftpClient.put(filename);
                File file_in = new File(localPath + File.separator + 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();
                os.close();
                ftpClient.closeServer();
            }
        } catch (Exception ex) {
            throw new Exception("ftp upload file error:" + ex.getMessage());
        }
    }
}


单元测试代码:
测试目录结构:D:\upload\为FTP主目录 
D:\upload\To为FTP主目录下的子目录

import static org.junit.Assert.*;
import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class FtpToolTest extends TestCase{
    FtpTool ftpTool;
    public FtpToolTest(String arg0) {
        super(arg0);
    }
    @Before
    public void setUp() throws Exception {
        ftpTool = new FtpTool();
        ftpTool.setIp("10.151.125.74");
        ftpTool.setPort(21);
        ftpTool.setUser("test");
        ftpTool.setPwd("test");
        ftpTool.setRemotePath(".\\To\\");
        super.setUp();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

//    @Test

//    public void testDownloadFile() {

//        try {

//            ftpTool.downloadFile(ftpTool.getRemotePath(),"d:/upload","sourcefile.txt");

//        } catch (Exception e) {

//            e.printStackTrace();

//        }

//    }

    
//    @Test

//    public void testUploadFile() {

//        try {

//            ftpTool.uploadFile(ftpTool.getRemotePath(),"d:/upload","descfile.txt");

//        } catch (Exception e) {

//            e.printStackTrace();

//        }

//    }

    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(FtpToolTest.class);
    }
}


10-02 07:55