zoukankan      html  css  js  c++  java
  • ftp 根据特定正则匹配文件名 下载到本地 并且上传文件到ftp java *** 最爱那水货

      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     
  • 相关阅读:
    YTU 2543: 数字整除
    YTU 2542: 弟弟的作业
    YTU 2541: 汽水瓶
    YTU 2535: C++复数运算符重载(+与<<)
    YTU 2530: 小勇玩lol
    YTU 2520: 小慧唱卡拉OK
    YTU 2517: 打倒魔王↖(^ω^)↗
    YTU 2516: 剪刀石头布
    reload、replace、href、assign、window.history.go(0)的区别
    js 数组排序sort方法
  • 原文地址:https://www.cnblogs.com/chinazhou-wang/p/5910042.html
Copyright © 2011-2022 走看看