JAVA基础篇【工具类】FtpUtil TG【20130317整理】

[b]Ftp工具[/b]
在我们编程的时候,我们经常会遇到链接ftp存文件和取文件的场景,下面是我为大家分享的Ftp工具类

 import java.io.File;  
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
* 用来操作ftp的综合类。<br/>
* 主要依赖jar包commons-net-3.1.jar。
*
* @author Elfy
* @date 2013年02月25日
*/
public class FtpUtil {
// ftp 地址
private String url;
// ftp端口
private int port;
// 用户名
private String userName;
// 密码
private String password;

/**
* 构造函数
*
* @param url
* ftp地址
* @param port
* ftp端口
* @param userName
* 用户名
* @param password
* 密码
* @author Elfy
* @date 2013年02月25日
*
*/
public FtpUtil(String url, int port, String userName, String password) {
this.url = url;
this.port = port;
this.userName = userName;
this.password = password;
}

/**
* 从FTP服务器下载指定文件名的文件。
*
* @param remotePath
* FTP服务器上的相对路径
* @param fileName
* 要下载的文件名
* @param localPath
* 下载后保存到本地的路径
* @return 成功下载返回true,否则返回false。
* @throws IOException
* @author Elfy
* @date 2013年02月25日
*/
public boolean downFile(String remotePath, String fileName, String localPath)
throws IOException {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
FTPFile ff;
for (int i = 0; i < fs.length; i++) {
ff = fs[i];
if (null != ff && null != ff.getName()
&& ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}

/**
* 从FTP服务器列出指定文件夹下文件名列表。
*
* @param remotePath
* FTP服务器上的相对路径
* @return List<String> 文件名列表,如果出现异常返回null。
* @throws IOException
* @author Elfy
* @date 2013年02月25日
*/
public List<String> getFileNameList(String remotePath) throws IOException {
// 目录列表记录
List<String> fileNames = new ArrayList<String>();
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile file : fs) {
fileNames.add(file.getName());
}
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return fileNames;
}

}



:D 你的到来我很高兴!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值