java 读FTP 文件


package com.tzx.common.util.ftpscan;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import com.tzx.bo.iface.erp.filescan.BalanceRecordService;
import com.tzx.common.util.SpringDAOUtil;
import com.tzx.po.impl.hbn.pojo.erp.ftpscan.ErpBalanceRecord;

public class FTPListAllFiles {
private static Log log=LogFactory.getLog(FTPListAllFiles.class);
private BalanceRecordService balanceRecordService;
public FTPClient ftp;

public FTPListAllFiles() {
super();
}

public FTPListAllFiles(boolean isPrintCommmand) {
ftp = new FTPClient();
}

// 登录
public boolean login(String host, int port, String username, String password)
throws IOException {
this.ftp.connect(host, port);
if (FTPReply.isPositiveCompletion(this.ftp.getReplyCode())) {
if (this.ftp.login(username, password)) {
this.ftp.setControlEncoding("GBK");
return true;
}
}
if (this.ftp.isConnected()) {
this.ftp.disconnect();
}
return false;
}

// 关闭连接
public void disConnection() throws IOException {
if (this.ftp.isConnected()) {
this.ftp.disconnect();
}
}

// 通过路径获得路径下所有文件 输出文件名
public void List(String pathName) throws IOException {
if (pathName.startsWith("/") && pathName.endsWith("/")) {
String directory = pathName;
this.ftp.changeWorkingDirectory(directory);
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd*");
String dateStr = formatter.format(new Date());
FTPFile[] files = this.ftp.listFiles(dateStr);
InputStream is = null;
BufferedReader reader = null;
List<ErpBalanceRecord> ebrlist = new LinkedList<ErpBalanceRecord>();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
is = ftp.retrieveFileStream(files[i].getName());
if (is != null) {
int index = 0;
String line;
reader = new BufferedReader(new InputStreamReader(is));
String shh = null;
String shmc = null;
while ((line = reader.readLine()) != null) {
if (0 != index && 2 != index) {
String[] strTotal = null;
if (1 == index) {
strTotal = line.split("\\|");
shh = strTotal[1];
shmc = strTotal[2];
} else {
String[] strEbr = line.split("\\|");
ErpBalanceRecord ebr = new ErpBalanceRecord();
ebr.setDdbh(strEbr[0]);
ebr.setJyrq(strEbr[1]);
ebr.setJyje(Double.valueOf(strEbr[2]
.replace(",", "")));
ebr.setJysxf(Double.valueOf(strEbr[3]
.replace(",", "")));
ebr.setSbje(Double.valueOf(strEbr[4]
.replace(",", "")));
ebr.setZdbh(strEbr[5]);
ebr.setShh(shh);
ebr.setShmc(shmc);
ebr.setJylx("银行卡");
ebrlist.add(ebr);
}

}
index++;
}
}
reader.close();
if (is != null) {
is.close();
}

if (!ftp.completePendingCommand()) {
ftp.logout();
}
}
}
try {
balanceRecordService = (BalanceRecordService) SpringDAOUtil
.getBean("balanceRecordService");
if (ebrlist.size() > 0) {
balanceRecordService.batchSaveBalanceRecord(ebrlist);
log.info("成功插入 "+ebrlist.size()+" 条记录");
} else {
log.info("无数据");
}
} catch (Exception e) {
log.error("批处理执行出错");
e.printStackTrace();
}
}
}

public static void ftpService() throws IOException {
FTPListAllFiles f = new FTPListAllFiles(true);
// 地址,端口号,用户名,密码
if (f.login("ftp.haidilao.net", 21, "ylgl", "hdlasd123")) {
f.List("/");
}
f.disConnection();
}

public static void main(String[] args) throws IOException {
FTPListAllFiles.ftpService();

}

public void setBalanceRecordService(
BalanceRecordService balanceRecordService) {
this.balanceRecordService = balanceRecordService;
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值