org.apache.commons.net.ftp的使用

1、设置FTP 属性:用户名、密码、IP、端口、字符集等,利用properties文件实现动态配置
2、创建FTP连接:
private boolean connectServer() throws Exception {
boolean flag = true;
int reply;
if (this.ftpClient == null) {
try {
this.ftpClient = new FTPClient();
// 设置文件传输格式
this.ftpClient.setControlEncoding(this.charset);
// 设置FTP服务器端口
this.ftpClient.setDefaultPort(this.port);
// 设置FTP服务器IP
this.ftpClient.connect(this.ip);

log.info("Connected to " + this.ip);
// 登陆FTP服务器
boolean isLogin=this.ftpClient.login(this.userName, this.password);
if(isLogin){
log.info(this.userName+" is login FTP "+this.ip);
}else{
log.error(this.userName+" login FTP server refused");
}

// 设置FTP传输二进制文件
boolean setFtpType=this.ftpClient.setFileType(this.ftpClient.BINARY_FILE_TYPE);
reply = this.ftpClient.getReplyCode();
this.ftpClient.setDefaultTimeout(180000);
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
log.info("FTP server refused connection.");
flag = false;
}
} catch (SocketException e) {
// TODO Auto-generated catch block
log.error("创建FTP连接失败\n" + e.toString());
throw e;
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("创建FTP连接失败\n" + e.toString());
throw e;
}
}
return flag;
}
3、远程创建文件夹,(需递归创建)
/**
* 创建远程文件夹
*
* @param remotePath:FTP服务器的根目录
* @param dicName:文件存储目录(多级)
* @return
* @throws Exception
*/
private boolean createDics(String remotePath, String dicName)
throws Exception {
FTPFile[] files;
String[] root = remotePath.split("/");
String[] dics = dicName.split("/");
List dicList = new LinkedList();
if (dics != null) {
for (int i = 0; i < dics.length; i++) {
dicList.add(dics[i]);
}
}
if (root != null) {
for (int i = 0; i < root.length; i++) {
String rootName = root[i];
log.info("FTP 根目录:"+rootName);
ftpClient.changeWorkingDirectory(rootName);
}
}
return this.createDic(dicList);
}

/**
* 递归创建远程文件夹
*
* @param dics:存储文件夹名(一级)
* @return
*/
private boolean createDic(List dics) {
boolean isCreated = false;
try {
if (dics == null || dics.size() == 0) {
return true;
} else {
FTPFile[] files = this.ftpClient.listFiles();
String dicName = dics.get(0).toString();
log.info("文件夹:"+dicName);
if (files == null || files.length == 0) {
isCreated = this.ftpClient.makeDirectory(dicName);
} else {
for (int i = 0; i < files.length; i++) {

FTPFile ftpFile = files[i];
String ftpName = ftpFile.getName();
if (ftpName.equals(dicName)) {
isCreated = true;
break;
}
}
if (!isCreated) {
isCreated = this.ftpClient.makeDirectory(dicName);
}
log.info("FTP dic iscreated:"+isCreated);
}
if (isCreated) {
boolean isChanged = this.ftpClient
.changeWorkingDirectory(dicName);
log.info("FTP dic isChanged:"+isChanged);
dics.remove(0);
if (isChanged) {
haveDic = createDic(dics);
} else {
return false;
}
} else {
return false;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
log.error(e.toString());
return false;
}
return isCreated;
}
4、上传文件到指定目录
private boolean uploadFile(String fileLocalPath, String fileRemotePath) {
FileInputStream fis = null;
boolean isUpload = false;
try {
String dicName = this.getDicName(fileRemotePath);
log.info("FTP 文件夹:"+dicName);
String fileName = fileRemotePath.replace(dicName, "");
fileName = fileName.replace("/", "");
log.info("FTP 文件名:"+fileName);
createDics(this.rootPath, dicName);
if (this.haveDic) {
fis = new FileInputStream(fileLocalPath);
isUpload = this.ftpClient.storeFile(fileName, fis);
} else {
throw new Exception("没有相对应的文件夹");
}
} catch (IOException e) {
// TODO Auto-generated catch block
if (fis != null) {
try {
fis.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
log.error("上传文件失败\n" + e.toString());
isUpload = false;
} catch (Exception e) {
// TODO Auto-generated catch block
log.error("上传文件失败\n" + e.toString());
isUpload = false;
}
return isUpload;
}
5、关闭FTP连接
private void closeConnect() {
if (this.ftpClient != null) {
try {
this.ftpClient.logout();
this.ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
log.error("关闭Ftp连接失败!\n" + e.toString());
}

}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值