Commons Net 2.0 API 之ftpClient类

本文介绍了Java中的FTPClient类,该类提供了与FTP服务器交互的功能,包括连接、断开连接、上传文件、下载文件等操作,并详细解释了listFiles()与listNames()的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ftpClient类囊括了Java对于ftp服务器上的一系列操作。

ftpClient类继承自ftp类,增加了上传文件流、下载文件流、获得目录名称和文件名称等一系列操作。

创建ftpClient对象

ftpClient = new FTPClient();

连接ftpClient对象,为该对象添加一个connect方法

public boolean connect() {
boolean isConnectionSuccess = false;
if (ftpClient == null) {
ftpClient = new FTPClient();
}
try {
ftpClient.connect(host, port);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
return false;
}
isConnectionSuccess = ftpClient.login(userName, password);
if (!isConnectionSuccess) {
return false;
}
if (!ftpClient.setFileType(FTP.BINARY_FILE_TYPE)) {
return false;
}
if (workingDirectory != null) {
ftpClient.makeDirectory(workingDirectory);
if (!ftpClient.changeWorkingDirectory(workingDirectory)) {
return false;
}
}
ftpClient.enterLocalPassiveMode();
} catch (IOException e) {
if (ftpClient != null && ftpClient.isConnected())
try {
ftpClient.disconnect();
} catch (IOException e1) {
return false;
}
}
return isConnectionSuccess;
}


关闭连接

public void disConnect() {
if (ftpClient != null && ftpClient.isConnected())
try {
ftpClient.disconnect();
} catch (IOException e1) {
logger
.error("[error]--disConnect exception:"
+ e1.getMessage());
}
}

上传至ftp方法 ftpclient.appendFile(string filename,fileinputstream fileinputstream)

public boolean uploadFile(File file) {
boolean isUpload = false;
InputStream in = null;
if (file != null) {
try {
in = new FileInputStream(file);
isUpload = ftpClient.appendFile(file.getName(), in);
} catch (IOException e) {
return false;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
return false;
}
}
}
} else {
return false;
}
return isUpload;
}

从ftp下载文件

public boolean downloadFile(String remoteFile,String localFile) throws IOException
{
boolean flag=false;
File outfile=new File(localFile);
FileOutputStream out=null;
if(!outfile.exists())
{
String[] names = ftpClient.listNames(remoteFile);
if(names.length!=0)
{
try
{
out=new FileOutputStream(outfile);
flag=ftpClient.retrieveFile(remoteFile, out);
}
catch (IOException e) {
flag = false;
return flag;
} finally {
try {
if (out != null)
{
out.close();
flag=true;
logger.info("Download successfully...");
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
return flag;
}
}
}
else
{
logger.error("File not found from the FTP...");
flag=false;
return flag;
}
}
else
{
logger.error("The localFile exists,please renamed the localFile!");
flag=false;
return flag;
}
}


请注意ftpclient 的listFiles()和listNames()的区别:

1.listFiles()可以列出目录下所有的文件 返回对象为ftpFile类的数组ftpFile[]。

2.重载:同时在listFiles(string filename)增加参数可以用于查询 当前目录下所有名称包含"filename"的文件,返回同样是ftpFile[]。

3.技巧:可以根据ftpFile[]数组的length来确认文件是否存在。

4.区别:listFiles()和listNames()的区别在于listFiles()返回文件数组ftpFile[],而listNames()返回文件名称数组string[]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值