JAva FTPファイルを読む

5032 ワード


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 ebrlist = new LinkedList();
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;
}

}