FtpClient的复制功能实现
public boolean batchCopyFileToPath(List<String> fileNames, String fromPath, String toPath) throws IOException{
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);
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;
}