class="java" name="code">
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;
	}
}
  
  
  
    
      
        
          - commons-net-3.3.jar (274.4 KB)
- 下载次数: 0