栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

FtpClient的复制功能实现
public boolean batchCopyFileToPath(List 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号