分類  >  Web前端 >

java 讀取Ftp指定位置的資料解析併入庫

tags:    時間:2013-12-10 00:44:38
java 讀取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;  /** * 從FTP讀取文件 * @author tony * */ public class FtpUtils {   private FTPClient ftpClient; private String fileName, strencoding; private int columns, rowCount; private String ip = "65.0.15.26"; 
//伺服器IP地址 
private String userName = "anonymous"; 
//用戶名 
private String userPwd = "anonymous"; 
//密碼 
private int port = 21; 
//埠號 
private String path = "/aaa/CIC_Department/";
 //讀取文件的存放目錄 /** * 
init ftp servere */ 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); }  
/** * 以當前系統時間生成文件名 * @return */ 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; }  
 /** * @param ip * @param port
 * @param userName 
* @param userPwd * @param path 
* @throws SocketException
 * @throws IOException 
* function:連接到伺服器
 */

 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(); } } 
 /** * @throws IOException * function:關閉連接 */ public void closeServer(){ if(ftpClient.isConnected()){ 
try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }  
/** * @param path * @return * function:讀取指定目錄下的文件名 * @throws IOException */
 public List getFileList(String path){ List fileLists = new ArrayList(); 
// 獲得指定目錄下所有文件名 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; } 
 /** * @param fileName * @param sourceFile * @return * @throws IOException * function:下載文件 */
 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; 
} 
/** * 返回一個文件流 * @param fileName * @return */ public String readFile(String fileName){ 
String result = ""; InputStream ins = null; try { ins = ftpClient.retrieveFileStream(fileName);  
// byte []b = new byte[ins.available()]; // ins.read(b); 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(); }  
// 主動調用一次getReply()把接下來的226消費掉. 這樣做是可以解決這個返回null問題
 ftpClient.getReply(); }
catch (IOException e) { e.printStackTrace(); } return result; } 
 /** * @param fileName * @return * function:從伺服器上讀取指定的文件
 * @throws ParseException * @throws IOException 
*/ 

public List readFile() throws ParseException{  List contentList = new ArrayList();
 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");//機構級別為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(); } 
// 主動調用一次getReply()把接下來的226消費掉. 這樣做是可以解決這個返回null問題 ftpClient.getReply();
 System.out.println("此次任務一共讀取["+contentList.size()+"]條數據記錄"); } catch (IOException e) { 
e.printStackTrace(); } return contentList; } 
 /** * @param fileName * function:刪除文件 */ 

public void deleteFile(String fileName){ try { ftpClient.deleteFile(fileName); }
 catch (IOException e) { e.printStackTrace(); } } 

 /** * @param args * @throws ParseException */
 public static void main(String[] args) throws ParseException { 
FtpUtils ftp = new FtpUtils();  ftp.readFile();
 /*ftp.unloadFile("D:\\test_t_department.txt", "t_department_20110623.txt");
 List files = ftp.getFileList(path); for(int i = 0 ; i < files.size() ; i++){ 
String fileName = files.get(i); ftp.unloadFile("D:\\test", "t_department_20110623.txt"); 
System.out.println(fileName); String result = ftp.readFile("t_department_20110623.txt"); 
System.out.println(result); 
ftp.deleteFile(fileName); }
*/ 
 }


推薦閱讀文章

Bookmark the permalink ,來源:互聯網