public class CommonsFtp extends AbstractFtp
常见搭建ftp的工具有:
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_PORT
默认端口
|
ftpConfig
DEFAULT_CHARSET
Constructor and Description |
---|
CommonsFtp(org.apache.commons.net.ftp.FTPClient client)
构造
|
CommonsFtp(FtpConfig config,
FtpMode mode)
构造
|
Modifier and Type | Method and Description |
---|---|
boolean |
cd(String directory)
改变目录
|
void |
close() |
boolean |
delDir(String dirPath)
删除文件夹及其文件夹下的所有文件
|
boolean |
delFile(String path)
删除指定目录下的指定文件
|
void |
download(String path,
File outFile)
下载文件
|
void |
download(String path,
String fileName,
File outFile)
下载文件
|
void |
download(String path,
String fileName,
OutputStream out)
下载文件到输出流
|
boolean |
download(String path,
String fileName,
OutputStream out,
Charset fileNameCharset)
下载文件到输出流
|
boolean |
existFile(String path)
判断ftp服务器目录内是否还有子元素(目录或文件)
|
org.apache.commons.net.ftp.FTPClient |
getClient()
获取FTPClient客户端对象
|
InputStream |
getFileStream(String path)
读取FTP服务器上的文件为输入流
|
InputStream |
getFileStream(String dir,
String fileName)
读取文件为输入流
|
CommonsFtp |
init()
初始化连接
|
CommonsFtp |
init(FtpConfig config,
FtpMode mode)
初始化连接
|
boolean |
isBackToPwd()
是否执行完操作返回当前目录
|
List<String> |
ls(String path)
遍历某个目录下所有文件和目录,不会递归遍历
|
List<String> |
ls(String path,
Predicate<org.apache.commons.net.ftp.FTPFile> predicate)
遍历某个目录下所有文件和目录,不会递归遍历
此方法自动过滤"." |
org.apache.commons.net.ftp.FTPFile[] |
lsFiles(String path)
遍历某个目录下所有文件和目录,不会递归遍历
|
List<org.apache.commons.net.ftp.FTPFile> |
lsFiles(String path,
Predicate<org.apache.commons.net.ftp.FTPFile> predicate)
遍历某个目录下所有文件和目录,不会递归遍历
此方法自动过滤"." |
boolean |
mkdir(String dir)
在当前远程目录(工作目录)下创建新的目录
|
static CommonsFtp |
of(Connector connector,
Charset charset)
构造
|
static CommonsFtp |
of(Connector connector,
Charset charset,
String serverLanguageCode,
String systemKey)
构造
|
static CommonsFtp |
of(Connector connector,
Charset charset,
String serverLanguageCode,
String systemKey,
FtpMode mode)
构造
|
static CommonsFtp |
of(String host)
构造CommonsFtp,匿名登录
|
static CommonsFtp |
of(String host,
int port)
构造,匿名登录
|
static CommonsFtp |
of(String host,
int port,
String user,
String password)
构造
|
String |
pwd()
远程当前目录
|
CommonsFtp |
reconnectIfTimeout()
如果连接超时的话,重新进行连接 经测试,当连接超时时,client.isConnected()仍然返回ture,无法判断是否连接超时 因此,通过发送pwd命令的方式,检查连接是否超时
|
void |
recursiveDownloadFolder(String sourcePath,
File destDir)
递归下载FTP服务器上文件到本地(文件目录和服务器同步)
|
CommonsFtp |
setBackToPwd(boolean backToPwd)
设置执行完操作是否返回当前目录
|
CommonsFtp |
setMode(FtpMode mode)
设置FTP连接模式,可选主动和被动模式
|
int |
stat(String path)
获取服务端目录状态。
|
void |
upload(String remotePath,
File uploadFile)
递归上传文件(支持目录)
上传时,如果uploadFile为目录,只复制目录下所有目录和文件到目标路径下,并不会复制目录本身 上传时,自动创建父级目录 |
boolean |
uploadFile(String remotePath,
File file)
上传文件到指定目录,可选:
1. remotePath为null或""上传到当前路径
2. remotePath为相对路径则相对于当前路径的子路径
3. remotePath为绝对路径则上传到此路径
|
boolean |
uploadFile(String remotePath,
String fileName,
File file)
上传文件到指定目录,可选:
1. remotePath为null或""上传到当前路径
2. remotePath为相对路径则相对于当前路径的子路径
3. remotePath为绝对路径则上传到此路径
|
boolean |
uploadFile(String remotePath,
String fileName,
InputStream fileStream)
上传文件到指定目录,可选:
1. remotePath为null或""上传到当前路径
2. remotePath为相对路径则相对于当前路径的子路径
3. remotePath为绝对路径则上传到此路径
|
download, exist, getConfig, mkDirs
public static final int DEFAULT_PORT
public CommonsFtp(FtpConfig config, FtpMode mode)
config
- FTP配置mode
- 模式public CommonsFtp(org.apache.commons.net.ftp.FTPClient client)
client
- 自定义实例化好的FTPClient
public static CommonsFtp of(String host)
host
- 域名或IPpublic static CommonsFtp of(String host, int port)
host
- 域名或IPport
- 端口public static CommonsFtp of(String host, int port, String user, String password)
host
- 域名或IPport
- 端口user
- 用户名password
- 密码public static CommonsFtp of(Connector connector, Charset charset)
connector
- 连接信息,包括host、port、user、password等信息charset
- 编码public static CommonsFtp of(Connector connector, Charset charset, String serverLanguageCode, String systemKey)
connector
- 连接信息,包括host、port、user、password等信息charset
- 编码serverLanguageCode
- 服务器语言 例如:zhsystemKey
- 服务器标识 例如:org.apache.commons.net.ftp.FTPClientConfig.SYST_NTpublic static CommonsFtp of(Connector connector, Charset charset, String serverLanguageCode, String systemKey, FtpMode mode)
connector
- 连接信息,包括host、port、user、password等信息charset
- 编码serverLanguageCode
- 服务器语言systemKey
- 系统关键字mode
- 模式public CommonsFtp init()
public CommonsFtp init(FtpConfig config, FtpMode mode)
config
- FTP配置mode
- 模式public CommonsFtp setMode(FtpMode mode)
mode
- 模式枚举public CommonsFtp setBackToPwd(boolean backToPwd)
backToPwd
- 执行完操作是否返回当前目录public boolean isBackToPwd()
public CommonsFtp reconnectIfTimeout()
public boolean cd(String directory)
directory
- 目录public String pwd()
public List<String> ls(String path)
Ftp
path
- 需要遍历的目录public List<String> ls(String path, Predicate<org.apache.commons.net.ftp.FTPFile> predicate)
path
- 目录predicate
- 过滤器,null表示不过滤,默认去掉"."和".."两种目录public List<org.apache.commons.net.ftp.FTPFile> lsFiles(String path, Predicate<org.apache.commons.net.ftp.FTPFile> predicate)
path
- 目录predicate
- 过滤器,null表示不过滤,默认去掉"."和".."两种目录public org.apache.commons.net.ftp.FTPFile[] lsFiles(String path) throws FtpException, IORuntimeException
path
- 目录,如果目录不存在,抛出异常FtpException
- 路径不存在IORuntimeException
- IO异常public boolean mkdir(String dir) throws IORuntimeException
Ftp
dir
- 目录名IORuntimeException
public int stat(String path) throws IORuntimeException
path
- 路径IORuntimeException
- IO异常public boolean existFile(String path) throws IORuntimeException
path
- 文件路径IORuntimeException
- IO异常public boolean delFile(String path) throws IORuntimeException
Ftp
path
- 目录路径IORuntimeException
public boolean delDir(String dirPath) throws IORuntimeException
Ftp
dirPath
- 文件夹路径IORuntimeException
public boolean uploadFile(String remotePath, File file)
1. remotePath为null或""上传到当前路径 2. remotePath为相对路径则相对于当前路径的子路径 3. remotePath为绝对路径则上传到此路径
remotePath
- 服务端路径,可以为null
或者相对路径或绝对路径file
- 文件public boolean uploadFile(String remotePath, String fileName, File file) throws IORuntimeException
1. remotePath为null或""上传到当前路径 2. remotePath为相对路径则相对于当前路径的子路径 3. remotePath为绝对路径则上传到此路径
file
- 文件remotePath
- 服务端路径,可以为null
或者相对路径或绝对路径fileName
- 自定义在服务端保存的文件名IORuntimeException
- IO异常public boolean uploadFile(String remotePath, String fileName, InputStream fileStream) throws IORuntimeException
1. remotePath为null或""上传到当前路径 2. remotePath为相对路径则相对于当前路径的子路径 3. remotePath为绝对路径则上传到此路径
remotePath
- 服务端路径,可以为null
或者相对路径或绝对路径fileName
- 文件名fileStream
- 文件流IORuntimeException
- IO异常public void upload(String remotePath, File uploadFile)
remotePath
- 目录路径uploadFile
- 上传文件或目录public void download(String path, File outFile)
path
- 文件路径,包含文件名outFile
- 输出文件或目录,当为目录时,使用服务端的文件名public void recursiveDownloadFolder(String sourcePath, File destDir)
sourcePath
- ftp服务器目录destDir
- 本地目录public void download(String path, String fileName, File outFile) throws IORuntimeException
path
- 文件所在路径(远程目录),不包含文件名fileName
- 文件名outFile
- 输出文件或目录,当为目录时使用服务端文件名IORuntimeException
- IO异常public void download(String path, String fileName, OutputStream out)
path
- 文件路径fileName
- 文件名out
- 输出位置public boolean download(String path, String fileName, OutputStream out, Charset fileNameCharset) throws IORuntimeException
path
- 服务端的文件路径fileName
- 服务端的文件名out
- 输出流,下载的文件写出到这个流中fileNameCharset
- 文件名编码,通过此编码转换文件名编码为ISO8859-1IORuntimeException
- IO异常public InputStream getFileStream(String path)
Ftp
path
- 文件路径InputStream
public InputStream getFileStream(String dir, String fileName) throws IORuntimeException
dir
- 服务端的文件目录fileName
- 服务端的文件名InputStream
IORuntimeException
- IO异常public org.apache.commons.net.ftp.FTPClient getClient()
FTPClient
public void close() throws IOException
IOException
Copyright © 2025. All rights reserved.