-
ftp 文件读取和解析入库
- package com.longtop.ecommerce.service.dept;
-
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.SocketException;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
-
- import com.longtop.ecommerce.bo.Organization;
-
- public class FtpUtils {
-
-
- private FTPClient ftpClient;
- private String fileName, strencoding;
- private int columns, rowCount;
- private String ip = "65.0.15.26";
- private String userName = "anonymous";
- private String userPwd = "anonymous";
- private int port = 21;
- private String path = "/aaa/CIC_Department/";
- public FtpUtils() {
- this.reSet();
- }
- public void reSet(){
- fileName = "t_department_"+getFileName()+".txt";
- columns = 0;
- rowCount = 0;
- strencoding = "GBK";
- this.connectServer(ip, port, userName, userPwd, path);
- }
-
- private String getFileName(){
- SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMdd");
- String str = "";
- try {
- str = sdFormat.format(new Date());
- }
- catch(Exception e) {
- return "";
- }
- if (str.equals("1900-01-01")) {
- str = "";
- }
-
- return str;
- }
-
-
- public void connectServer(String ip , int port , String userName , String userPwd , String path){
- ftpClient = new FTPClient();
- try {
- ftpClient.connect(ip, port);
- ftpClient.login(userName, userPwd);
- if(path != null && path.length() > 0){
- ftpClient.changeWorkingDirectory(path);
- }
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public void closeServer(){
- if(ftpClient.isConnected()){
- try {
- ftpClient.logout();
- ftpClient.disconnect();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- public List<String> getFileList(String path){
- List<String> fileLists = new ArrayList<String>();
- FTPFile[] ftpFiles = null;
- try {
- ftpFiles = ftpClient.listFiles(path);
- } catch (IOException e) {
- e.printStackTrace();
- }
- for(int i = 0 ; ftpFiles != null && i < ftpFiles.length ; i++){
- FTPFile file = ftpFiles[i];
- if(file.isFile()){
- fileLists.add(file.getName());
- }
- }
- return fileLists;
- }
-
- public boolean unloadFile(String fileName , String sourceFile){
- boolean flag = false;
- try{
- FileOutputStream fos = new FileOutputStream(fileName);
- flag = ftpClient.retrieveFile(sourceFile, fos);
- fos.flush();
- fos.close();
- }catch(Exception e){
- flag = false;
- e.printStackTrace();
- }
- return flag;
- }
- public String readFile(String fileName){
- String result = "";
- InputStream ins = null;
- try {
- ins = ftpClient.retrieveFileStream(fileName);
-
- BufferedReader reader=new BufferedReader(new InputStreamReader(ins));
- String inLine = reader.readLine();
- while (inLine != null) {
- result += (inLine + System.getProperty("line.separator"));
- inLine = reader.readLine();
- }
- reader.close();
- if(ins != null){
- ins.close();
- }
-
- ftpClient.getReply();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return result;
- }
-
- public List readFile() throws ParseException{
-
- List<Organization> contentList = new ArrayList<Organization>();
- InputStream ins = null;
- try {
- ins = ftpClient.retrieveFileStream(fileName);
-
- BufferedReader reader=new BufferedReader(new InputStreamReader(ins,strencoding));
-
- String inLine = reader.readLine();
-
- while (inLine != null) {
- if (inLine.length() + 1 > columns)
- columns = inLine.length() + 1;
- String beanStr = inLine+System.getProperty("line.separator");
- if(beanStr.indexOf(",")>0){
- String[] beanStrs = beanStr.split(",");
- Organization org = new Organization();
- DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
- Date date = null;
- date = format1.parse(beanStrs[0].substring(1, beanStrs[0].length()-1));
- org.setBuildDate(date);
- String parentId = null;
- if((beanStrs[1].substring(1, beanStrs[1].length()-1)).equals("")){
- parentId = "00";
- org.setOrgLevel("00");
- }else parentId = (beanStrs[1].substring(1, beanStrs[1].length()-1));
- org.setParentId(parentId);
- org.setOrgCode(beanStrs[2].substring(1, beanStrs[2].length()-1));
- org.setOrgBrief(beanStrs[3].substring(1, beanStrs[3].length()-1));
- if(beanStrs[4].length()>3){
- org.setOrgName(beanStrs[4].substring(1, beanStrs[4].length()-3));
- }
- contentList.add(org);
- }
-
- inLine = reader.readLine();
- rowCount++;
- }
- reader.close();
- if(ins != null){
- ins.close();
- }
- ftpClient.getReply();
- System.out.println("此次任务一共读取["+contentList.size()+"]条数据记录");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return contentList;
- }
-
- public void deleteFile(String fileName){
- try {
- ftpClient.deleteFile(fileName);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) throws ParseException {
- FtpUtils ftp = new FtpUtils();
-
- ftp.readFile();
-
- }
-
相关阅读:
我真的没读野鸡大学!是他们不好好起名字!
Request.Cookies和Response.Cookies
深受理科生喜欢的10大专业
如何玩转“互联网+教育”?
js调试工具Console命令详解
XSS获取cookie并利用
257. Binary Tree Paths
EXEC sp_executesql with multiple parameters
235. Lowest Common Ancestor of a Binary Search Tree
226. Invert Binary Tree
-
原文地址:https://www.cnblogs.com/zhuzhuxuan/p/6377450.html
Copyright © 2020-2023
润新知