首页 > 代码库 > FTP读取文件及防止防火墙自动挂起设置

FTP读取文件及防止防火墙自动挂起设置

具体代码如下:

 1 /** 2      * ftp取天气网信息,参数为ftp地址、账号、密码 3      * @param ip 4      * @param port 5      * @param user 6      * @param pswd 7      * @param path  文件路径 8      * @return 9      */10     public List<JsonWeatherInfo> getHttpXMLByFTP(String ip,int port,String user,String pswd,String path){11         12         FTPClient ftpClient = new FTPClient();13          try {14            // 连接15            ftpClient.connect(ip, port) ;16            // 登录17            ftpClient.login(user, pswd);18            if (path != null && path.length() > 0) {19             // 跳转到指定目录20             ftpClient.changeWorkingDirectory(path);21            }22         }catch (SocketException e) {23               e.printStackTrace();24         }catch (IOException e) {25               e.printStackTrace();26          }    27         //设置FTPClient为被动传输模式即可解决线程挂起问题,防火墙问题28         ftpClient.enterLocalPassiveMode();  29         30         logger.info("【连接成功,进行取值】");31         //根据当前时间,得到需要取的文件名称32         String dateStr = DateUtil.getFormatDate(DateUtil.getNowDate(),"yyyyMMdd HH:mm:ss");33         String ymd = dateStr.substring(0, 8);34         String hhmm = dateStr.substring(9, 15).replace(":", "");35         int hhmmi = Integer.parseInt(hhmm) ;36         String zsymd = ymd ;37         int zshhmmi = hhmmi ;38         39         String hzStr = "800" ;40         if(hhmmi<805){41             //早上8点5分前,取上一天的42             ymd =  DateUtil.getLastDay("yyyyMMdd HH:mm:ss").substring(0, 8);43             hzStr = "2000";44         }else if (hhmmi<1205){45             //取早8点的46             hzStr = "0800";47         }else if (hhmmi<1715){48             //取中午12点的49             hzStr = "1200";50         }else if(hhmmi <=2400){51             //取下午17点10后的52             hzStr = "2000";53         }54         55         String zsf = "07" ;56         if(zshhmmi<705){57         //取前天的指数58             zsymd = DateUtil.getLastDay("yyyyMMdd HH:mm:ss").substring(0, 8);59             zsf = "17" ;60         }else if(zshhmmi<1705){61             zsf = "07" ;62         }else if(zshhmmi<=2400){63             zsf = "17" ;64         }65         66         //文件名称67         String weatherFName = "gn_360_"+ymd+hzStr+".txt" ;68         String ziwaixianFName = zsymd+zsf+"ziwaixian.txt" ;69         String chuanyiFName = zsymd+zsf+"chuanyi.txt" ;70         String xicheFName = zsymd+zsf+"xiche.txt" ;71         String ganmaoFName = zsymd+zsf+"ganmao.txt" ;72         String yundongFName = zsymd+zsf+"yundong.txt" ;73         74         logger.info("【文件名:】"+weatherFName+"##"+ziwaixianFName+"##"+chuanyiFName+"##"+xicheFName+"##"+ganmaoFName+"##"+yundongFName+"##");75         76         String weatherContent = getStrFromFTP(ftpClient,weatherFName) ;77         String ziwaixianContent = getStrFromFTP(ftpClient,ziwaixianFName) ;78         String chuanyiContent = getStrFromFTP(ftpClient,chuanyiFName) ;79         String xicheContent = getStrFromFTP(ftpClient,xicheFName) ;80         String ganmaoContent = getStrFromFTP(ftpClient,ganmaoFName) ;81         String yundongContent = getStrFromFTP(ftpClient,yundongFName) ;82         83         //将指数指标存储到Map中,key=天气城市id,value=http://www.mamicode.com/提供了几天的数据,每天区分以#区分,几天不确定84         logger.info("【取值成功,进行转化到MAP】");85         HashMap ziwaixianMap = covertZBContentToMap(ziwaixianContent);86         HashMap chuanyiMap = covertZBContentToMap(chuanyiContent);87         HashMap xicheMap = covertZBContentToMap(xicheContent);88         HashMap ganmaoMap = covertZBContentToMap(ganmaoContent);89         HashMap yundongMap = covertZBContentToMap(yundongContent);90         91         logger.info("【取值成功,进行逻辑处理】");92 }

使用到的2个方法如下:

 1     /* 2      * 获取ftp上指定文件的内容 3      */ 4     private String getStrFromFTP(FTPClient ftpClient,String fileName){ 5         logger.info("进入取值方法:"+ftpClient.toString()+"fileName:"+fileName); 6          7         InputStream ins = null; 8           StringBuilder builder = null; 9           try {10                // 从服务器上读取指定的文件11               logger.info("进入取值方法try");12               //设置FTPClient为被动传输模式即可解决线程挂起问题,防火墙问题13                 ftpClient.enterLocalPassiveMode();  14                 // 设置以二进制形式传输15                 ftpClient.setFileType(FTP.BINARY_FILE_TYPE);16                ins = ftpClient.retrieveFileStream(fileName);17                BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "gbk"));18                logger.info("进入取值方法try2");19                String line;20                builder = new StringBuilder(150);21                while ((line = reader.readLine()) != null) {22                 System.out.println(line);23                 logger.info("line:"+line);24                 builder.append(line).append("==");25                }26                reader.close();27                if (ins != null) {28                 ins.close();29                }30                // 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题31                logger.info("进入取值方法Reply前");32                ftpClient.getReply();33                logger.info("进入取值方法Reply后");34           } catch (Exception e) {35               logger.info("【ftp上指定文件的内容出错,文件名为:】"+fileName);36                e.printStackTrace();37           }38           logger.info("取值方法完毕,内容:"+builder.toString());39           return builder.toString();40     }41     42     /*43      * 指标数组转化到Map,key=城市id,value=http://www.mamicode.com/提供了几天的数据,每天区分以#区分,几天不确定>44      */45     private HashMap covertZBContentToMap(String content){46         HashMap map = new HashMap();47         String[] ContentArr = content.split("==") ; 48         for(int i=0;i<ContentArr.length;i++){49             String[] vauleArr = ContentArr[i].split("\\|\\|") ;50             String key = vauleArr[1].trim() ;51             if(map.containsKey(key)){52                 //如果已存储了key,则表示多天的数据, 则把数据拼接,连接符#53                 String temp = (String)map.get(key) + "#" + ContentArr[i] ;54                 map.remove(key);55                 map.put(key, temp) ;56             }else{57                 map.put(key, ContentArr[i]) ;58             }59         }60         logger.info("map长度:"+map.size());61         return map ; 62     }