public class FtpUtils extends AbstractFtp
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_PORT
默认端口
|
charset, DEFAULT_CHARSET, host, password, port, user| Constructor and Description |
|---|
FtpUtils(String host)
构造,匿名登录
|
FtpUtils(String host,
int port)
构造,匿名登录
|
FtpUtils(String host,
int port,
String user,
String password)
构造
|
FtpUtils(String host,
int port,
String user,
String password,
Charset charset)
构造
|
FtpUtils(String host,
int port,
String user,
String password,
Charset charset,
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 |
existFile(String path)
判断ftp服务器文件是否存在
|
org.apache.commons.net.ftp.FTPClient |
getClient()
获取FTPClient客户端对象
|
FtpUtils |
init()
初始化连接
|
FtpUtils |
init(String host,
int port,
String user,
String password)
初始化连接
|
FtpUtils |
init(String host,
int port,
String user,
String password,
FtpMode mode)
初始化连接
|
List<String> |
ls(String path)
遍历某个目录下所有文件和目录,不会递归遍历
|
org.apache.commons.net.ftp.FTPFile[] |
lsFiles(String path)
遍历某个目录下所有文件和目录,不会递归遍历
|
boolean |
mkdir(String dir)
在当前远程目录(工作目录)下创建新的目录
|
String |
pwd()
远程当前目录
|
FtpUtils |
reconnectIfTimeout()
如果连接超时的话,重新进行连接
经测试,当连接超时时,client.isConnected()仍然返回ture,无法判断是否连接超时
因此,通过发送pwd命令的方式,检查连接是否超时
|
FtpUtils |
setMode(FtpMode mode)
设置FTP连接模式,可选主动和被动模式
|
boolean |
upload(String path,
File file)
上传文件到指定目录,可选:
|
boolean |
upload(String path,
String fileName,
File file)
上传文件到指定目录,可选:
|
boolean |
upload(String path,
String fileName,
InputStream fileStream)
上传文件到指定目录,可选:
|
exist, mkDirs, toParentpublic static final int DEFAULT_PORT
public FtpUtils(String host)
host - 域名或IPpublic FtpUtils(String host, int port)
host - 域名或IPport - 端口public FtpUtils(String host, int port, String user, String password)
host - 域名或IPport - 端口user - 用户名password - 密码public FtpUtils(String host, int port, String user, String password, Charset charset)
host - 域名或IPport - 端口user - 用户名password - 密码charset - 编码public FtpUtils init()
public FtpUtils init(String host, int port, String user, String password)
host - 域名或IPport - 端口user - 用户名password - 密码public FtpUtils init(String host, int port, String user, String password, FtpMode mode)
host - 域名或IPport - 端口user - 用户名password - 密码mode - 模式public FtpUtils setMode(FtpMode mode)
mode - 模式枚举public FtpUtils reconnectIfTimeout()
reconnectIfTimeout in class AbstractFtppublic boolean cd(String directory)
cd in class AbstractFtpdirectory - 目录public String pwd()
pwd in class AbstractFtppublic List<String> ls(String path)
AbstractFtpls in class AbstractFtppath - 需要遍历的目录public org.apache.commons.net.ftp.FTPFile[] lsFiles(String path)
path - 目录public boolean mkdir(String dir)
AbstractFtpmkdir in class AbstractFtpdir - 目录名public boolean existFile(String path)
path - 文件路径public boolean delFile(String path)
AbstractFtpdelFile in class AbstractFtppath - 目录路径public boolean delDir(String dirPath)
AbstractFtpdelDir in class AbstractFtpdirPath - 文件夹路径public boolean upload(String path, File file)
1. path为null或""上传到当前路径 2. path为相对路径则相对于当前路径的子路径 3. path为绝对路径则上传到此路径
upload in class AbstractFtppath - 服务端路径,可以为null 或者相对路径或绝对路径file - 文件public boolean upload(String path, String fileName, File file)
1. path为null或""上传到当前路径 2. path为相对路径则相对于当前路径的子路径 3. path为绝对路径则上传到此路径
file - 文件path - 服务端路径,可以为null 或者相对路径或绝对路径fileName - 自定义在服务端保存的文件名public boolean upload(String path, String fileName, InputStream fileStream)
1. path为null或""上传到当前路径 2. path为相对路径则相对于当前路径的子路径 3. path为绝对路径则上传到此路径
path - 服务端路径,可以为null 或者相对路径或绝对路径fileName - 文件名fileStream - 文件流public void download(String path, File outFile)
download in class AbstractFtppath - 文件路径outFile - 输出文件或目录public void download(String path, String fileName, File outFile)
path - 文件路径fileName - 文件名outFile - 输出文件或目录public void download(String path, String fileName, OutputStream out)
path - 文件路径fileName - 文件名out - 输出位置public org.apache.commons.net.ftp.FTPClient getClient()
FTPClientpublic void close()
throws IOException
IOExceptionCopyright © 2019. All rights reserved.