whyclxw
2025-03-06 48cb62c5b74e76e6af8055852530d8fa5459939a
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
//package com.whyc.dto;
//
//import com.enterprisedt.net.ftp.*;
//import org.apache.commons.lang.StringUtils;
//
//import java.io.*;
//import java.text.ParseException;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.LinkedList;
//import java.util.List;
//
//public class FtpHelper {
//    private FTPClient ftp;
//
//    /**
//     * 初始化Ftp信息
//     *
//     * @param ftpServer   ftp服务器地址
//     * @param ftpPort     Ftp端口号
//     * @param ftpUsername ftp 用户名
//     * @param ftpPassword ftp 密码
//     */
//    public FtpHelper(String ftpServer, int ftpPort, String ftpUsername,
//                     String ftpPassword) {
//        connect(ftpServer, ftpPort, ftpUsername, ftpPassword);
//    }
//
//    /**
//     * 连接到ftp
//     *
//     * @param ftpServer   ftp服务器地址
//     * @param ftpPort     Ftp端口号
//     * @param ftpUsername ftp 用户名
//     * @param ftpPassword ftp 密码
//     */
//    public void connect(String ftpServer, int ftpPort, String ftpUsername, String ftpPassword) {
//        ftp = new FTPClient();
//        try {
//            ftp.setControlEncoding("GBK");
//            ftp.setRemoteHost(ftpServer);
//            ftp.setRemotePort(ftpPort);
//            ftp.setTimeout(1000*60*60*12);
//            ftp.setConnectMode(FTPConnectMode.ACTIVE);
//            ftp.connect();
//            ftp.login(ftpUsername, ftpPassword);
//            ftp.setType(FTPTransferType.BINARY);
//        } catch (Exception e) {
//            e.printStackTrace();
//            ftp = null;
//        }
//    }
//
//    /**
//     * 更改ftp路径
//     *
//     * @param ftp
//     * @param dirName
//     * @return
//     */
//    public boolean checkDirectory(FTPClient ftp, String dirName) {
//        boolean flag;
//        try {
//            ftp.chdir(dirName);
//            flag = true;
//        } catch (Exception e) {
//            //e.printStackTrace();
//            flag = false;
//        }
//        return flag;
//    }
//
//    /**
//     * 断开ftp链接
//     */
//    public void disconnect() {
//        try {
//            if (ftp.connected()) {
//                ftp.quit();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 读取ftp文件流
//     *
//     * @param filePath ftp文件路径
//     * @return s
//     * @throws Exception
//     */
//    public InputStream downloadFile(String filePath) throws Exception {
//        InputStream inputStream = null;
//        String fileName = "";
//        filePath = StringUtils.removeStart(filePath, "/");
//        int len = filePath.lastIndexOf("/");
//        if (len == -1) {
//            if (filePath.length() > 0) {
//                fileName = filePath;
//            } else {
//                throw new Exception("没有输入文件路径");
//            }
//        } else {
//            fileName = filePath.substring(len + 1);
//
//            String type = filePath.substring(0, len);
//            String[] typeArray = type.split("/");
//            for (String s : typeArray) {
//                ftp.chdir(s);
//            }
//        }
//        byte[] data;
//        try {
//            data = ftp.get(fileName);
//            inputStream = new ByteArrayInputStream(data);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return inputStream;
//    }
//
//    /**
//     * 上传文件到ftp
//     *
//     * @param file     文件对象
//     * @param filePath 上传的路径
//     * @throws Exception
//     */
//    public void uploadFile(File file, String filePath) throws Exception {
//        InputStream inStream = new FileInputStream(file);
//        uploadFile(inStream, filePath);
//    }
//
//    /**
//     * 上传文件到ftp
//     *
//     * @param inStream 上传的文件流
//     * @param filePath 上传路径
//     * @throws Exception
//     */
//    public void uploadFile(InputStream inStream, String filePath)
//            throws Exception {
//        if (inStream == null) {
//            return;
//        }
//        String fileName = "";
//        filePath = StringUtils.removeStart(filePath, "/");
//        int len = filePath.lastIndexOf("/");
//        if (len == -1) {
//            if (filePath.length() > 0) {
//                fileName = filePath;
//            } else {
//                throw new Exception("没有输入文件路径");
//            }
//        } else {
//            fileName = filePath.substring(len + 1);
//            String type = filePath.substring(0, len);
//            String[] typeArray = type.split("/");
//            for (String s : typeArray) {
//                s.trim();
//                if (!checkDirectory(ftp, s)) {
//                    if(!ftp.existsDirectory(s)) {
//                        ftp.mkdir(s);
//                    }
//                    changeDirectory(s);
//                }
//            }
//        }
//        ftp.put(inStream, fileName);
//        changeDirectory("/");
//    }
//
//    public void uploadFile2(File inFile,String fileName) throws IOException, FTPException {
//        InputStream inStream = new FileInputStream(inFile);
//        ftp.put(inStream, fileName);
//    }
//
//    /**
//     * 删除ftp文件
//     *
//     * @param filePath 文件路径
//     * @throws Exception
//     */
//    public void deleteFile(String filePath) throws Exception {
//        String fileName = "";
//        filePath = StringUtils.removeStart(filePath, "/");
//        int len = filePath.lastIndexOf("/");
//        if (len == -1) {
//            if (filePath.length() > 0) {
//                fileName = filePath;
//            } else {
//                throw new Exception("没有输入文件路径");
//            }
//        } else {
//            fileName = filePath.substring(len + 1);
//
//            String type = filePath.substring(0, len);
//            String[] typeArray = type.split("/");
//            for (String s : typeArray) {
//                if (checkDirectory(ftp, s)) {
//                    ftp.chdir(s);
//                }
//            }
//        }
//        ftp.delete(fileName);
//    }
//
//
//     //切换目录
//    public void changeDirectory(String path) {
//        if (!StringUtils.isEmpty(path)) {
//            try {
//                ftp.chdir(path);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    //读取文件列表
//    public List<File> getFileList(String path) {
//        List<File> rsList = new ArrayList<>();
//        if (path == null || path.equals("")) {
//            return rsList;
//        }
//        File file = new File(path);
//        if (!file.exists()) {
//            return rsList;
//        }
//        // 处理文件
//        if (file.isFile()) {
//            rsList.add(file);
//            return rsList;
//        }
//        // 处理文件夹
//        if (null == file.listFiles()) {
//            rsList.add(file);
//            return rsList;
//        }
//        LinkedList<File> forDealList = new LinkedList<>();
//        forDealList.addAll(Arrays.asList(file.listFiles()));
//        while (!forDealList.isEmpty()) {
//            File firstFile = forDealList.removeFirst();
//            // 处理文件
//            if (firstFile.isFile()) {
//                // 把文件自身先加入结果
//                rsList.add(firstFile);
//                continue;
//            }
//            // 处理文件夹
//            File[] files = firstFile.listFiles();
//            if (files == null) {
//                continue;
//            }
//            for (File curr : files) {
//                if (curr.isDirectory()) {
//                    // 将此文件夹放入待处理列表
//                    forDealList.add(curr);
//                } else {
//                    rsList.add(curr);
//                }
//            }
//        }
//        return rsList;
//    }
//
//    public void deleteDir(String dirName) throws IOException, FTPException, ParseException {
//        FTPClient client = ftp;
//
//        //String[] dir = client.dir(dirName, true);
//        FTPFile[] ftpFiles = client.dirDetails(dirName);
//        for (FTPFile ftpFile : ftpFiles) {
//            if(ftpFile.isDir()){
//                deleteDir(dirName+"/"+ftpFile.getName());
//            }else{
//                client.delete(dirName+"/"+ftpFile.getName());
//            }
//        }
//        /*for (String subDirName : dir) {
//            String name = subDirName.substring(subDirName.lastIndexOf(" ") + 1);
//            if(subDirName.contains("<DIR>")){ //文件夹
//                //name = new String(name.getBytes(StandardCharsets.ISO_8859_1),"GBK");
//                deleteDir(dirName+"/"+name);
//                //client.rmdir(dirName+"/"+name);
//            }else{
//                //name = new String(name.getBytes(StandardCharsets.ISO_8859_1),"GBK");
//                client.delete(dirName+"/"+name);
//            }
//        }*/
//        client.rmdir(dirName);
//    }
//
//    public String[] getDirList() throws IOException, FTPException {
//        return ftp.dir();
//    }
//
//    public void mkdir(String dirPath) throws IOException, FTPException {
//        ftp.mkdir(dirPath);
//    }
//}