1 /** 2 * 建立FTP鏈接,FTP服務器地址、端口、登陸用戶信息都在配置里配置即可。 3 * @throws IOException 4 */ 5 public boolean connectFtp(String ftpAddress, String ftpPort, String frpUserName, String frpPassword) throws IOException{ 6 log.info("*****連接FTP服務器...*****"); 7 try{ 8 ftpClient.connect(ftpAddress, Integer.valueOf(ftpPort).intValue()); 9 ftpClient.setControlEncoding("GB2312"); 10 int reply = ftpClient.getReplyCode(); 11 if(FTPReply.isPositiveCompletion(reply)){ 12 if(ftpClient.login(frpUserName,frpPassword)){ 13 log.info("*****連接FTP服務器成功!*****"); 14 return true; 15 } 16 }else{ 17 log.error("*****連接失敗!響應代碼為【"+ reply+"】*****"); 18 } 19 disconnect(); 20 }catch (Exception e) { 21 log.error("*****連接失敗:" + e.getMessage()); 22 } 23 return false; 24 } 25 26 /** 27 * 設置FTP客戶端 被動模式、數據模式為二進制、字符編碼GBK 28 */ 29 public void setConnectType(){ 30 try { 31 ftpClient.enterLocalPassiveMode(); 32 ftpClient.setDefaultTimeout(1000 * 120);//120秒 33 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 34 ftpClient.setControlEncoding("GB2312"); 35 } catch (IOException e) { 36 e.printStackTrace(); 37 } 38 } 39 40 41 /** 42 * 斷開與遠程服務器的連接 43 * @throws IOException 44 */ 45 public void disconnect() { 46 if(ftpClient.isConnected()){ 47 try { 48 ftpClient.disconnect(); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 } 54 55 56 /** 57 * 過濾不符合的文件並批量下載 58 * @param remoteFileReg 文件前綴的正則表達式 59 * @param localPath 本地路徑 .property 文件配置 60 * @param remote_down_path ftp文件路徑 61 * @return List 下載到本地的文件路徑 集合 62 * @throws IOException 63 */ 64 @SuppressWarnings("unchecked") 65 public List downloads(String remoteFileReg,String localPath,String remote_down_path) throws IOException{ 66 List<String> fileNames = new ArrayList<String>(); 67 log.info("*****轉移到服務器目錄:" + remote_down_path); 68 setConnectType(); 69 boolean changeFlag = ftpClient.changeWorkingDirectory(remote_down_path); 70 FTPFile[] files = ftpClient.listFiles(); 71 //String[] names = ftpClient.listNames(); 72 log.info("*****改變目錄是否成功:" + changeFlag); 73 log.info("*****服務器上report目錄下所有校驗報告的文件數為:【" +files.length + "】" ); 74 if(files.length == 0){ 75 log.info("*****未在服務器上找到文件!*****"); 76 return null; 77 }else{//目錄下有文件 78 //把 bak文件的前綴找出來 ,區分讀取和未讀取的xls 和 xlsx ,只下載 未讀取的文件 79 List<String> bakList = new ArrayList<String>(); 80 List<String> list = new ArrayList<String>(); 81 82 for (int i = 0; i < files.length; i++) { 83 FTPFile ftpFile = files[i]; 84 String fileName = ftpFile.getName(); 85 86 if(!fileName.endsWith(".bak") && ftpFile == null){ 87 log.info("******* "+ fileName + "文件無數據!"); 88 continue; 89 } 90 //匹配指定的文件前綴 和后綴 為 .bak 格式的文件 91 //bak 文件是文件讀取完畢后生成的標記文件 92 Pattern bak = Pattern.compile("^"+remoteFileReg+"\\.bak"); 93 Matcher m = bak.matcher(fileName); 94 if (m.find()) { 95 //取.bak文件的 前綴 96 //System.out.println(fileName); 97 //System.out.println(fileName.split("\\.")[0]); 98 bakList.add(fileName.split("\\.")[0]); 99 continue; 100 } 101 102 //匹配指定的文件前綴 和后綴 為 .xls .xlsx 格式的文件 103 //TODO 以后遇到其他的格式文件 需要把后綴抽出來作為參數傳入 104 Pattern xls = Pattern.compile("^"+remoteFileReg+"\\.xls$"+"|"+"^"+remoteFileReg+"\\.xlsx$"+"|"+"^"+remoteFileReg+"\\.csv$"); 105 Matcher mm = xls.matcher(fileName); 106 if(mm.find()){ 107 list.add(fileName); 108 continue; 109 } 110 } 111 112 Iterator<String> it = list.iterator(); 113 while (it.hasNext()) { 114 String xls = it.next(); 115 for (int i = 0; i < bakList.size(); i++) { 116 String bak = bakList.get(i); 117 //bak文件存在 , 去掉此文件 118 if (xls.indexOf(bak) !=-1) { 119 it.remove(); 120 bakList.remove(i); 121 } 122 } 123 } 124 125 126 for (String fFile : list) { 127 //下載未讀取的文件 128 File downFile = new File(localPath + fFile); 129 //System.out.println(localPath); 130 File downPath = new File(localPath); 131 if(!downPath.exists()){ 132 downPath.mkdirs(); 133 } 134 String fileDir = remote_down_path + fFile; 135 OutputStream os = new FileOutputStream(downFile); 136 ftpClient.retrieveFile(new String(fileDir.getBytes("GB2312"),"ISO-8859-1"), os); 137 log.info("*****文件已下載到:" + downFile.getAbsolutePath() + "******"); 138 fileNames.add(downFile.getAbsolutePath()); 139 os.close(); 140 } 141 log.info("**** 此次共下載了【"+list.size()+"】個文件! *****"); 142 } 143 return fileNames; 144 } 145 146 /** 147 * 上傳標志文件 148 * @param remoteFile 149 * @param localFile 150 * @return 151 */ 152 public boolean upload(String localFileName,String remoteFileName){ 153 154 boolean b = false; 155 try { 156 File file = new File(localFileName); 157 FileInputStream input = new FileInputStream(file); 158 b = ftpClient.changeWorkingDirectory(remoteFileName); 159 log.info("*****改變目錄是否成功:" + b); 160 String remoteFile = remoteFileName + file.getName(); 161 b = ftpClient.storeFile(new String(remoteFile.getBytes("GB2312"),"ISO-8859-1"), input); 162 if(b){ 163 log.info(" ****** 標志文件"+localFileName+"上傳成功!"); 164 } 165 input.close(); 166 } catch (FileNotFoundException e) { 167 e.printStackTrace(); 168 } catch (IOException e) { 169 e.printStackTrace(); 170 } 171 return b; 172 } 173