sun.net.ftp.FtpClient

傅正豪
2023-12-01
package classTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FtpOperation {
	private FtpClient client = new FtpClient();
	
	/**
	 * 登录FTP,并返回登录是否成功的Boolean值
	 * @param host
	 * @param port
	 * @param user
	 * @param password
	 * @return
	 */
	public boolean login(String host, int port, String user, String password) {
		boolean flag = true;
		try {
			client.openServer(host, port);
			client.login(user, password);
		} catch (IOException e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	/**
	 * 关闭FTP连接
	 */
	public void close() {
		if(client.serverIsOpen()) {
			try {
				client.closeServer();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 在父级节点创建子节点目录,如果父级节点为根节点parent可以为空或“/”
	 * @param parent
	 * @param path
	 * @return
	 */
	public boolean makeDir(String parent, String path) {
		boolean flag = true;
		flag = cdAssignPath(parent);
		if(flag) {
			try {
				//创建已经存在的目录会报错
				client.sendServer("MKD " + path + "\r\n");
			}catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}
	
	/**
	 * 上传文件
	 * @param path保存FTP位置
	 * @param file要上传的文件
	 * @param remoteName在FTP保存时的名字
	 */
	public void upload(String path, File file, String remoteName) {
		TelnetOutputStream write = null;
		FileInputStream read = null;
		byte[] by = new byte[1024];
		try {
			if(cdAssignPath(path)) {
				read = new FileInputStream(file);
				write = client.put(remoteName);
				while(read.read(by) != -1) {
					write.write(by);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (read != null) {
				try {
					read.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (write != null) {
				try {
					write.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 下载文件
	 * @param remotePath
	 * @param remoteName
	 * @param localPath
	 * @param localName
	 */
	public void download(String remotePath, String remoteName, String localPath, String localName) {
		byte[] by = new byte[1024];
		if(cdAssignPath(remotePath)) {
			try {
				TelnetInputStream read = client.get(remoteName);
				File file = new File(localPath);
				if(!file.exists()) {
					file.mkdirs();
				}
				FileOutputStream write = new FileOutputStream(new File(localPath + "/" + localName));
				while(read.read(by) > 0) {
					write.write(by);
				}
				read.close();
				write.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获取指定路径下的文件列表
	 * @param path
	 * @return
	 */
	public List<String> ls(String path) {
		List<String> list = new ArrayList<String>();
		if(cdAssignPath(path)) {
			String line = "";
			BufferedReader ls;
			try {
				ls = new BufferedReader(new InputStreamReader(client.list()));
				while((line = ls.readLine()) != null) {
					//文件列表显示格式:-rw-r--r-- 1 ftp ftp              4 Jul 02 17:23 zqz.txt
					String[] array = line.split(" ");
					list.add(array[array.length -1]);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println(list);
		return list;
	}
	/**
	 * 切换当前目录当根目录
	 */
	private void cdRoot() {
		try {
			while(!"/".equals(client.pwd())) {
				client.cdUp();
			}
			System.out.println(client.pwd());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 切换当前目录到指定路径,该路径必须从根路径开始
	 * @param path
	 * @return
	 */
	public boolean cdAssignPath(String path) {
		boolean flag = true;
		cdRoot();
		try {
			client.cd(path);
			System.out.println(client.pwd());
		} catch (IOException e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
}

 

 类似资料:

相关阅读

相关文章

相关问答