java整合ftp 一些操作

git  地址:https://gitee.com/work123gs/ftp-demo.git

 

其中一个类:

package com.xiangshu.puls.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
* @author
*/
@Slf4j
public class DownLoadFtp {

private FTPClient ftp;

public FTPClient getFtp() {
return ftp;
}

public void setFtp(FTPClient ftp) {
this.ftp = ftp;
}

public DownLoadFtp() {
this.ftp = new FTPClient();
}

/**
* 下载FTP下指定文件
*
* @param filePath FTP文件路径
* @param downPath 下载保存的目录
*/
public boolean downLoad(String filePath, String downPath) {

// 默认失败
boolean flag = false;
//下载文件夹不存在,则创建文件夹
File downfolder = new File(downPath);
if (!downfolder.exists()) {
downfolder.mkdirs();
}
try {
// 跳转到文件目录
ftp.changeWorkingDirectory(filePath);
// 获取目录下文件集合
ftp.enterLocalPassiveMode();

FTPFile[] files = ftp.listFiles();

for (FTPFile file : files) {
// 取得指定文件并下载
String fileName = file.getName();
//判断文件是不是.ZIP格式
if (fileName.endsWith(".zip")) {
File downFile = new File(downPath + File.separator + file.getName());

OutputStream out = new FileOutputStream(downFile);
// 绑定输出流下载文件,需要设置编码集,不然可能出现文件为空的情况
flag = ftp.retrieveFile(new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1), out);
//超时时间5000毫秒
ftp.setSoTimeout(50000);

out.flush();
out.close();
if (flag) {
System.out.println("成功");
}
}
}

} catch (Exception e) {
System.out.println("失败");
}

return flag;
}

/*
* 连接到ftp服务器的方法
* */
public void connectServer(String hostip, Integer port, String username, String password) {
try {
ftp.connect(hostip, port);
// 连接服务器
ftp.login(username, password);
//超时时间5000毫秒
ftp.setConnectTimeout(50000);
System.out.println("连接成功1");
// 登录
// 检测是否连接成功
int reply = ftp.getReplyCode();
// 看返回的值是不是230,如果是,表示登陆成功
if (!FTPReply.isPositiveCompletion(reply)) {
System.out.println("连接成功2");
// 返回的code>=200&&code<300return
ftp.disconnect();
// 关闭FTP连接
}
ftp.setControlEncoding("UTF-8");
// 设置字符编码
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置文件传输格式

} catch (Exception e) {
e.printStackTrace();

}

}

/**
* 删除FTP上指定文件夹下文件及其子文件方法,添加了对中文目录的支持
*
* @param ftpFolder 需要删除的文件夹
*/
public boolean deleteByFolder(String ftpFolder) {

boolean flag = false;
try {
ftp.changeWorkingDirectory(new String(ftpFolder.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles();
for (FTPFile file : files) {
//判断为文件则删除
if (file.isFile()) {
ftp.deleteFile(new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}
ftp.removeDirectory(new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
}

files = ftp.listFiles();
if (files.length == 0) {
flag = true;
}
} catch (
Exception e) {
e.printStackTrace();
log.info("删除失败");
}
return flag;
}
}
posted @ 2022-03-22 00:10  老油条666  阅读(216)  评论(0编辑  收藏  举报