FTP多路径递归下载

 

一、递归下载主类

package cn.net.ssd.utils.decode.gisDraw;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

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

import cn.net.ssd.utils.ftp.FTPUtil;
import cn.net.ssd.utils.ftp.FtpParam;

/**
* 多路径
* 表:
* @author zouweike
*
*/
public class WindUtil {

/**
*
* @param fileList 输入ftp参数和以及采集的文件名称
*
* @return 下载完成后,返回下载文件存放的路径和文件名,记录到数据库中
* @throws Exception
*/
public static List<Map<String, String>> decode(ArrayList<Map<String, String>> fileList) throws Exception {
//例如:ftp 等于 zar,cb2222453,10.152.234.61,/2015,/sjdata/caijiFile/ldzl/zh_radar/zh_radar_jishuju,10,#SYS_TASKID#
String ftpP = fileList.get(0).get("file_name");
String [] param = ftpP.split(",");
String username = param[0]; //ftp用户名
String password = param[1]; //ftp密码
String IP = param[2]; //ftp地址ip
String path = param[3]; //下载源路径的目录
String sjdataStr = param[4]; //保存路径:sjdata的路径
int size = Integer.parseInt(param[5]); //每次下载多少个文件
String taskid = param[6]; //流程id
boolean sfcj_file = "0".equals(param[7]) ? true : false; //是否采集文件 0:采集 1:不采集,返回文件名,方便记录已经下载的文件
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
fileList.remove(0);
String names = listToString(fileList);
OutputStream is = null;
FTPClient ftpClient = null;
try {
FtpParam ftpParam = new FtpParam();
ftpParam.setHost(IP);
ftpParam.setPort(21);
ftpParam.setUsername(username); //用户名
ftpParam.setPassword(password); //密码
ftpParam.setEncoding("iso-8859-1");
ftpParam.setWorkingDirectory(path); //源路径的目录
ftpClient = FTPUtil.getFTPClient(ftpParam);
int fileCount = 0;
ftpClient.changeWorkingDirectory(path);
ftpClient.setControlEncoding("iso-8859-1");//注意编码格式
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setServerLanguageCode("zh");//中文
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding(ftpParam.getEncoding());
//递归采集文件
fileCount = digui(fileCount, list, ftpClient, sjdataStr, path, names, is, IP, size, taskid, sfcj_file);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(ftpClient.isConnected()){
try{
ftpClient.logout(); //退出FTP
ftpClient.disconnect(); //断开连接
}catch(Exception e){
e.printStackTrace();
}
}
if(is !=null) {
is.close();
}
}
return list;
}

private static int digui(int fileCount, List<Map<String, String>> list, FTPClient ftpClient,
String sjdataStr, String path, String names, OutputStream is, String IP,
int size, String taskid, boolean sfcj_file) throws Exception {
if(ftpClient.changeWorkingDirectory(path)) {
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
String ff_fileName = ff.getName();// 服务器上的文件名
if (fileCount >= size) { // 一次只读取文件的数量
break;
}
if(ff.isDirectory()) {
fileCount = digui(fileCount, list, ftpClient, sjdataStr, path + "/" + ff_fileName, names, is, IP, size, taskid, sfcj_file);
} else {
if (names.indexOf(ff_fileName) >= 0) {
continue;
}
if(sfcj_file) { //采集文件,并记录采集表
File localFile = new File(sjdataStr + path + File.separator + ff_fileName);
File fileParent = localFile.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
is = new FileOutputStream(localFile);
ftpClient.enterLocalPassiveMode();
if(ftpClient.retrieveFile(ff_fileName, is)){
HashMap<String, String> filenamemap = new HashMap<String, String>();
filenamemap.put("FILENAME", ff_fileName);
filenamemap.put("FILEPATH", sjdataStr + path);
list.add(filenamemap);
}
if (is != null)
is.close();
} else { //只记录表
HashMap<String, String> filenamemap = new HashMap<String, String>();
filenamemap.put("FILENAME", ff_fileName);
filenamemap.put("FILEPATH", sjdataStr + path);
list.add(filenamemap);
}
fileCount++;
}
}
}
return fileCount;
}

/**
* 返回已经下载的文件名字符串.
* @param list
* @return
*/
public static String listToString(ArrayList<Map<String, String>> list) {
if (list == null) {
return null;
}
StringBuilder result = new StringBuilder();
boolean first = true;
// 第一个前面不拼接","
for (Map map : list) {
JSONObject jsonObject = JSONObject.fromObject(map);
if (first) {
first = false;
} else {
result.append(",");
}
result.append(jsonObject.toString());
}
return result.toString();
}

}

 

 

二、FTPUtil

package cn.net.ssd.utils.ftp;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

import cn.net.ssd.exceptions.ftp.FTPConnectException;

/**
*
* @ClassName: FTPUtil
* @Description:
* @author
* @date 2016年8月23日 下午4:18:06
* @version 1.0
*/
public class FTPUtil {

private static Logger logger = Logger.getLogger(FTPUtil.class);

/**
* 获取FTPClient对象
*
* @Title: getFTPClient
* @Description:
* @param ftpParam
* @return
*/
public static FTPClient getFTPClient(FtpParam ftpParam) {
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
ftpClient.setDefaultTimeout(1000*60*30);
ftpClient.setConnectTimeout(1000*60*30);
ftpClient.setDataTimeout(1000*60*30); //设置传输超时(30分钟)
ftpClient.connect(ftpParam.getHost(), ftpParam.getPort());// 连接FTP服务器
ftpClient.login(ftpParam.getUsername(), ftpParam.getPassword());// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
ftpClient.disconnect();
throw new FTPConnectException("未连接到"+ ftpParam.getHost() +"的FTP,用户名或密码错误。");
} else {
logger.info("FTP连接成功。");
}
} catch (ConnectException e) {
System.out.println();
throw new FTPConnectException("连接错误读取错误。无法连接此路径:" + ftpParam.getHost() + ftpParam.getWorkingDirectory());
} catch(SocketException e) {
e.printStackTrace();
throw new FTPConnectException(ftpParam.getHost() + "的FTP连接地址或端口可能错误,请修连接地址或"+ ftpParam.getPort() +"端口。");
} catch(IOException e) {
e.printStackTrace();
throw new FTPConnectException(ftpParam.getHost() + "的FTP的"+ ftpParam.getPort() +"端口错误,请修改端口。");
}
return ftpClient;
}

}

 

 

三、FTP参数类

package cn.net.ssd.utils.ftp;

import java.util.Date;

import cn.net.ssd.common.ConfigCode;
import cn.net.ssd.context.WebContextHolder;
import cn.net.ssd.entity.ftp.Ftp;
import cn.net.ssd.utils.StringUtils;

/**
* ftp参数对象
*
* @ClassName: FtpParam
* @Description:
* @author
* @date 
* @version 1.0
*/
public class FtpParam extends Ftp {

private static final long serialVersionUID = -3213973145341824250L;

/** 文件名称 **/
private String fileName;

/** 本地文件路径 **/
private String localFilePath;

public String getFileName() {
return fileName;
}

public FtpParam setFileName(String fileName) {
this.fileName = fileName;
return this;
}

public String getLocalFilePath() {
if (StringUtils.isBlank(localFilePath)) {
this.localFilePath = WebContextHolder.getValueByKey(ConfigCode.FTP_TEMP_PATH);
}
return localFilePath;
}

public FtpParam setLocalFilePath(String localFilePath) {
this.localFilePath = localFilePath;
return this;
}

public String getWorkingDirectory() {
return super.workingDirectory;
}

public FtpParam setWorkingDirectory(String workingDirectory) {
/* if (StringUtils.isNotBlank(workingDirectory) && StringUtils.isNotBlank(super.encoding)) {
super.workingDirectory = StringUtils.toEncodingString(workingDirectory, super.encoding);
} else {*/
super.workingDirectory = workingDirectory;
/*}*/
return this;
}

/**
* 设置
*
* @return FtpParam
*/
public FtpParam setId(Integer id) {
super.id = id;
return this;
}

/**
* 获取
*
* @return id
*/
public Integer getId() {
return super.id;
}

/**
* 设置 FTP端口
*
* @return FtpParam
*/
public FtpParam setPort(Integer port) {
super.port = port;
return this;
}

/**
* 获取 FTP端口
*
* @return ftpPort
*/
public Integer getPort() {
return super.port;
}

/**
* 设置 密码
*
* @return
*/
public FtpParam setPassword(String password) {
super.password = password;
return this;
}

/**
* 获取 密码
*
* @return ftpPassword
*/
public String getPassword() {
return super.password;
}

/**
* 设置 FTP链接地址
*
* @return FtpParam
*/
public FtpParam setHost(String host) {
super.host = host;
return this;
}

/**
* 获取 FTP链接地址
*
* @return ftpHost
*/
public String getHost() {
return super.host;
}

/**
* 设置 FTP用户名
*
* @return FtpParam
*/
public FtpParam setUsername(String username) {
super.username = username;
return this;
}

/**
* 获取 FTP用户名
*
* @return ftpUsername
*/
public String getUsername() {
return super.username;
}

/**
* 设置 备注
*
* @return FtpParam
*/
public FtpParam setRemarks(String remarks) {
super.remarks = remarks;
return this;
}

/**
* 获取 备注
*
* @return ftpRemarks
*/
public String getRemarks() {
return super.remarks;
}

public Date getCreateTime() {
return super.createTime;
}

public FtpParam setCreateTime(Date createTime) {
super.createTime = createTime;
return this;
}

public String getName() {
return super.name;
}

public FtpParam setName(String name) {
super.name = name;
return this;
}

public String getEncoding() {
return super.encoding;
}

public FtpParam setEncoding(String encoding) {
super.encoding = encoding;
return this;
}
}

 

转载于:https://www.cnblogs.com/ygkeke/p/10873908.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值