whyclj
2019-10-22 fa4b2e971f642840f21a15db2a2abcf4b5c54513
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package com.ftpfile;
 
import android.util.Log;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
 
 
public class FTPFileUtil {
    public static final String TAG = "FTPFileUtil";
 
    private static String FileRoot = "/pub/USER_DATA/";                //数据根目录
    public String battName = "";
    public FTPClient ftp;
    public ArrayList<String> arFiles;
 
    public String server_ip = "192.168.7.130";        //设备ip地址
    public String userNmae  = "ftp";                //默认ftp用户名
    public String passWord  = "";                    //默认用户密码
    public int port = 21;                            //ftp默认端口
 
 
    /**
     * 读取所有的电池组
     * @return
     */
    public List<MyFTPFile> readAllBattt(String server_ip) {
        List<MyFTPFile> files = new ArrayList<>();
        Integer parentId=1;//根目录
        Integer id=2;//变量需要
        try {
            if(login(server_ip, port, userNmae, passWord)) {
                Log.e(TAG, "readAllBattt: &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&" );
                ListFileRoot(FileRoot, files, 1, 1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(ftp != null) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Log.e(TAG, "readAllBattt: #####################"+files.size() );
        return files;
    }
 
    /**
     * 获取当前电池组所有的历史放电数据
     * @return
     */
    public List<MyFTPFile> readAllTestData(String battName) {
        List<MyFTPFile> files = new ArrayList<>();
        Integer parentId=1;//根目录
        Integer id=2;//变量需要
        try {
 
            System.out.println(FileRoot+battName+"//");
            if(login(server_ip, port, userNmae, passWord)) {
                ListBattTestData(FileRoot+battName+"/", files, 1, 1);
            }
            disConnection();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return files;
 
    }
 
    /**
     * 解析指定的放电数据
     */
    public void readBattTestData(String battName,String fileName) {
        try {
            if(login(server_ip, port, userNmae, passWord)) {
                //ReadBattTestData(FileRoot+battName+"/",fileName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 重载构造函数
     *
     *
     */
    public FTPFileUtil() {
        ftp = new FTPClient();
        arFiles = new ArrayList<String>();
//      if (isPrintCommmand) {
//          ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
//      }
    }
 
    /**
     * 登陆FTP服务器
     *
     * @param host     FTPServer IP地址
     * @param port     FTPServer 端口
     * @param username FTPServer 登陆用户名
     * @param password FTPServer 登陆密码
     * @return 是否登录成功
     * @throws IOException
     */
    public boolean login(String host, int port, String username, String password) throws IOException {
        this.ftp.connect(host, port);
        if (FTPReply.isPositiveCompletion(this.ftp.getReplyCode())) {
            if (this.ftp.login(username, password)) {
                /**
                 需要注意这句代码,如果调用List()方法出现,文件的无线递归,与真实目录结构不一致的时候,可能就是因为转码后,读出来的文件夹与正式文件夹字符编码不一致所导致。
                 则需去掉转码,尽管递归是出现乱码,但读出的文件就是真实的文件,不会死掉。等读完之后再根据情况进行转码。
                 如果ftp部署在windows下,则:
                 for (String arFile : f.arFiles) {
                 arFile = new String(arFile.getBytes("iso-8859-1"), "GBK");
                 logger.info(arFile);
                 }
                 */
                ftp.enterLocalPassiveMode();        //设置成被动模式
                this.ftp.setControlEncoding("utf-8");
                return true;
            }
        }
        if (this.ftp.isConnected()) {
            this.ftp.disconnect();
        }
        return false;
    }
 
    /**
     * 关闭数据链接
     *
     * @throws IOException
     */
    public void disConnection() throws IOException {
        if (this.ftp.isConnected()) {
            this.ftp.disconnect();
        }
    }
 
    public List<MyFTPFile>  setFileAttributeList(List<MyFTPFile> fileAttributeList,MyFTPFile fileAttribute,FTPFile file,String filePath){
        fileAttribute.setFileName(file.getName());
        if (file.getName().indexOf(".")>0){
            fileAttribute.setFileType(file.getName().substring(file.getName().lastIndexOf(".")+1));
        }
        fileAttribute.setFileSize(file.getSize());
        fileAttribute.setFileCreateTime(file.getTimestamp().getTime());
        fileAttribute.setFilePath(filePath+file.getName());
        fileAttribute.setCreateTime(new Date());
        fileAttributeList.add(fileAttribute);
        return fileAttributeList;
    }
 
 
 
    /**
     * 递归遍历出目录下面所有文件
     *
     * @param pathName 需要遍历的目录,必须以"/"开始和结束
     * @throws IOException
     */
    public void ListFileRoot(String pathName, List<MyFTPFile> fileAttributeList,Integer parentId,Integer id) throws IOException {
        if (pathName.startsWith("/") && pathName.endsWith("/")) {
            //更换目录到当前目录
            this.ftp.changeWorkingDirectory(pathName);
            FTPFile[] files = this.ftp.listFiles();
            List<FileAttribute> dirfileAttributeList =new ArrayList<>();
            for (FTPFile file : files) {
                //System.out.println("12");
                MyFTPFile  fileAttribute=new MyFTPFile();
                fileAttribute.setParentId(parentId);
                if (file.isFile()) {
 
                    fileAttribute.setIsfile(1);
                    //组装文件类
                    // setFileAttributeList(fileAttributeList,fileAttribute,file,pathName);
                    //System.out.println(file.getRawListing());
                    // arFiles.add(pathName + file.getName());
                } else if (file.isDirectory()) {
                    id++;
                    //System.out.println(file.getRawListing());
                    fileAttribute.setFileId(id);
                    fileAttribute.setFileName(file.getName());
                    fileAttribute.setIsfile(0);
                    fileAttribute.setFileCreateTime(file.getTimestamp().getTime());
                    fileAttribute.setFilePath(pathName+file.getName());
                    fileAttribute.setCreateTime(new Date());
                    fileAttributeList.add(fileAttribute);
                    //dirfileAttributeList.add(fileAttribute);
 
                }
            }
 
            if (dirfileAttributeList!=null&&dirfileAttributeList.size()>0){
                for ( FileAttribute  fileAttribute :dirfileAttributeList){
                    //save(fileAttribute);保存数据库返回主键id
                    parentId=fileAttribute.getFileId();
                    // 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。
                    if (!".".equals(fileAttribute.getFileName()) && !"..".equals(fileAttribute.getFileName())) {
                        List(pathName + fileAttribute.getFileName() + "/",fileAttributeList,parentId,id);
                    }
                }
            }
 
        }
    }
 
    /**
     *  递归遍历出目录下面所有测试文件
     *
     * @param pathName 需要遍历的目录,必须以"/"开始和结束
     * @throws IOException
     */
    public void ListBattTestData(String pathName, List<MyFTPFile> fileAttributeList,Integer parentId,Integer id) throws IOException {
        if (pathName.startsWith("/") && pathName.endsWith("/")) {
            //更换目录到当前目录
            this.ftp.changeWorkingDirectory(pathName);
            FTPFile[] files = this.ftp.listFiles();
            for (FTPFile file : files) {
                MyFTPFile  fileAttribute=new MyFTPFile();
                fileAttribute.setParentId(parentId);
                if (file.isFile() && file.getName().endsWith(".FBO")) {
                    fileAttribute.setIsfile(1);
                    System.out.println(file.getName());
 
                    //组装文件类
                    setFileAttributeList(fileAttributeList,fileAttribute,file,pathName);
                    //System.out.println(file.getRawListing());
                    arFiles.add(pathName + file.getName());
                }
            }
            System.out.println(arFiles.size());
        }
    }
 
    /**
     * 解析指定的历史放电数据
     *
     * @param pathName 需要遍历的目录,必须以"/"开始和结束
     * @throws IOException
     */
//    public void ReadBattTestData(String filePath,String fileName) throws IOException {
//        System.out.println(filePath);
//
//        if (filePath.startsWith("/") && filePath.endsWith("/")) {
//            //更换目录到当前目录
//            this.ftp.changeWorkingDirectory(filePath);
//
//            InputStream ins = null;
//            try {
//
//                // 从服务器上读取指定的文件
//                ins = this.ftp.retrieveFileStream(fileName);
//                //System.out.println(ins);
//                FboDataInf data_inf = new FboDataInf();
//                ArrayList<FboData> al_fbo_data = new ArrayList<FboData>();
//                FboData.checkFboFile(ins, data_inf, al_fbo_data);
//
//                System.out.println(data_inf);
//                System.out.println("88888===="+al_fbo_data.size());
//                // 主动调用一次getReply()把接下来的226消费掉. 这样做是可以解决这个返回null问题
//                this.ftp.getReply();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
 
 
    /**
     * 递归遍历出目录下面所有文件
     *
     * @param pathName 需要遍历的目录,必须以"/"开始和结束
     * @throws IOException
     */
    public void List(String pathName, List<MyFTPFile> fileAttributeList,Integer parentId,Integer id) throws IOException {
        if (pathName.startsWith("/") && pathName.endsWith("/")) {
            //更换目录到当前目录
            this.ftp.changeWorkingDirectory(pathName);
            FTPFile[] files = this.ftp.listFiles();
            List<MyFTPFile> dirfileAttributeList =new ArrayList<>();
            for (FTPFile file : files) {
                MyFTPFile  fileAttribute=new MyFTPFile();
                fileAttribute.setParentId(parentId);
                if (file.isFile()) {
                    fileAttribute.setIsfile(1);
                    //组装文件类
                    setFileAttributeList(fileAttributeList,fileAttribute,file,pathName);
                    System.out.println(file.getRawListing());
                    // arFiles.add(pathName + file.getName());
                } else if (file.isDirectory()) {
                    id++;
                    System.out.println(file.getRawListing());
                    fileAttribute.setFileId(id);
                    fileAttribute.setFileName(file.getName());
                    fileAttribute.setIsfile(0);
                    fileAttribute.setFileCreateTime(file.getTimestamp().getTime());
                    fileAttribute.setFilePath(pathName+file.getName());
                    fileAttribute.setCreateTime(new Date());
                    fileAttributeList.add(fileAttribute);
                    dirfileAttributeList.add(fileAttribute);
 
                }
            }
 
            if (dirfileAttributeList!=null&&dirfileAttributeList.size()>0){
                for ( MyFTPFile fileAttribute :dirfileAttributeList){
                    //save(fileAttribute);保存数据库返回主键id
                    parentId=fileAttribute.getFileId();
                    // 需要加此判断。否则,ftp默认将‘项目文件所在目录之下的目录(./)’与‘项目文件所在目录向上一级目录下的目录(../)’都纳入递归,这样下去就陷入一个死循环了。需将其过滤掉。
                    if (!".".equals(fileAttribute.getFileName()) && !"..".equals(fileAttribute.getFileName())) {
                        List(pathName + fileAttribute.getFileName() + "/",fileAttributeList,parentId,id);
                    }
                }
            }
 
        }
    }
 
    /**
     * 递归遍历目录下面指定的文件名
     *
     * @param pathName 需要遍历的目录,必须以"/"开始和结束
     * @param ext      文件的扩展名
     * @throws IOException
     */
    public void List(String pathName, String ext) throws IOException {
        if (pathName.startsWith("/") && pathName.endsWith("/")) {
            //更换目录到当前目录
            this.ftp.changeWorkingDirectory(pathName);
            FTPFile[] files = this.ftp.listFiles();
            for (FTPFile file : files) {
                if (file.isFile()) {
                    if (file.getName().endsWith(ext)) {
                        arFiles.add(pathName + file.getName());
                    }
                } else if (file.isDirectory()) {
                    if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
                        List(pathName + file.getName() + "/", ext);
                    }
                }
            }
        }
    }
 
    class MyFTPFile{
        public Integer fileId;
        public String fileName;
        public String fileType;
        public Long fileSize;
        public String filePath;
        public Integer isfile=1;
        public Integer parentId;
        public Date fileCreateTime;
        public Date createTime;
        public Integer getFileId() {
            return fileId;
        }
        public String getFileName() {
            return fileName;
        }
        public String getFileType() {
            return fileType;
        }
        public Long getFileSize() {
            return fileSize;
        }
        public String getFilePath() {
            return filePath;
        }
        public Integer getIsfile() {
            return isfile;
        }
        public Integer getParentId() {
            return parentId;
        }
        public Date getFileCreateTime() {
            return fileCreateTime;
        }
        public Date getCreateTime() {
            return createTime;
        }
        public void setFileId(Integer fileId) {
            this.fileId = fileId;
        }
        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
        public void setFileType(String fileType) {
            this.fileType = fileType;
        }
        public void setFileSize(Long fileSize) {
            this.fileSize = fileSize;
        }
        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }
        public void setIsfile(Integer isfile) {
            this.isfile = isfile;
        }
        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }
        public void setFileCreateTime(Date fileCreateTime) {
            this.fileCreateTime = fileCreateTime;
        }
        public void setCreateTime(Date createTime) {
            this.createTime = createTime;
        }
        @Override
        public String toString() {
            return "MyFTPFile [fileId=" + fileId + ", fileName=" + fileName + ", fileType=" + fileType + ", fileSize="
                    + fileSize + ", filePath=" + filePath + ", isfile=" + isfile + ", parentId=" + parentId
                    + ", fileCreateTime=" + fileCreateTime + ", createTime=" + createTime + "]";
        }
 
    }
 
 
    public static void main(String[] args) {
        FTPFileUtil util = new FTPFileUtil();
        //读取所有的电池组列表
        //List<MyFTPFile> myFiles = util.readAllBattt("192.168.7.130");
 
        //读取所有的历史文件列表
        //List<MyFTPFile> myFiles = util.readAllTestData("Default_Battery");
 
        util.readBattTestData("Default_Battery","F2017-01-15 02.16.18.FBO");
 
//        for(MyFTPFile file:myFiles) {
//            System.err.println(file);
//        }
 
    }
}