廖井涛
2024-03-04 eae17d27ec56a6b7887f5597335e38ca40273ef4
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
package ng.db;
 
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import sun.misc.BASE64Encoder;
 
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Random;
 
public class toFtp {
 
    /**
     * »ñȡһ¸öftpÁ¬½Ó
     * @param host ipµØÖ·
     * @param port ¶Ë¿Ú
     * @param username Óû§Ãû
     * @param password ÃÜÂë
     * @return ·µ»ØftpÁ¬½Ó¶ÔÏó
     * @throws Exception Á¬½Óftpʱ·¢ÉúµÄ¸÷ÖÖÒì³£
     */
    public static FTPClient getFtpClient(String host, Integer port, String username, String password) throws Exception{
        FTPClient ftpClient = new FTPClient();
 
 
        // Á¬½Ó·þÎñÆ÷
        ftpClient.connect(host, port);
 
        int reply = ftpClient.getReplyCode();
        if(!FTPReply.isPositiveCompletion(reply)){
 
            ftpClient.disconnect();
            return null;
        }
 
        // µÇÈë·þÎñÆ÷
        boolean login = ftpClient.login(username, password);
        if(!login){
 
            ftpClient.logout();
            ftpClient.disconnect();
            return null;
        }
 
        // Á¬½Ó²¢Çҳɹ¦µÇ½ftp·þÎñÆ÷
 
 
        // ÉèÖÃͨµÀ×Ö·û¼¯£¬ ÒªÓë·þÎñ¶ËÉèÖÃÒ»ÖÂ
        ftpClient.setControlEncoding("UTF-8");
        // ÉèÖÃÎļþ´«Êä±àÂëÀàÐÍ£¬ ×Ö½Ú´«Ê䣺BINARY_FILE_TYPE, Îı¾´«Ê䣺ASCII_FILE_TYPE£¬ ½¨ÒéʹÓÃBINARY_FILE_TYPE½øÐÐÎļþ´«Êä
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        // ¶¯Ä£Ê½: enterLocalActiveMode(),±»¶¯Ä£Ê½: enterLocalPassiveMode(),Ò»°ãÑ¡Ôñ±»¶¯Ä£Ê½
        ftpClient.enterLocalPassiveMode();
        // Çл»Ä¿Â¼
        //ftpClient.changeWorkingDirectory("xxxx");
 
        return ftpClient;
    }
 
    /**
     * ¶Ï¿ªftpÁ¬½Ó
     * @param ftpClient ftpÁ¬½Ó¿Í»§¶Ë
     */
    public static void disConnect(FTPClient ftpClient){
        if(ftpClient == null){
            return;
        }
        try {
 
            ftpClient.logout();
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
 
        }
 
    }
 
    /**
     * ÎļþÏÂÔØ
     * @param ftpClient ftpÁ¬½Ó¿Í»§¶Ë
     * @param path Îļþ·¾¶
     * @param fileName ÎļþÃû³Æ
     */
    public static ArrayList<String> download(FTPClient ftpClient, String path, String fileName) throws Exception {
 
 
 
        if(ftpClient == null || path == null || fileName == null){
            return null;
        }
 
        // ÖÐÎÄĿ¼´¦Àí´æÔÚÎÊÌ⣬ ×ª»¯ÎªftpÄܹ»Ê¶±ðÖÐÎĵÄ×Ö·û¼¯
        String remotePath;
        try {
            remotePath = new String(path.getBytes(StandardCharsets.UTF_8), FTP.DEFAULT_CONTROL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            remotePath = path;
        }
 
 
 
 
        //»ñȡָ¶¨Â·¾¶ÎļþÀïÃæÍ¼Æ¬´æ·ÅµÄ·¾¶
        InputStream inputStream = ftpClient.retrieveFileStream(remotePath);
        ArrayList<String> getPictureList=new ArrayList<String>();
        ArrayList<String> folderNameList=new ArrayList<String>();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"utf-8"))) {
            String line;
            while ((line = br.readLine()) != null) {
 
                folderNameList.add(line);
            }
 
            getPictureList.add(folderNameList.get(folderNameList.size()-2));
            getPictureList.add(folderNameList.get(folderNameList.size()-1));
        } catch (IOException e) {
            e.printStackTrace();
        }
        inputStream.close();
        ftpClient.completePendingCommand();
 
        ArrayList<String> result=new ArrayList<String>();
        for( String item: getPictureList){
            //System.out.println(item);
            InputStream InputStream = ftpClient.retrieveFileStream("./data/"+item);
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            try{
                byte[] buffer = new byte[1024*900];
                int i;
                while ( (i = InputStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, i);
                   //w outputStream.flush();
                }
                buffer = outStream.toByteArray();
                String picture="data:image/jpeg;base64,"+ Base64.getEncoder().encodeToString(buffer);;
 
                result.add(picture);
            } catch (Exception e) {
 
            }
            InputStream.close();
            outStream.close();
            ftpClient.completePendingCommand();
        }
        //InputStream inputStream1=ftpClient.retrieveFileStream("./data/"+lujin);
 
 
//        System.out.println("---");
//        System.out.println(inputStream1);
//        if (inputStream1 == null) {
//            return;
//        }
//        FileOutputStream outputStream = new FileOutputStream("D:\\picture\\" + "1.jpg");
//        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream1);
//        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
//        try{
//            byte[] buffer = new byte[2048];
//            int i;
//            while ((i = bufferedInputStream.read(buffer)) != -1) {
//                bufferedOutputStream.write(buffer, 0, i);
//                bufferedOutputStream.flush();
//            }
//        } catch (Exception e) {
//
//        }
//        inputStream1.close();
//        outputStream.close();
//        bufferedInputStream.close();
//        bufferedOutputStream.close();
 
 
        // ¹Ø±ÕÁ÷Ö®ºó±ØÐëÖ´ÐУ¬·ñÔòÏÂÒ»¸öÎļþµ¼ÖÂÁ÷Ϊ¿Õ
//        boolean complete = ftpClient.completePendingCommand();
//        if(complete){
//            //System.out.println("Îļþ{}ÏÂÔØ³É¹¦"+ remotePath);
//        }else{
//            //System.out.println("Îļþ{}ÏÂÔØÊ§°Ü"+ remotePath);
//        }
    return result;
    }
 
    /**
     * ÉÏ´«Îļþ
     * @param ftpClient ftpÁ¬½Ó¿Í»§¶Ë
     * @param sourcePath Ô´µØÖ·
     */
    public static void upload(FTPClient ftpClient, String sourcePath) throws Exception{
        if(ftpClient == null || sourcePath == null){
            return;
        }
 
        File file = new File(sourcePath);
        if(!file.exists() || !file.isFile()){
            return;
        }
 
        // ÖÐÎÄĿ¼´¦Àí´æÔÚÎÊÌ⣬ ×ª»¯ÎªftpÄܹ»Ê¶±ðÖÐÎĵÄ×Ö·û¼¯
        String remotePath;
        try {
            remotePath = new String(file.getName().getBytes(StandardCharsets.UTF_8), FTP.DEFAULT_CONTROL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            remotePath = file.getName();
        }
 
        try(
                InputStream inputStream = new FileInputStream(file);
                OutputStream outputStream = ftpClient.storeFileStream(remotePath);
        ){
            byte[] buffer = new byte[2048];
            int length;
            while((length = inputStream.read(buffer)) != -1){
                outputStream.write(buffer, 0, length);
                outputStream.flush();
            }
        }catch (Exception e){
 
        }
 
        // ¹Ø±ÕÁ÷Ö®ºó±ØÐëÖ´ÐУ¬·ñÔòÏÂÒ»¸öÎļþµ¼ÖÂÁ÷Ϊ¿Õ
        boolean complete = ftpClient.completePendingCommand();
        if(complete){
            System.out.println("Îļþ{}ÉÏ´«Íê³É"+ remotePath);
        }else{
            System.out.println("Îļþ{}ÉÏ´«Ê§°Ü"+ remotePath);
        }
 
 
    }
}