package com.dev.data; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TimeZone; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPReply; import com.base.Com; import com.config.AppConfig; /** * ¶ÁÈ¡ÅäÖÃÎļþ * @author LiJun * */ public class ReadConfigFile { //±¾µØÅäÖÃÎļþĿ¼[ΪÁ˽â¾ö¿Í»§¶ËÆô¶¯Ê±²»ÄÜÁ¬½Óµ½ftp·þÎñÆ÷ʱbug] public static String cfgpath = System.getProperty("user.dir") + File.separator + "config/RealList.txt"; //ϵͳÀàÐÍ public static final int ZIZHAN_SYS_TYPE = 1; public static final int ZHONGDUAN_SYS_TYPE = 2; private static String FileRoot = "/config/"; //Êý¾Ý¸ùĿ¼ public String battName = ""; public FTPClient ftp; public String filename = "RealList.txt"; public String server_ip = "192.168.7.130"; //É豸ipµØÖ· public String userNmae = "ftp"; //ĬÈÏftpÓû§Ãû public String passWord = ""; //ĬÈÏÓû§ÃÜÂë public int port = 21; //ĬÈÏ¶Ë¿Ú public Map sysTypes = new HashMap(){ { put(ZIZHAN_SYS_TYPE,"×Óվϵͳ"); put(ZHONGDUAN_SYS_TYPE,"ÖÕ¶Ë"); } }; public ReadConfigFile() { } public List readConfigData() { List configs = new ArrayList(); boolean flag = false; try { System.out.println(cfgpath); if(ftpLogin(server_ip, port, userNmae, passWord)) { flag = true; judeFileExists(cfgpath); parseConfigFile(configs); } //System.err.println(configs.size()); if(configs.size()<1){ System.out.println("½âÎö±¾µØÅäÖÃÎļþ¡£¡£¡£"); //µÇ½ftp·þÎñÆ÷ʧ°Ü,δ½âÎöµ½ÅäÖÃÎļþ,½âÎö±¾µØÎļþ parseLocalConfigFile(configs); }else { System.out.println("½âÎöÅäÖÃÎļþ³É¹¦"+configs.size()); } } catch (Exception e) { e.printStackTrace(); } finally { try { //½âÎöÍê³ÉÍ˳öftpÁ¬½Ó disConnection(); } catch (IOException e) { e.printStackTrace(); } if(flag) { System.out.println("µÇ½ftp·þÎñÆ÷³É¹¦ip:"+server_ip+";file:"+FileRoot+filename+";name:"+userNmae+";pass:"+passWord+";port:"+port+"; at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)); }else { System.out.println("µÇ½ftp·þÎñÆ÷ʧ°Üip:"+server_ip+";file:"+FileRoot+filename+";name:"+userNmae+";pass:"+passWord+";port:"+port+"; at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)); } } return configs; } /** * ¶ÁÈ¡FTPÅäÖÃÎļþ * @param temp */ public void parseConfigFile(List temp) { if (FileRoot.startsWith("/") && FileRoot.endsWith("/")) { //¸ü»»Ä¿Â¼µ½µ±Ç°Ä¿Â¼ StringBuilder stringBuilder; InputStream inputStream = null; OutputStream outputStream = null; BufferedReader bufferedReader = null; BufferedWriter bufferedWrite = null; try { this.ftp.changeWorkingDirectory(FileRoot); // ÐèÒª¼Ó´ËÅжϡ£·ñÔò£¬ftpĬÈϽ«¡®ÏîÄ¿ÎļþËùÔÚĿ¼֮ϵÄĿ¼£¨./£©¡¯Óë¡®ÏîÄ¿ÎļþËùÔÚĿ¼ÏòÉÏÒ»¼¶Ä¿Â¼ÏµÄĿ¼£¨../£©¡¯¶¼ÄÉÈëµÝ¹é£¬ÕâÑùÏÂÈ¥¾ÍÏÝÈëÒ»¸öËÀÑ­»·ÁË¡£Ð轫Æä¹ýÂ˵ô¡£ inputStream = this.ftp.retrieveFileStream(filename);//¸ù¾ÝÖ¸¶¨Ãû³Æ»ñȡָ¶¨Îļþ bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"GBK")); outputStream = new FileOutputStream(new File(cfgpath)); bufferedWrite = new BufferedWriter(new OutputStreamWriter(outputStream,"GBK")); String line=null; stringBuilder = new StringBuilder(150); int count = 0; while ((line = bufferedReader.readLine()) != null) { bufferedWrite.write(line); bufferedWrite.newLine(); bufferedWrite.flush(); stringBuilder.delete( 0, stringBuilder.length() ); stringBuilder.append(line); readConfigTxt(stringBuilder,temp); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(bufferedReader != null) { bufferedReader.close(); } if(inputStream != null) { inputStream.close(); } if(bufferedWrite != null) { bufferedWrite.close(); } if(outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } } /** * ¶ÁÈ¡±¾µØÅäÖÃÎļþ * @param temp */ public void parseLocalConfigFile(List temp) { File file = new File(cfgpath); System.out.println(cfgpath); if (file.exists()) { //¸ü»»Ä¿Â¼µ½µ±Ç°Ä¿Â¼ StringBuilder stringBuilder; InputStream inputStream = null; BufferedReader bufferedReader = null; try { // ÐèÒª¼Ó´ËÅжϡ£·ñÔò£¬ftpĬÈϽ«¡®ÏîÄ¿ÎļþËùÔÚĿ¼֮ϵÄĿ¼£¨./£©¡¯Óë¡®ÏîÄ¿ÎļþËùÔÚĿ¼ÏòÉÏÒ»¼¶Ä¿Â¼ÏµÄĿ¼£¨../£©¡¯¶¼ÄÉÈëµÝ¹é£¬ÕâÑùÏÂÈ¥¾ÍÏÝÈëÒ»¸öËÀÑ­»·ÁË¡£Ð轫Æä¹ýÂ˵ô¡£ inputStream = new FileInputStream(file);//¸ù¾ÝÖ¸¶¨Ãû³Æ»ñȡָ¶¨Îļþ bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"GBK")); String line=null; stringBuilder = new StringBuilder(150); int count = 0; while ((line = bufferedReader.readLine()) != null) { stringBuilder.delete( 0, stringBuilder.length() ); stringBuilder.append(line); readConfigTxt(stringBuilder,temp); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(bufferedReader != null) { bufferedReader.close(); } if(inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } } /** * ½âÎö×Ö·û´®ÖеÄÎļþÐÅÏ¢ * @param str * @param devices */ public void readConfigTxt(StringBuilder str,List devices) { if(null != str) { /* ÕÒ³öÖ¸¶¨µÄ2¸ö×Ö·ûÔÚ ¸Ã×Ö·û´®ÀïÃæµÄ λÖà */ int StartIndex = str.indexOf("@"); int EndIndex = str.lastIndexOf("$"); if(StartIndex >= 0 && EndIndex >=0 && str.length() > 18) { String[] s = str.substring(StartIndex+1,EndIndex).split(","); ConfigData temp = new ConfigData(); temp.dev_number = Integer.parseInt(s[0].trim()); //Êý¾Ý±àºÅ temp.data_value = s[1].trim(); //Êý¾ÝÃû³Æ temp.unit = s[2].trim(); //µ¥Î» temp.value_min = Float.parseFloat(s[3]); //×îСֵ temp.value_max = Float.parseFloat(s[4]); //×î´óÖµ temp.dataacc = Float.parseFloat(s[5]); //¾«¶È(1->ÕûÊý£»10->0.1;100->0.01) temp.alm_num1 = Integer.parseInt(s[6].trim()); //±¨¾¯1±àºÅ temp.alm_num1_value = Float.parseFloat(s[7]); //±¨¾¯1¸æ¾¯Öµ temp.alm_num2 = Integer.parseInt(s[8].trim()); //±¨¾¯2±àºÅ temp.alm_num2_value = Float.parseFloat(s[9]); //±¨¾¯2¸æ¾¯Öµ temp.varname = s[10].trim(); //±äÁ¿Ãû³Æ //System.out.println(temp.dev_number+"====="+temp.varname); if(temp.varname.indexOf(":") >0) { temp.vname = temp.varname.substring(0, temp.varname.indexOf(":")); //É豸±êʶ·û } temp.macsource = s[11].trim(); //ʵʱÊý¾Ý¿âËù´¦¼ÆËã»ú temp.datasource = s[12].trim(); //Êý¾ÝÔ´ temp.dataaccess = s[13].trim(); //»ñȡ;¾¶ temp.vartype = Integer.parseInt(s[14].trim()); //±äÁ¿ÀàÐÍ(1¡ª>Ä£ÄâÁ¿;2->¿ª¹ØÁ¿;3->״̬Á¿) temp.wanlogo = s[15].trim(); //ÒÔÌ«Íø±êʶ temp.frameposit = Integer.parseInt(s[16].trim()); //Ö¡ÖÐλÖà temp.defalutval = Float.parseFloat(s[17].trim()); //ĬÈÏÖµ temp.codename = s[18].trim(); //´úºÅ devices.add(temp); //System.out.println(temp); } } } //·ÖÎöÅäÖÃÎļþÖеĽṹ public List analyConfigFile(List source,List devices,List cfgs) { //List devices = new ArrayList(); boolean flag = false; int dev_id = 1001; int sys_type = 1; System.out.println(source.size()); for(int i = 0;i 0) { this.ftp.connect(strIp, intPorts); }else { this.ftp.connect(strIp); } // FTP·þÎñÆ÷Á¬½Ó»Ø´ð int reply = this.ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { this.ftp.disconnect(); return isLogin; } this.ftp.login(username, password); // ÉèÖô«ÊäЭÒé this.ftp.enterLocalPassiveMode(); this.ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //System.out.println(this.ftpClient.getSystemType()); // »ñÈ¡µÇ¼ÐÅÏ¢ //FTPClientConfig config = new FTPClientConfig(this.ftpClient.getSystemType().split(" ")[0]); FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_OS2); config.setServerLanguageCode("zh"); this.ftp.configure(config); // ʹÓñ»¶¯Ä£Ê½ÉèΪĬÈÏ //this.ftpClient.enterLocalPassiveMode(); //System.out.println("999999999999999"); isLogin = true; }catch (Exception e) { isLogin = false; e.printStackTrace(); } //this.ftpClient.setBufferSize(1024 * 800); //this.ftpClient.setDataTimeout(120 * 1000); return isLogin; } /** * ¹Ø±ÕÊý¾ÝÁ´½Ó * * @throws IOException */ public void disConnection() throws IOException { if (this.ftp.isConnected()) { this.ftp.disconnect(); } } // ÅжÏÎļþÊÇ·ñ´æÔÚ public static void judeFileExists(String filepath) { File file = new File(filepath); if (file.exists()) { //ÎļþÒÑ´æÔÚ } else { //System.out.println("file not exists, create it ..."); try { File fileParent = file.getParentFile(); if(!fileParent.exists()){ fileParent.mkdirs(); } file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { //ReadConfigFile util = new ReadConfigFile(); //util.readBattTestData("Default_Battery","F2017-01-14 18.40.44.FBO"); } }