Java第十一篇:FTPClient将指定目录下的文件批量复制到另一个目录下

FtpClient的复制功能实现

/**
* 将指定文件目录下的多个文件复制到另一个指定文件中来
* @param fileNames 要复制的文件名
* @param fromPath 从哪个文件目录中复制
* @param toPath 复制到哪个目录
* @return true-复制成功,false-复制失败
* @throws IOException
*/
public boolean batchCopyFileToPath(List<String> fileNames, String fromPath, String toPath) throws IOException{
	//如果toPath不存在,则创建该目录
	if(!mkDir("",toPath)){
		logger.error("创建"+ toPath+"失败");
		return false;
	}
	if(!this.ftpClient.isConnected()){
		return false;
	}
	for(String fileName : fileNames){
		//设置当前处理文件夹的层级
		this.ftpClient.changeToParentDirectory();
		this.ftpClient.changeToParentDirectory();
		this.ftpClient.setControlEncoding("UTF-8");
		this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
		this.ftpClient.setBufferSize(1024);
		this.ftpClient.enterLocalPassiveMode();
		Inputstream ins = null;
		if(this.ftpClient.changeWorkingDirectory(formatPath(fromPath))){
			ins = this.ftpClient.retrieveFileStream(fileName);
			//使用retrieveFileStream必须调用getReply(),避免下次为空
			this.ftpClient.getReply();
		}
		//返回正确的目录
		this.ftpClient.changeToParentDirectory();
		this.ftpClient.changeToParentDirectory();
		if(this.ftpClient.changeWorkingDirectory(formatPath(fromPath))){
			if(!this.ftpClient.storeFile(fileName,ins)){
				return false;
			}
		}
		if(ins != null){
			ins.close();
		}
	}
}

private String formatPath(String filePath){
	if(StringUtils.isBlank(filePath)){
		filePath = "~/";
	}else{
		filePath = "~/" + filePath;
	}
	if(!filePath.endWith("/")){
		filePath += "/";
	}
	return filePath;
}
使用Java语言实现FTPClient删除远程服务器上的文件目录,可以按照以下步骤进行操作: 1. 创建FTPClient对象,并连接到FTP服务器。 2. 使用FTPClient的login()方法登录FTP服务器。 3. 使用FTPClient的changeWorkingDirectory()方法切换到要删除的目录。 4. 使用FTPClient的deleteFile()方法删除指定文件。 5. 使用FTPClient的removeDirectory()方法删除指定目录。 6. 关闭FTP连接。 以下是Java代码示例: ``` import org.apache.commons.net.ftp.FTPClient; import java.io.IOException; public class FTPDeleteExample { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String user = "username"; String pass = "password"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(user, pass); String directoryPath = "/example/directory"; if (ftpClient.changeWorkingDirectory(directoryPath)) { String fileName = "example.txt"; boolean deleted = ftpClient.deleteFile(fileName); if (deleted) { System.out.println("File " + fileName + " deleted successfully."); } else { System.out.println("Failed to delete file " + fileName + "."); } } else { System.out.println("Directory " + directoryPath + " not found."); } String emptyDirectoryPath = "/empty/directory"; if (ftpClient.changeWorkingDirectory(emptyDirectoryPath)) { boolean removed = ftpClient.removeDirectory(emptyDirectoryPath); if (removed) { System.out.println("Directory " + emptyDirectoryPath + " removed successfully."); } else { System.out.println("Failed to remove directory " + emptyDirectoryPath + "."); } } else { System.out.println("Directory " + emptyDirectoryPath + " not found."); } ftpClient.logout(); } catch (IOException ex) { System.err.println("Error: " + ex.getMessage()); } finally { try { ftpClient.disconnect(); } catch (IOException ex) { System.err.println("Error: " + ex.getMessage()); } } } } ``` 在上面的代码中,使用FTPClient连接到FTP服务器,然后使用changeWorkingDirectory()方法切换到要删除的目录,并使用deleteFile()方法删除指定文件,或使用removeDirectory()方法删除指定目录。最后,关闭FTP连接。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值