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
package com.whyc.dto;
 
import com.whyc.constant.YamlProperties;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
@Component
public class FTPClientUtil {
 
    public static FTPClient connect(){
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(YamlProperties.ftpIp,YamlProperties.ftpPort);
            ftpClient.login(YamlProperties.ftpUserName,YamlProperties.ftpPassword);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            final int bufferSize = 1024 * 1024;
            ftpClient.setBufferSize(bufferSize);
 
            return ftpClient;
 
 
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static void transferFile(File originalFile, String descFilePath){
        FTPClient ftpClient = connect();
        if(ftpClient !=null) {
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(originalFile);
                ftpClient.storeFile(descFilePath, fileInputStream);
 
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileInputStream.close();
                    if (ftpClient.isConnected()) {
                        ftpClient.logout();
                        ftpClient.disconnect();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
 
    }
 
 
}