智慧保安后台管理-外网项目备份
Administrator
2021-08-12 d4e35652a8a879c9e53016eeb1e3e9018c716502
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
package org.springblade.modules.information.controller;
 
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;
 
import java.io.*;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
 
public class FtpUtil {
 
    FtpClient ftpClient;
 
    /**
     * 连接FTP服务
     *
     * @param url           //IP地址
     * @param port//端口号
     * @param username//用户名
     * @param password//密码
     * @return
     */
    public static FtpClient connectFTP(String url, int port, String username, String password) {
        //创建ftp
        FtpClient ftp = null;
        try {
            //创建地址
            SocketAddress addr = new InetSocketAddress(url, port);
            //连接
            ftp = FtpClient.create();
            ftp.connect(addr);
            //登陆
            ftp.login(username, password.toCharArray());
            ftp.setBinaryType();
        } catch (FtpProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ftp;
    }
 
    /**
     * 取ftp上的文件内容
     *
     * @param ftpFile
     * @param ftp
     * @return
     */
    public static List<String> download(String ftpFile, FtpClient ftp) {
        List<String> list = new ArrayList<String>();
        String str = "";
        InputStream is = null;
        BufferedReader br = null;
        try {
            // 获取ftp上的文件
            is = ftp.getFileStream(ftpFile);
            //转为字节流
            br = new BufferedReader(new InputStreamReader(is));
            while ((str = br.readLine()) != null) {
                list.add(str);
            }
            br.close();
        } catch (FtpProtocolException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
 
    public static void main(String[] args) {
        FtpClient ftp = connectFTP("192.168.0.109",21,"anonymous","");
            List<String> list = download("D:\\anbao", ftp);
        for (int i = 0; i < list.size(); i++) {
            String strOut = null;
            if (list.get(i).toString() != null) {
                try {
                    byte[] bs = list.get(i).toString().getBytes();
 
                    strOut = new String(bs, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            System.out.println(strOut);
            System.out.println(list.get(i).toString());
        }
    }
}