Chinaunix首页 | 论坛 | 博客
  • 博客访问: 425809
  • 博文数量: 79
  • 博客积分: 2886
  • 博客等级: 少校
  • 技术积分: 968
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-16 10:33
文章分类

全部博文(79)

文章存档

2013年(7)

2012年(17)

2011年(28)

2010年(25)

2009年(1)

2008年(1)

我的朋友

分类: Java

2011-03-30 17:16:01

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);
    }
}


阅读(1082) | 评论(0) | 转发(0) |
0

上一篇:I/O操作类

下一篇:RMI系统学习

给主人留下些什么吧!~~