ftp 文件读取和解析入库

  1. package com.longtop.ecommerce.service.dept;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.InputStreamReader;  
  9. import java.net.SocketException;  
  10. import java.text.DateFormat;  
  11. import java.text.ParseException;  
  12. import java.text.SimpleDateFormat;  
  13. import java.util.ArrayList;  
  14. import java.util.Date;  
  15. import java.util.List;  
  16.   
  17. import org.apache.commons.net.ftp.FTPClient;  
  18. import org.apache.commons.net.ftp.FTPFile;  
  19.   
  20. import com.longtop.ecommerce.bo.Organization;  
  21.   
  22. /** 
  23. * 从FTP读取文件 
  24. * @author tony 
  25. */  
  26. public class FtpUtils {  
  27.   
  28.   
  29. private FTPClient ftpClient;  
  30. private String fileName, strencoding;  
  31. private int columns, rowCount;  
  32. private String ip = "65.0.15.26"; //服务器IP地址  
  33. private String userName = "anonymous"; //用户名  
  34. private String userPwd = "anonymous"; //密码  
  35. private int port = 21; //端口号  
  36. private String path = "/aaa/CIC_Department/"; //读取文件的存放目录  
  37. /** 
  38. * init ftp servere 
  39. */  
  40. public FtpUtils() {  
  41. this.reSet();  
  42. }  
  43. public void reSet(){  
  44. //以当前系统时间拼接文件名  
  45. fileName = "t_department_"+getFileName()+".txt";  
  46. columns = 0;  
  47. rowCount = 0;  
  48. strencoding = "GBK";  
  49. this.connectServer(ip, port, userName, userPwd, path);  
  50. }  
  51.   
  52. /** 
  53. * 以当前系统时间生成文件名 
  54. * @return 
  55. */  
  56. private String getFileName(){  
  57. SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMdd");  
  58. String str = "";  
  59. try {  
  60. str = sdFormat.format(new Date());  
  61. }  
  62. catch(Exception e) {  
  63. return "";  
  64. }  
  65. if (str.equals("1900-01-01")) {  
  66. str = "";  
  67. }  
  68.   
  69. return str;  
  70. }  
  71.   
  72.   
  73. /** 
  74. * @param ip 
  75. * @param port 
  76. * @param userName 
  77. * @param userPwd 
  78. * @param path 
  79. * @throws SocketException 
  80. * @throws IOException 
  81. * function:连接到服务器 
  82. */  
  83. public void connectServer(String ip , int port , String userName , String userPwd , String path){  
  84. ftpClient = new FTPClient();  
  85. try {  
  86. // 连接  
  87. ftpClient.connect(ip, port);  
  88. // 登录  
  89. ftpClient.login(userName, userPwd);  
  90. if(path != null && path.length() > 0){  
  91. // 跳转到指定目录  
  92. ftpClient.changeWorkingDirectory(path);  
  93. }  
  94. catch (SocketException e) {  
  95. e.printStackTrace();  
  96. catch (IOException e) {  
  97. e.printStackTrace();  
  98. }  
  99. }  
  100.   
  101. /** 
  102. * @throws IOException 
  103. * function:关闭连接 
  104. */  
  105. public void closeServer(){  
  106. if(ftpClient.isConnected()){  
  107. try {  
  108. ftpClient.logout();  
  109. ftpClient.disconnect();  
  110. catch (IOException e) {  
  111. e.printStackTrace();  
  112. }  
  113. }  
  114. }  
  115.   
  116. /** 
  117. * @param path 
  118. * @return 
  119. * function:读取指定目录下的文件名 
  120. * @throws IOException 
  121. */  
  122. public List<String> getFileList(String path){  
  123. List<String> fileLists = new ArrayList<String>();  
  124. // 获得指定目录下所有文件名  
  125. FTPFile[] ftpFiles = null;  
  126. try {  
  127. ftpFiles = ftpClient.listFiles(path);  
  128. catch (IOException e) {  
  129. e.printStackTrace();  
  130. }  
  131. for(int i = 0 ; ftpFiles != null && i < ftpFiles.length ; i++){  
  132. FTPFile file = ftpFiles[i];  
  133. if(file.isFile()){  
  134. fileLists.add(file.getName());  
  135. }  
  136. }  
  137. return fileLists;  
  138. }  
  139.   
  140. /** 
  141. * @param fileName 
  142. * @param sourceFile 
  143. * @return 
  144. * @throws IOException 
  145. * function:下载文件 
  146. */  
  147. public boolean unloadFile(String fileName , String sourceFile){  
  148. boolean flag = false;  
  149. try{  
  150. FileOutputStream fos = new FileOutputStream(fileName);  
  151. flag = ftpClient.retrieveFile(sourceFile, fos);  
  152. fos.flush();  
  153. fos.close();  
  154. }catch(Exception e){  
  155. flag = false;  
  156. e.printStackTrace();  
  157. }  
  158. return flag;  
  159. }  
  160. /** 
  161. * 返回一个文件流 
  162. * @param fileName 
  163. * @return 
  164. */  
  165. public String readFile(String fileName){  
  166. String result = "";  
  167. InputStream ins = null;  
  168. try {  
  169. ins = ftpClient.retrieveFileStream(fileName);  
  170.   
  171. // byte []b = new byte[ins.available()];  
  172. // ins.read(b);  
  173. BufferedReader reader=new BufferedReader(new InputStreamReader(ins));  
  174. String inLine = reader.readLine();  
  175. while (inLine != null) {  
  176. result += (inLine + System.getProperty("line.separator"));  
  177. inLine = reader.readLine();  
  178. }  
  179. reader.close();  
  180. if(ins != null){  
  181. ins.close();  
  182. }  
  183.   
  184. // 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题  
  185. ftpClient.getReply();  
  186. catch (IOException e) {  
  187. e.printStackTrace();  
  188. }  
  189. return result;  
  190. }  
  191.   
  192. /** 
  193. * @param fileName 
  194. * @return 
  195. * function:从服务器上读取指定的文件 
  196. * @throws ParseException 
  197. * @throws IOException 
  198. */  
  199. public List readFile() throws ParseException{  
  200.   
  201. List<Organization> contentList = new ArrayList<Organization>();  
  202. InputStream ins = null;  
  203. try {  
  204. //从服务器上读取指定的文件  
  205. ins = ftpClient.retrieveFileStream(fileName);  
  206.   
  207. BufferedReader reader=new BufferedReader(new InputStreamReader(ins,strencoding));  
  208.   
  209. String inLine = reader.readLine();  
  210.   
  211. while (inLine != null) {  
  212. if (inLine.length() + 1 > columns)  
  213. columns = inLine.length() + 1;  
  214. String beanStr = inLine+System.getProperty("line.separator");  
  215. if(beanStr.indexOf(",")>0){  
  216. String[] beanStrs = beanStr.split(",");  
  217. Organization org = new Organization();  
  218. DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");  
  219. Date date = null;  
  220. date = format1.parse(beanStrs[0].substring(1, beanStrs[0].length()-1));  
  221. org.setBuildDate(date);  
  222. String parentId = null;  
  223. if((beanStrs[1].substring(1, beanStrs[1].length()-1)).equals("")){  
  224. parentId = "00";//默认值,表示顶级机构  
  225. org.setOrgLevel("00");//机构级别为00  
  226. }else parentId = (beanStrs[1].substring(1, beanStrs[1].length()-1));  
  227. org.setParentId(parentId);//去掉引号  
  228. org.setOrgCode(beanStrs[2].substring(1, beanStrs[2].length()-1));  
  229. org.setOrgBrief(beanStrs[3].substring(1, beanStrs[3].length()-1));  
  230. if(beanStrs[4].length()>3){  
  231. org.setOrgName(beanStrs[4].substring(1, beanStrs[4].length()-3));  
  232. }  
  233. contentList.add(org);  
  234. }  
  235.   
  236. inLine = reader.readLine();  
  237. rowCount++;  
  238. }  
  239. reader.close();  
  240. if(ins != null){  
  241. ins.close();  
  242. }  
  243. // 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题  
  244. ftpClient.getReply();  
  245. System.out.println("此次任务一共读取["+contentList.size()+"]条数据记录");  
  246. catch (IOException e) {  
  247. e.printStackTrace();  
  248. }  
  249. return contentList;  
  250. }  
  251.   
  252. /** 
  253. * @param fileName 
  254. * function:删除文件 
  255. */  
  256. public void deleteFile(String fileName){  
  257. try {  
  258. ftpClient.deleteFile(fileName);  
  259. catch (IOException e) {  
  260. e.printStackTrace();  
  261. }  
  262. }  
  263.   
  264. /** 
  265. * @param args 
  266. * @throws ParseException 
  267. */  
  268. public static void main(String[] args) throws ParseException {  
  269. FtpUtils ftp = new FtpUtils();  
  270.   
  271. ftp.readFile();  
  272. /*ftp.unloadFile("D:\\test_t_department.txt", "t_department_20110623.txt"); 
  273. List<String> files = ftp.getFileList(path); 
  274. for(int i = 0 ; i < files.size() ; i++){ 
  275. String fileName = files.get(i); 
  276. ftp.unloadFile("D:\\test", "t_department_20110623.txt"); 
  277. System.out.println(fileName); 
  278. String result = ftp.readFile("t_department_20110623.txt"); 
  279. System.out.println(result); 
  280. ftp.deleteFile(fileName); 
  281. }*/  
  282.   
posted @ 2017-02-08 11:34  zhuzhuxuan  阅读(1417)  评论(0编辑  收藏  举报