1.需要的包
- org.apache.commons.net.ftp.FTPClient;
2.介绍
- 此处使用到了递归的算法,然后去执行下载整个文件夹或者下载单个文件
- 程序自动递归下载指定目录下的所有文件及子文件夹,上传时,自动递归创建服务器目录
- 此处执行的代码逻辑是先下载图片和xls文件,其他类型的文件不予下载,然后再上传到另一个服务器上,然后再把本地的缓存路径删除!
3. 代码
public static void main(String[] args) {
String sysDate = "2021-02-23";
FTPClient uploadClient = null;
FTPClient downClient = null;
try {
downClient = FtpUtil.connectFtpServer("39.100.247.387", 21, "upolad", "srdJ5RaWzJbjtRpw",0);
String Dir = "/bhpsftp/e_photo/real-xiaobai/2021-02-23";
String[] types = {".jpg",".xls",".xlsx"};
String local = "D:\\temp\\real-xiaobai\\2021-02-23";
FtpUtil.doDownload(downClient, Dir, local, types);
uploadClient = FtpUtil.connectFtpServer("39.100.247.387", 21, "upolad", "srdJ5RaWzJbjtRpw",0);
String uploadDir = "/bhpsftp/e_photo/real-xiaobai/2021-02-23";
File localFile = new File("D:\\temp\\real-xiaobai\\2021-02-23");
boolean makeFtpDirs = FtpUtil.makeFtpDirs(uploadClient, uploadDir);
System.out.println(makeFtpDirs);
System.out.println(uploadDir);
FtpUtil.doUpload(uploadClient, uploadDir, localFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
FtpUtil.disconnectFtpServer(uploadClient);
FtpUtil.delete(new File("D:\\temp\\/real-xinzhuang/2021-02-23"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Logger log = LoggerFactory.getLogger(FtpUtil.class);
public static FTPClient connectFtpServer(String host, int port,
String username, String password) throws Exception {
return connectFtpServer(host, port, username, password, 1);
}
public static FTPClient connectFtpServer(String host, int port,
String username, String password, int mode) throws Exception {
log.info("ftp连接信息:ip[" + host + "],port[" + port + "],ftpUser["
+ username + "],ftpPasswd[" + password + "]");
FTPClient ftpClient = null;
ftpClient = new FTPClient();
ftpClient.connect(host, port);
ftpClient.setControlEncoding("GBK");
if (ftpClient.login(username, password)) {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
log.error("ftp连接失败[服务器拒绝连接]");
return null;
} else {
if (mode == 0) {
ftpClient.enterLocalActiveMode();
} else {
ftpClient.enterLocalPassiveMode();
}
}
} else {
log.error("ftp连接失败[登录失败]");
return null;
}
return ftpClient;
}
public static boolean isConnected(FTPClient ftpClient) {
if (ftpClient.isConnected()) {
return true;
}
return false;
}
public static void disconnectFtpServer(FTPClient ftpClient)
throws Exception {
if (ftpClient != null) {
ftpClient.logout();
ftpClient.disconnect();
}
}
public static boolean isDirectory(FTPClient ftpClient, String path)
throws IOException {
if (path.endsWith(".")) {
return false;
}
return ftpClient.changeWorkingDirectory(new String(path.getBytes(),
"ISO-8859-1"));
}
public static void makeLocalDirs(String path) {
File localFile = new File(path);
if (!localFile.exists()) {
localFile.mkdirs();
}
}
public static String hasLocalFile(String path) {
File localFile = new File(path);
return localFile.exists() ? "1" : "0";
}
public static boolean downloadFile(FTPClient ftpClient, String dir,
String localPath) {
boolean boo = false;
try {
if (!dir.endsWith(".")) {
File file = new File(localPath);
OutputStream os = new FileOutputStream(file);
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
boo = ftpClient.retrieveFile(new String(dir.getBytes(),
"ISO-8859-1"), os);
os.flush();
os.close();
}
return boo;
} catch (Exception e) {
e.printStackTrace();
return boo;
}
}
public static void doDownload(FTPClient ftpClient, String path,
String localPath, String[] types) throws IOException {
if (!path.endsWith(".") && types != null && types.length > 0) {
makeLocalDirs(localPath);
ftpClient.changeWorkingDirectory(new String(path.getBytes(),
"ISO-8859-1"));
String[] fileNames = ftpClient.listNames();
for (String fname : fileNames) {
if (isDirectory(ftpClient, path + "/" + fname)) {
doDownload(ftpClient, path + "/" + fname, localPath + "/"
+ fname, types);
} else {
for (String type : types) {
if (fname.endsWith(type)) {
downloadFile(ftpClient, path + "/" + fname,
localPath + "/" + fname);
}
}
}
}
}
}
public static boolean makeFtpDirs(FTPClient ftpClient, String path)
throws IOException {
if (!ftpClient.changeWorkingDirectory(path)) {
return ftpClient.makeDirectory(path);
}
return false;
}
public static void doUpload(FTPClient ftpClient, String path, File localFile)
throws IOException {
if (localFile.isDirectory()) {
for (File ff : localFile.listFiles()) {
if (ff.isDirectory()) {
ftpClient.changeWorkingDirectory(path);
if (!ftpClient.changeWorkingDirectory(path + "/"
+ ff.getName())) {
ftpClient.makeDirectory(ff.getName());
}
doUpload(ftpClient, path + "/" + ff.getName(), ff);
} else {
uploadFile(ftpClient, path, ff);
}
}
} else {
uploadFile(ftpClient, path, localFile);
}
}
private static boolean uploadFile(FTPClient ftp, String remotePath,
File localFile){
InputStream input = null;
try {
ftp.changeWorkingDirectory(remotePath);
input = new FileInputStream(localFile);
return ftp.storeFile(localFile.getName(),input);
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if(input != null){
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void delete(File dirFile) {
if (dirFile != null && dirFile.exists()) {
if(dirFile.isDirectory()){
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
delete(files[i]);
}else{
files[i].delete();
}
}
}
dirFile.delete();
}
}