- package com.ccb.ftp;
- import java.io.IOException;
- import java.net.SocketException;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPListParseEngine;
- public class FTPClientTool {
- /**
- * @param args
- */
- public static void main(String[] args) {
- FTPClient client = new FTPClient();
- try {
- //连接指定的ftp服务器,需要设定好服务器的ip地址
- client.connect("192.168.20.21");
- //登录的用户名和密码
- client.login("admin", "admin");
- System.out.println("login ftp ok ...");
- //查看当前目录
- String workingDirectory = client.printWorkingDirectory();
- System.out.println(workingDirectory);
- //获取指定目录下的文件及目录
- FTPListParseEngine engine = client.initiateListParsing("/dir1/zenoss-2.4.1-x86.vmware.zip");
- while(engine.hasNext()){
- FTPFile[] files = engine.getNext(5);
- for(int i=0;i<files.length;i++){
- //获取文件名
- System.out.println(files[i].getName());
- //获取文件大小
- long size = files[i].getSize();
- System.out.println(size/1024+"kb");
- }
- }
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }