java通過ftp方式讀取檔案,並解析入庫

2021-06-14 18:29:41 字數 4903 閱讀 4282

對於自己是個總結,下次使用的時候直接copy,要注意的地方是ftp訪問檔案的方式,訪問的是ftp使用者的home目錄,而不是從絕對路徑訪問。

private final static string deleteuser = "delete from customermanager_user where cmanagerno in ";

private final static string queryuser = "select cmanagerno from customermanager_user where cmanagerno in ";

private final static string insertuser = "insert into customermanager_user(cmanagerno, cmanagercity, cmanagercontry, cmanagername, cmanagercontact, cmanagerstatus) values(?,?,?,?,?,?)";

private custommanagerjobdao managerjobdao;

public custommanagerjobdao getmanagerjobdao()

public void setmanagerjobdao(custommanagerjobdao managerjobdao)

/*** customermanager_group 表增刪查

*/private final static string deletegroup = "delete from customermanager_group where id in ";

private final static string querygroup = "select id from customermanager_group where id in ";

private final static string insertgroup = "insert into customermanager_group(id, cmgroupno, cmgroupcity, cmgroupcontry, cmgroupcustomername, cmgroupcorno, cmgroupcoradd, cmgroupcustomertype, cmgroupiffirst, cmgroupcustomerlevel, cmgroupcontact, cmgroupslalevel, cmgroupaddress, cmgroupsatus) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

private static integer file1length = integer.valueof(6);

private static integer file2length = integer.valueof(14);

private final static string removeflag = "1";

private final static string updateflag = "0";

private list removekeylist = new linkedlist(); 

private list insertkeylist = new linkedlist();

private list insertlist = new linkedlist();

static

string file2 = staticmethod.nullobject2string(xmlmanage.getfile(

"/config/customanager-ftp-boss.xml").getproperty("file2length"));

if (file1 != null && file2.length() >= 0)

}public void parsedata(string filepath) else if (updateflag.equals(keyflag))

} else if (table2size == fileds.length) else if (updateflag.equals(keyflag))

}if (count % 5000 == 0)

queryanddelete(removekeylist, insertlist); // 先查一把!然後再刪

queryandinsert(insertkeylist, insertlist);// 先查一把!然後再新增

removekeylist.clear();

insertkeylist.clear();

insertlist.clear();

}br.close();

long endtime = system.currenttimemillis();

system.out.println((endtime - begintime) / 3600);

} catch (filenotfoundexception e) catch (ioexception e)

}public void getfilebyftp()

string filename = staticmethod.getyyyymmdd(staticmethod

.getlocalstring(-1));

system.out.println("filename===" + filename);

string filepath1 = "ftp://" + userlogin + ":" + pwdlogin + "@"

+ ftpserver + serverpath + filename + "_001.txt";

string filepath2 = "ftp://" + userlogin + ":" + pwdlogin + "@"

+ ftpserver + serverpath + filename + "_002.txt";

system.out.println("path===" + filepath1);

url url = new url(filepath1);

urlconnection urlconn = url.openconnection();

bufferedreader br = new bufferedreader(new inputstreamreader(

urlconn.getinputstream()));

string line = null;

while ((line = br.readline()) != null)

parsedata(filepath1);

parsedata(filepath2);

br.close();

closeftpconnect();

} catch (malformedurlexception e1) catch (ioexception e)  

}/**

* 把地市名稱換成id

* * @param keylist

*/public string changeareanametoid(string records) else if (file2length.intvalue() == records.length)

return records;

}/**

* 對資料進行查詢,如果存在則刪除

* * @param keylist

*/public void queryanddelete(list keylist, list datalist)

list retlist = null;

try

} else if (file2length.intvalue() == records.length)

}} catch (sqlexception e)

}public void queryandinsert(list keylist, list datalist)

list retlist = new arraylist();

try else

} else if (file2length.intvalue() == records.length && retlist != null) else

}} catch (sqlexception e)

}public static string getexecutesql(final string querysql, list retlist)

record = sqlsb.tostring();

string sqlwhere = record.substring(0, record.lastindexof(","));

return querysql + "(" + sqlwhere + ")";

}public logger logger = logger.getlogger(this.getclass());

ftpclient ftpclient=null;

/* * 獲取ftp服務

*/public string connectserver( string ip, int port, string user,

string password, string path) catch (ioexception ex)

return returnresult;

}private string connecttoftpserver(string ftpserver, string userlogin,

string pwdlogin) catch (ftploginexception e) catch (ioexception e) catch (securityexception e)

return "connect success";

}  /*

* 關閉ftp

*/public void closeftpconnect() catch (ioexception ex) }}

informatica讀取FTP檔案

以下為乙個完整的informatica讀取ftp檔案,並匯入到系統中。sh home infa informatica 9.5.1 server infa shared crm prod shell ftpfromlive.sh zip file name shell指令碼內容如下 bin bash...

Java 讀取配置檔案的方式

通過乙個propertiesutil類,在tomcat初始化時,對properties物件props進行初始化 指向對應的配置檔案filename mmall public class propertiesutil catch ioexception e public static string g...

檔案讀取方式

過程 1.開啟檔案 2.讀資料 3.關閉檔案 1,w 寫模式,它是不能讀的,如果用w模式開啟乙個已經存在的檔案,會清空以前的檔案內容,重新寫 w 是讀寫內容,只要沾上w,肯定會清空原來的檔案 2,r 讀模式,只能讀,不能寫,而且檔案必須存在 r 是讀寫模式,只要沾上r,檔案必須存在 3,a 追加模式...