whyclj
2020-10-23 291d27a34b52911dbcfd8232f0685d34b0fc96a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.dev.data;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Date;
 
import org.apache.log4j.chainsaw.Main;
 
import com.base.Com;
import com.base.ComFn;
 
/**
 * ¼Ç¼UDPÊý¾ÝÖ¡
 * @author LiJun
 *
 */
public class RecordUDPPackage {
    public static final int PackageType_ChildStood    = 1;        //×ÓÕ¾Êý¾Ý°ü
    public static final int PackageType_Battery         = 2;        //Ðîµç³Ø×éÊý¾Ý
    public static final int PackageType_InverPower     = 3;        //Äæ±äµçÔ´Êý¾Ý
    public static final int PackageType_SteamTurbine  = 4;        //ÆûÂÖ·¢µç»úÊý¾Ý
    public static final int PackageType_SwitchBoard    = 5;        //¿ª¹ØÁ¿°åÊý¾Ý
    
    //udp±¨Îı¸·ÝĿ¼
    //public static final String BACKUP_FILEPATH = "D:/tomcat7/webapps/ftpbackup/updbackup/";
    public static final String BACKUP_FILEPATH = "D:/tomcat7/webapps/ftpbackup/updbackup/";
    
    static {
        File file = new File(BACKUP_FILEPATH);
        if(!file.exists()) {
            file.mkdir();
        }
    }
    
    
    /**
     * ¼Ç¼Êý¾ÝÖ¡    
     * @param packagetype
     * @param ip
     * @param arr
     */
    public static void RecordUDPPackageData(int packagetype,String ip,byte[] source) {
        File file = null;
        switch(packagetype) {
            case  PackageType_Battery:{
                file = new File(BACKUP_FILEPATH+"battery_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
            }break;
            case PackageType_ChildStood:{
                file = new File(BACKUP_FILEPATH+"childstood_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
            }break;
            case PackageType_InverPower:{
                file = new File(BACKUP_FILEPATH+"interpower_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
            }break;
            case PackageType_SteamTurbine:{
                file = new File(BACKUP_FILEPATH+"steamturbine_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
            }break;
            case PackageType_SwitchBoard:{
                file = new File(BACKUP_FILEPATH+"switchboard_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
            }break;
        }
        if(null != file) {
            BufferedWriter out = null;
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //System.out.println("¿ªÊ¼Ð´Èë");
            try {
 
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
                out.write(Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+":"+ip+"#"+ComFn.bytesToHexString(source, source.length));
                out.write("\n");
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
    }
    
    /**
     * ¼Ç¼Êý¾ÝÖ¡    
     * @param packagetype
     * @param ip
     * @param arr
     */
    public static void RecordUDPPackageData(int packagetype,String ip,byte[] source,Date time) {
        File file = null;
        switch(packagetype) {
            case  PackageType_Battery:{
                file = new File(BACKUP_FILEPATH+"battery_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
            }break;
            case PackageType_ChildStood:{
                file = new File(BACKUP_FILEPATH+"childstood_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
            }break;
            case PackageType_InverPower:{
                file = new File(BACKUP_FILEPATH+"interpower_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
            }break;
            case PackageType_SteamTurbine:{
                file = new File(BACKUP_FILEPATH+"steamturbine_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
            }break;
            case PackageType_SwitchBoard:{
                file = new File(BACKUP_FILEPATH+"switchboard_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
            }break;
        }
        if(null != file) {
            BufferedWriter out = null;
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //System.out.println("¿ªÊ¼Ð´Èë");
            try {
 
                out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
                out.write(Com.getDateTimeFormat(time, Com.DTF_YMDhms)+":"+ip+"#"+ComFn.bytesToHexString(source, source.length));
                out.write("\n");
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
    }
    
    public static void main(String[] args) {
        for(int i=0;i<10;i++) {
            RecordUDPPackageData(1, "127.0.0.1", "hhehhehe".getBytes());
        }
    }
}