根据前面的FtpUtil写一个demo

说说现在开发中一般都是对象化,对于配置文件也不例外。

1、FTPConfig 配置类

/***
*
* @author 
*
*/
public class FTPConfig {
private String server;
private int port;
private String username;
private String password;
private String location;

public String getServer() {
return server;
}

public void setServer(String server) {
this.server = server;
}

public int getPort() {
return port;
}

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

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}
public FTPConfig(){

}

public FTPConfig(String server, int port, String username, String password, String location) {
this.server = server;
this.port = port;
this.username = username;
this.password = password;
this.location = location;
}
}

2、一个真正的FtpDemo 开始了

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.List;

public class FtpDemo {
public static void main(String[] args) {
FTPConfig ftpConfig = new FTPConfig();
ftpConfig.setServer("198.1.1.12");
ftpConfig.setPort(21);
ftpConfig.setUsername("ftpuserdemo");
ftpConfig.setPassword("ftpuserdemo");
ftpConfig.setLocation("");
FtpUtil ftpUtil = new FtpUtil();
try {
ftpUtil.connectServer(ftpConfig);
List<String> list = ftpUtil.getFileList("/home/ftpuser/demo/");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

如果需要跑通demo的话要主要两点,

第一是加上commons-net-3.3.jar,

第二需要修改这里的配置改成自己的配置就好。

祝大家好运。。。

posted @ 2016-04-25 16:07  麻雀虽小五脏俱全  阅读(315)  评论(0编辑  收藏  举报