ftp上传下载


package com.ftp.example;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FileTransferClient;

public class FtpClientExample {

/**
*
* @param ftpip ftpIP地址
* @param user 用户
* @param pswd 密码
* @return
*/
public FileTransferClient getConnect(String ftpip, String user, String pswd) {
FileTransferClient client = new FileTransferClient();
try {
client.setRemoteHost(ftpip);
client.setUserName(user);
client.setPassword(pswd);
client.setRemotePort(Integer.parseInt("21"));
client.connect();
if (client.isConnected()) {
client.setContentType(FTPTransferType.BINARY);
System.out.println("------ftp连接成功!!");
client.getAdvancedFTPSettings().setControlEncoding("GB2312");
return client;
} else {
System.out.println("------不能登录FTP服务器,请和管理员联系!!");
return null;
}
} catch (FTPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;

}

/**
* 上传到ftp
* @param localhostPath 本地的文件路径
* @param ftpPath 上传存放文件的ftp路径
* @param FileName 存放到ftp文件名
* @return
* @throws FTPException
* @throws IOException
* @throws ParseException
*/
public boolean uploadFileToFtp(String localhostPath, String ftpPath,
String FileName) throws FTPException, IOException, ParseException {
String pathFile = localhostPath;
String reFile = ftpPath + FileName;
FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
int length = client.directoryList(ftpPath).length;
if (length <= 0) {
System.out.println("FTP文件路径错误");
return false;
}
client.uploadFile(pathFile, reFile);
client.disconnect();
return true;
}



/**
* 从ftp下载到本地
* @param ftpPath
* @param fileNames
* @param localhostPath
* @return
* @throws FTPException
* @throws IOException
* @throws ParseException
*/
public boolean downloadFileFromFtp(String ftpPath, List fileNames,
String localhostPath) throws FTPException, IOException,
ParseException {
String pathFile = localhostPath;
String reFile = ftpPath;
FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
int length = client.directoryList(ftpPath).length;
if (length <= 0) {
System.out.println("ftp路径" + ftpPath + "不是正确:");
return false;
}
System.out.println("可下载的文件个数" + fileNames.size());
for (int i = 0; i < fileNames.size(); i++) {
String fileName = (String) fileNames.get(i);
pathFile = pathFile + "\\" + fileName;
reFile = reFile + fileName;
System.out.println(reFile + "是否存在:" + client.exists(reFile));
if (client.exists(reFile)) {
System.out.println("pathFile--" + pathFile);
System.out.println("reFile--" + reFile);
client.downloadFile(pathFile, reFile);
client.deleteFile(reFile);
pathFile = localhostPath;
reFile = ftpPath;
continue;
}
}
client.disconnect();
return true;
}




/**
* 获取文件夹下面所有的子目录文件
*
* @param filePath
* @return
* @throws ParseException
* @throws IOException
* @throws FTPException
*/
public List getPathFile(String filePath) throws FTPException, IOException,
ParseException {
List list = new ArrayList();
FileTransferClient client = this.getConnect("127.0.0.1", "pas", "pas");
FTPFile diretoryNames[];
diretoryNames = client.directoryList(filePath);
if (diretoryNames.length > 0) // 目录下面有文件
{
for (int j = 0; j < diretoryNames.length; j++) {
if (diretoryNames[j].getName().equals(".")
|| diretoryNames[j].getName().equals(".."))
continue;
list.add(diretoryNames[j].getName());
}
}
client.disconnect();
return list;
}




/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FtpClientExample example = new FtpClientExample();
try {
List list = example.getPathFile("/bank/010/sqzf/");
System.out.println(list.size());
} catch (FTPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值