一个java FTP的客户端
使用的了apache的commons-net实现批量下载,上传,删除等功能:java 代码
[*]import java.io.File;
[*]import java.io.FileInputStream;
[*]import java.io.FileNotFoundException;
[*]import java.io.FileOutputStream;
[*]import java.io.IOException;
[*]import java.net.SocketException;
[*]import java.util.List;
[*]import java.util.Properties;
[*]
[*]import org.apache.commons.net.ftp.FTPClient;
[*]import org.apache.commons.net.ftp.FTPFile;
[*]import org.apache.commons.net.ftp.FTPListParseEngine;
[*]
[*]public class FtpClientHandler {
[*]
[*] private FTPClient f = new FTPClient();
[*]
[*] private String host = "10.168.211.37";
[*]
[*] private String username = "test";
[*]
[*] private String password = "test";
[*]
[*] private String r_workDirector = "./";
[*]
[*] private String l_workDirector = "../";
[*]
[*] private String config_file = "../config/boss-config.properties";
[*]
[*] private String remote_files = "*";
[*]
[*] private String local_files = "*";
[*]
[*] public FtpClientHandler() throws IOException {
[*] super();
[*] init();
[*] }
[*]
[*] public void init() throws IOException {
[*] FileInputStream fis = new FileInputStream(config_file);
[*] Properties config = new Properties();
[*] config.load(fis);
[*] host = config.getProperty("Boss_FTP_Url");
[*] username = config.getProperty("Username");
[*] password = config.getProperty("Password");
[*] r_workDirector = config.getProperty("Remote_workdirector");
[*] l_workDirector = config.getProperty("Local_workdirector");
[*]
[*] remote_files = config.getProperty("Remote_download_files");
[*] local_files = config.getProperty("Local_upload_files");
[*]
[*] }
[*]
[*] private void connection() throws SocketException, IOException {
[*] f.connect(host);
[*] f.login(username,
[*] password);
[*] f.changeWorkingDirectory(r_workDirector);
[*] }
[*]
[*] private void close() throws IOException {
[*] if (f != null)
[*] f.disconnect();
[*] }
[*]
[*] public void DeleteFiles() throws IOException {
[*] connection();
[*] FTPListParseEngine engine = f.initiateListParsing(remote_files);
[*] FTPFile[] files = engine.getFiles();
[*] for (int i = 0; i < files.length; i++) {
[*] System.out.print(i + ">>");
[*] System.out.println(files.getRawListing());
[*] f.dele(files.getName());
[*] }
[*] close();
[*] }
[*]
[*] public void DownloadFiles() throws FileNotFoundException, IOException {
[*] connection();
[*] FTPListParseEngine engine = f.initiateListParsing(remote_files);
[*] FTPFile[] files = engine.getFiles();
[*] for (int i = 0; i < files.length; i++) {
[*] System.out.print(i + ">>");
[*] System.out.println(files.getRawListing());
[*] f.retrieveFile(files.getName(),
[*] new FileOutputStream(new File(l_workDirector
[*] + files.getName())));
[*] }
[*] close();
[*] }
[*]
[*] public void StorFiles() throws FileNotFoundException, IOException {
[*] connection();
[*] File file = new File(l_workDirector);// 在此目录中找文件
[*]
[*] BFSFileSearch search = new BFSFileSearch();
[*] List fileList = search.scanFiles(file,
[*] local_files);
[*] for (int i = 0; i < fileList.size(); i++) {
[*] String fileName = (String) fileList.get(i);
[*] f.storeFile(fileName,
[*] new FileInputStream(new File(l_workDirector + fileName)));
[*] }
[*] close();
[*] }
[*]}
其中利用深度搜索机制来对*,?等通配符的支持:
java 代码
[*]import java.io.File;
[*]import java.util.ArrayList;
[*]import java.util.List;
[*]import java.util.Vector;
[*]
[*]public class BFSFileSearch {
[*] /**
[*] * @param fileName
[*] * String 需要查找的文件,可含通配符如*.java
[*] * @param base
[*] * File String 欲查找的文件夹
[*] * @param ArrayList
[*] * fileList 结果集
[*] * @param count
[*] * int 控制结果数量,为0,表示返回所有的匹配的文件
[*] */
[*] public void scanFiles(File base, String fileName, List fileList, int count) {
[*] Queue queue = new Queue();// 实例化队列
[*] queue.put(base);// 入队
[*] while (!queue.isEmpty()) {
[*] File f = (File) queue.get();// 出队列
[*] if (f.exists() && f.isDirectory()) {
[*] String[] files = f.list();
[*] for (int i = 0; i < files.length; i++) {
[*] File f2 = new File(f,
[*] files);
[*] if (f2.isDirectory()) {// 文件夹则进队列
[*] queue.put(f2);
[*] } else {// 文件则进行匹配
[*] String filePath = f2.getAbsolutePath();
[*] filePath = filePath.substring(filePath.lastIndexOf("/") + 1);// 提取文件名
[*] if (wildcardMatch(fileName,
[*] filePath)) {// 匹配成功
[*] if (count != 0 && fileList.size() >= count) {
[*] return;
[*] }
[*] fileList.add(filePath);// 添加到结果集
[*] }
[*] }
[*] }
[*] }
[*] }
[*] }
[*]
[*] /**
[*] * overwrite method scanFiles
[*] * @param base
[*] * @param fileName
[*] * @return
[*] */
[*] public List scanFiles(File base, String fileName) {
[*] Queue queue = new Queue();// 实例化队列
[*] queue.put(base);// 入队
[*] List fileList = new ArrayList();
[*] while (!queue.isEmpty()) {
[*] File f = (File) queue.get();// 出队列
[*] if (f.exists() && f.isDirectory()) {
[*] String[] files = f.list();
[*] for (int i = 0; i < files.length; i++) {
[*] File f2 = new File(f,
[*] files);
[*] if (f2.isDirectory()) {// 文件夹则进队列
[*] queue.put(f2);
[*] } else {// 文件则进行匹配
[*] String filePath = f2.getAbsolutePath();
[*] filePath = filePath.substring(filePath.lastIndexOf("/") + 1);// 提取文件名
[*] if (wildcardMatch(fileName,
[*] filePath)) {// 匹配成功
[*] fileList.add(filePath);// 添加到结果集
[*] }
[*] }
[*] }
[*] }
[*] }
[*] return fileList;
[*] }
[*]
[*] public boolean wildcardMatch(String pattern, String string) {
[*] int stringLength = string.length();
[*] int stringIndex = 0;
[*] for (int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex) {
[*] char c = pattern.charAt(patternIndex);
[*]
[*] if (c == '*') {
[*] while (stringIndex < stringLength) {
[*] if (wildcardMatch(pattern.substring(patternIndex + 1),
[*] string.substring(stringIndex))) {
[*] return true;
[*] }
[*] ++stringIndex;
[*] }// end of while
[*]
[*] } else if (c == '?') {
[*] ++stringIndex;
[*] if (stringIndex > stringLength) {
[*] return false;
[*] }
[*]
[*] } else {
[*] if (stringIndex >= stringLength
[*] || c != string.charAt(stringIndex)) {
[*] return false;
[*] }
[*] ++stringIndex;
[*] }
[*] }
[*] return stringIndex == stringLength;
[*] }
[*]
[*] class Queue {// 先进先出的队列
[*] private Vector vector = new Vector();
[*]
[*] public void put(Object object) {// 入队
[*] vector.addElement(object);
[*] }
[*]
[*] public Object get() {// 出队
[*] Object object = peek();
[*] if (object != null) {
[*] vector.removeElementAt(0);
[*] }
[*] return object;
[*] }
[*]
[*] public Object peek() {// 取队列首元素
[*] if (isEmpty()) {
[*] return null;
[*] }
[*] return vector.elementAt(0);
[*] }
[*]
[*] public boolean isEmpty() {// 队列是否为空
[*] return vector.isEmpty();
[*] }
[*]
[*] public int size() {// 队列的大小
[*] return vector.size();
[*] }
[*]
[*] }
[*]}
页:
[1]