Package org.miaixz.bus.extra.ftp
Class CommonsFtp
java.lang.Object
org.miaixz.bus.extra.ftp.AbstractFtp
org.miaixz.bus.extra.ftp.CommonsFtp
- All Implemented Interfaces:
Closeable,AutoCloseable,Ftp
Apache Commons FTP客户端封装
此客户端基于Apache-Commons-Net
常见搭建ftp的工具有:
- filezila server ;根目录一般都是空
- linux vsftpd ; 使用的 系统用户的目录,这里往往都是不是根目录,如:/home/bus/ftp
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
FieldsFields inherited from class org.miaixz.bus.extra.ftp.AbstractFtp
ftpConfigFields inherited from interface org.miaixz.bus.extra.ftp.Ftp
DEFAULT_CHARSET -
Constructor Summary
ConstructorsConstructorDescriptionCommonsFtp(org.apache.commons.net.ftp.FTPClient client) 构造CommonsFtp(FtpConfig config, org.miaixz.bus.core.lang.EnumMap.FtpMode mode) 构造 -
Method Summary
Modifier and TypeMethodDescriptionboolean改变目录voidclose()boolean删除文件夹及其文件夹下的所有文件boolean删除指定目录下的指定文件void下载文件void下载文件voiddownload(String path, String fileName, OutputStream out) 下载文件到输出流voiddownload(String path, String fileName, OutputStream out, Charset fileNameCharset) 下载文件到输出流boolean判断ftp服务器目录内是否还有子元素(目录或文件)org.apache.commons.net.ftp.FTPClient获取FTPClient客户端对象getFileStream(String path) 读取FTP服务器上的文件为输入流getFileStream(String dir, String fileName) 读取文件为输入流init()初始化连接初始化连接boolean是否执行完操作返回当前目录遍历某个目录下所有文件和目录,不会递归遍历遍历某个目录下所有文件和目录,不会递归遍历 此方法自动过滤"."和".."两种目录org.apache.commons.net.ftp.FTPFile[]遍历某个目录下所有文件和目录,不会递归遍历List<org.apache.commons.net.ftp.FTPFile> 遍历某个目录下所有文件和目录,不会递归遍历 此方法自动过滤"."和".."两种目录boolean在当前远程目录(工作目录)下创建新的目录static CommonsFtp构造CommonsFtp,匿名登录static CommonsFtp构造,匿名登录static CommonsFtp构造static CommonsFtp构造static CommonsFtp构造static CommonsFtpof(Connector connector, Charset charset, String serverLanguageCode, String systemKey, org.miaixz.bus.core.lang.EnumMap.FtpMode mode) 构造pwd()远程当前目录如果连接超时的话,重新进行连接 经测试,当连接超时时,client.isConnected()仍然返回ture,无法判断是否连接超时 因此,通过发送pwd命令的方式,检查连接是否超时voidrecursiveDownloadFolder(String sourcePath, File destDir) 递归下载FTP服务器上文件到本地(文件目录和服务器同步)setBackToPwd(boolean backToPwd) 设置执行完操作是否返回当前目录setMode(org.miaixz.bus.core.lang.EnumMap.FtpMode mode) 设置FTP连接模式,可选主动和被动模式int获取服务端目录状态。void递归上传文件(支持目录) 上传时,如果uploadFile为目录,只复制目录下所有目录和文件到目标路径下,并不会复制目录本身 上传时,自动创建父级目录booleanuploadFile(String remotePath, File file) 上传文件到指定目录,可选:booleanuploadFile(String remotePath, String fileName, File file) 上传文件到指定目录,可选:booleanuploadFile(String remotePath, String fileName, InputStream fileStream) 上传文件到指定目录,可选:Methods inherited from class org.miaixz.bus.extra.ftp.AbstractFtp
download, exist, getConfig, mkDirs
-
Field Details
-
DEFAULT_PORT
public static final int DEFAULT_PORT默认端口- See Also:
-
-
Constructor Details
-
CommonsFtp
构造- Parameters:
config- FTP配置mode- 模式
-
CommonsFtp
public CommonsFtp(org.apache.commons.net.ftp.FTPClient client) 构造- Parameters:
client- 自定义实例化好的FTPClient
-
-
Method Details
-
of
构造CommonsFtp,匿名登录- Parameters:
host- 域名或IP- Returns:
- CommonsFtp
-
of
构造,匿名登录- Parameters:
host- 域名或IPport- 端口- Returns:
- CommonsFtp
-
of
构造- Parameters:
host- 域名或IPport- 端口user- 用户名password- 密码- Returns:
- CommonsFtp
-
of
构造- Parameters:
connector- 连接信息,包括host、port、user、password等信息charset- 编码- Returns:
- CommonsFtp
-
of
public static CommonsFtp of(Connector connector, Charset charset, String serverLanguageCode, String systemKey) 构造- Parameters:
connector- 连接信息,包括host、port、user、password等信息charset- 编码serverLanguageCode- 服务器语言 例如:zhsystemKey- 服务器标识 例如:org.apache.commons.net.ftp.FTPClientConfig.SYST_NT- Returns:
- CommonsFtp
-
of
public static CommonsFtp of(Connector connector, Charset charset, String serverLanguageCode, String systemKey, org.miaixz.bus.core.lang.EnumMap.FtpMode mode) 构造- Parameters:
connector- 连接信息,包括host、port、user、password等信息charset- 编码serverLanguageCode- 服务器语言systemKey- 系统关键字mode- 模式- Returns:
- CommonsFtp
-
init
初始化连接- Returns:
- this
-
init
初始化连接- Parameters:
config- FTP配置mode- 模式- Returns:
- this
-
setMode
设置FTP连接模式,可选主动和被动模式- Parameters:
mode- 模式枚举- Returns:
- this
-
isBackToPwd
public boolean isBackToPwd()是否执行完操作返回当前目录- Returns:
- 执行完操作是否返回当前目录
-
setBackToPwd
设置执行完操作是否返回当前目录- Parameters:
backToPwd- 执行完操作是否返回当前目录- Returns:
- this
-
reconnectIfTimeout
如果连接超时的话,重新进行连接 经测试,当连接超时时,client.isConnected()仍然返回ture,无法判断是否连接超时 因此,通过发送pwd命令的方式,检查连接是否超时- Returns:
- this
-
cd
改变目录- Parameters:
directory- 目录- Returns:
- 是否成功
-
pwd
远程当前目录- Returns:
- 远程当前目录
-
ls
Description copied from interface:Ftp遍历某个目录下所有文件和目录,不会递归遍历- Parameters:
path- 需要遍历的目录- Returns:
- 文件和目录列表
-
ls
遍历某个目录下所有文件和目录,不会递归遍历 此方法自动过滤"."和".."两种目录- Parameters:
path- 目录predicate- 过滤器,null表示不过滤,默认去掉"."和".."两种目录- Returns:
- 文件名或目录名列表
-
lsFiles
public List<org.apache.commons.net.ftp.FTPFile> lsFiles(String path, Predicate<org.apache.commons.net.ftp.FTPFile> predicate) 遍历某个目录下所有文件和目录,不会递归遍历 此方法自动过滤"."和".."两种目录- Parameters:
path- 目录predicate- 过滤器,null表示不过滤,默认去掉"."和".."两种目录- Returns:
- 文件或目录列表
-
lsFiles
public org.apache.commons.net.ftp.FTPFile[] lsFiles(String path) throws org.miaixz.bus.core.lang.exception.InternalException 遍历某个目录下所有文件和目录,不会递归遍历- Parameters:
path- 目录,如果目录不存在,抛出异常- Returns:
- 文件或目录列表
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
mkdir
Description copied from interface:Ftp在当前远程目录(工作目录)下创建新的目录- Parameters:
dir- 目录名- Returns:
- 是否创建成功
- Throws:
org.miaixz.bus.core.lang.exception.InternalException
-
stat
获取服务端目录状态。- Parameters:
path- 路径- Returns:
- 状态int,服务端不同,返回不同
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
existFile
判断ftp服务器目录内是否还有子元素(目录或文件)- Parameters:
path- 文件路径- Returns:
- 是否存在
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
delFile
Description copied from interface:Ftp删除指定目录下的指定文件- Parameters:
path- 目录路径- Returns:
- 是否存在
- Throws:
org.miaixz.bus.core.lang.exception.InternalException
-
delDir
Description copied from interface:Ftp删除文件夹及其文件夹下的所有文件- Parameters:
dirPath- 文件夹路径- Returns:
- boolean 是否删除成功
- Throws:
org.miaixz.bus.core.lang.exception.InternalException
-
uploadFile
上传文件到指定目录,可选:1. remotePath为null或""上传到当前路径 2. remotePath为相对路径则相对于当前路径的子路径 3. remotePath为绝对路径则上传到此路径
- Parameters:
remotePath- 服务端路径,可以为null或者相对路径或绝对路径file- 文件- Returns:
- 是否上传成功
-
uploadFile
public boolean uploadFile(String remotePath, String fileName, File file) throws org.miaixz.bus.core.lang.exception.InternalException 上传文件到指定目录,可选:1. remotePath为null或""上传到当前路径 2. remotePath为相对路径则相对于当前路径的子路径 3. remotePath为绝对路径则上传到此路径
- Parameters:
remotePath- 服务端路径,可以为null或者相对路径或绝对路径fileName- 自定义在服务端保存的文件名file- 文件- Returns:
- 是否上传成功
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
uploadFile
public boolean uploadFile(String remotePath, String fileName, InputStream fileStream) throws org.miaixz.bus.core.lang.exception.InternalException 上传文件到指定目录,可选:1. remotePath为null或""上传到当前路径 2. remotePath为相对路径则相对于当前路径的子路径 3. remotePath为绝对路径则上传到此路径
- Parameters:
remotePath- 服务端路径,可以为null或者相对路径或绝对路径fileName- 文件名fileStream- 文件流- Returns:
- 是否上传成功
- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
upload
递归上传文件(支持目录) 上传时,如果uploadFile为目录,只复制目录下所有目录和文件到目标路径下,并不会复制目录本身 上传时,自动创建父级目录- Parameters:
remotePath- 目录路径uploadFile- 上传文件或目录
-
download
下载文件- Parameters:
path- 文件路径,包含文件名outFile- 输出文件或目录,当为目录时,使用服务端的文件名
-
recursiveDownloadFolder
递归下载FTP服务器上文件到本地(文件目录和服务器同步)- Parameters:
sourcePath- ftp服务器目录destDir- 本地目录
-
download
public void download(String path, String fileName, File outFile) throws org.miaixz.bus.core.lang.exception.InternalException 下载文件- Parameters:
path- 文件所在路径(远程目录),不包含文件名fileName- 文件名outFile- 输出文件或目录,当为目录时使用服务端文件名- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
download
下载文件到输出流- Parameters:
path- 文件路径fileName- 文件名out- 输出位置
-
download
public void download(String path, String fileName, OutputStream out, Charset fileNameCharset) throws org.miaixz.bus.core.lang.exception.InternalException 下载文件到输出流- Parameters:
path- 服务端的文件路径fileName- 服务端的文件名out- 输出流,下载的文件写出到这个流中fileNameCharset- 文件名编码,通过此编码转换文件名编码为ISO8859-1- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
getFileStream
Description copied from interface:Ftp读取FTP服务器上的文件为输入流- Parameters:
path- 文件路径- Returns:
InputStream
-
getFileStream
public InputStream getFileStream(String dir, String fileName) throws org.miaixz.bus.core.lang.exception.InternalException 读取文件为输入流- Parameters:
dir- 服务端的文件目录fileName- 服务端的文件名- Returns:
InputStream- Throws:
org.miaixz.bus.core.lang.exception.InternalException- IO异常
-
getClient
public org.apache.commons.net.ftp.FTPClient getClient()获取FTPClient客户端对象- Returns:
FTPClient
-
close
- Throws:
IOException
-