edtFTPj提供了一个简易的构建FTP客户端的方法集合。现在的需求是遍历FTP服务器上的文件,获取文件信息,主要是文件名和文件大小。
一般的想法是使用递归方法遍历。因为文件系统是类似普通树的结构,存在自相似性。不过这样的潜在问题是占用太多内存,每个函数压栈,保存了许多用不到的信息——对于每次函数获取的信息来说,只需要得到文件夹的名字(和当前目录字符串合并可以得到完整的路径),而不需要获得其他信息,所以决定使用stack和循环来模拟这个递归过程。
stack貌似java.util.stack这个类,不过貌似其超类vector名声不怎么好(可能是占用系统空间比较大等原因),java.util.stack成为了一个遗留类。所以需要重新写一个用array或者linkedlist来实现的stack类。
下面采用linkedlist类来实现stack,(模仿stack中的代码写的)。
使用这个stack来保存的主要是压栈的工作目录,其基本工作思路如下:
[list]
[*]获取当前目录下的所有目录,当前目录下的文件全部输出
[*]如果栈非空,切换当前目录到出栈的目录,继续上面的过程
[/list]
代码如下,具体测试FTP服务器和用户名密码等信息没有列出。
至此,可以遍历得到FTP下面的目录列表。
需要修改的地方:
[list]
[*]保存到文件
[*]多一个线程察看进度
[/list]
等
实际调用代码如下:
记得捕捉错误
一般的想法是使用递归方法遍历。因为文件系统是类似普通树的结构,存在自相似性。不过这样的潜在问题是占用太多内存,每个函数压栈,保存了许多用不到的信息——对于每次函数获取的信息来说,只需要得到文件夹的名字(和当前目录字符串合并可以得到完整的路径),而不需要获得其他信息,所以决定使用stack和循环来模拟这个递归过程。
stack貌似java.util.stack这个类,不过貌似其超类vector名声不怎么好(可能是占用系统空间比较大等原因),java.util.stack成为了一个遗留类。所以需要重新写一个用array或者linkedlist来实现的stack类。
下面采用linkedlist类来实现stack,(模仿stack中的代码写的)。
package com.hako.ftp;
import java.util.LinkedList;
import java.util.EmptyStackException;
/**
*
* @author XnnYygn
*
* @param <E>
*/
public class ListStack<E> extends LinkedList<E> {
/**
* create empty stack
*/
public ListStack() {
}
/**
* Tests if this stack is empty
*
* @return <code>true</code> if and only if this stack contains no items;
* <code>false</code> otherwise.
*/
public boolean empty() {
return size() == 0;
}
/**
* append item to the stack
*
* @param item
* E
*/
public void push(E item) {
add(item);
}
/**
* return the item from the stack and remove it
*
* @return the item
*/
public E pop() {
E obj = null;
obj = peek();
removeLast();
return obj;
}
/**
* return the item from the stack without removing it
*
* @return the item
*/
public E peek() {
if (size() > 0)
return getLast();
else
throw new EmptyStackException();
}
/**
* search the item in the stack
*
* @param o
* @return the index of item
*/
public int search(Object o) {
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
/** the generated serialVersionUID */
private static final long serialVersionUID = -1560355629647329776L;
}
使用这个stack来保存的主要是压栈的工作目录,其基本工作思路如下:
[list]
[*]获取当前目录下的所有目录,当前目录下的文件全部输出
[*]如果栈非空,切换当前目录到出栈的目录,继续上面的过程
[/list]
代码如下,具体测试FTP服务器和用户名密码等信息没有列出。
package com.hako.ftp;
import java.io.IOException;
import java.text.ParseException;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FileTransferClient;
public class FtpList {
// the ftp
private FileTransferClient ftp = null;
// the stack of task
private ListStack<String> stack = null;
// the count of item
private Integer count = 0;
public FtpList() throws FTPException, IOException, ParseException {
// prepare
ftp = new FileTransferClient();
ftp.setRemoteHost("");//host
ftp.setUserName("");//username
ftp.setPassword("");//password
ftp.getAdvancedSettings().setControlEncoding("GBK");
ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);
}// end of HakoFtpList
public void connect() throws IOException, FTPException {
// connect
ftp.connect();
System.out
.println("Connect successfully!");
}// end of Connect
public void disconnect() throws IOException, FTPException {
// disconnect
if (ftp.isConnected()) {
ftp.disconnect();
System.out
.println("Disconnect successfully");
}
}// end of Disconnect
public void getAllFileList() throws FTPException, IOException,
ParseException {
// get file from root
getAllFileList("");
}// end of getAllFileList
public void getAllFileList(String rootPath) throws FTPException,
IOException, ParseException {
// init stack and count
stack = new ListStack<String>();
count = 0;
// get file list under root
getFileList(rootPath);
// with stack
while (!stack.isEmpty()) {
getFileList(stack.pop());
}
}// end of getAllFileList(rootPath)
private void getFileList(String path) throws FTPException, IOException,
ParseException {
// check the path
if (path == null) {
return;
} else if (path == "") {
// get directory for ftp
ftp.changeDirectory("/");
} else {
// get directory for test
ftp.changeDirectory(path);
}
// get file list
FTPFile[] files = ftp.directoryList();
for (FTPFile file : files) {
if (file.isDir()) {
// System.out.println(path + "/" + file.getName());
stack.push(path + "/" + file.getName());
} else {
System.out.println(path + "/" + file.getName());
count++;
// System.out.println("Size " + file.size());
}
}
}// end of getFileList
}
至此,可以遍历得到FTP下面的目录列表。
需要修改的地方:
[list]
[*]保存到文件
[*]多一个线程察看进度
[/list]
等
实际调用代码如下:
FtpList ftplist= new FtpList();
ftplist.connect();
ftplist.getAllFileList();
ftplist.disconnect();
记得捕捉错误