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 |
package hanze.ga.wt3.ftp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
public class ESBFTP {
private String ftpAddress, ftpUserName, ftpPassword;
int ftpPort;
private FTPClient ftpClient;
/**
* Klasse maakt FTP-handelingen mogelijk
*
* @param ftpAddress
* IP-adres of hostnaam van FTP-server
* @param ftpUserName
* FTP-username
* @param ftpPassword
* FTP-password
* @param ftpPort
* FTP-poort (standaard 21)
*/
public ESBFTP(String ftpAddress, String ftpUserName, String ftpPassword, int ftpPort) {
this.ftpAddress = ftpAddress;
this.ftpUserName = ftpUserName;
this.ftpPassword = ftpPassword;
this.ftpPort = ftpPort;
}
/**
* Haalt bestanden uit directory
*
* @param directory
* doeldirectory
* @return Array van FTPFiles
*/
public FTPFile[] getFilesFromDir(String directory) {
this.makeConnection();
FTPFile[] output = null;
try {
output = this.ftpClient.listFiles(directory);
} catch (IOException e) {
e.printStackTrace();
}
this.terminateConnection();
return output;
}
/**
* Maakt verbinding met de FTP-server en slaat deze verbinding op in de
* lokale variabele ftpClient
*/
private void makeConnection() {
try {
ftpClient = new FTPClient();
ftpClient.connect(this.ftpAddress, this.ftpPort);
ftpClient.login(this.ftpUserName, this.ftpPassword);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Haalt opgegeven bestand van de server er verwijderd deze.
*
* @param filePath
* Pad + bestandsnaam
* @return Bestandsinhoud
*/
public String pollFile(String filePath) {
this.makeConnection();
ByteArrayOutputStream outputStream = null;
try {
outputStream = new ByteArrayOutputStream();
this.ftpClient.retrieveFile(filePath, outputStream);
// this.ftpClient.dele(filePath);
} catch (IOException e) {
e.printStackTrace();
}
this.terminateConnection();
return outputStream.toString();
}
/**
* Transporteert bestand naar de server
*
* @param file
* Stream van het bestand
* @param destination
* Pad + bestandsnaam
*/
public void store(InputStream file, String destination) {
try {
this.makeConnection();
this.ftpClient.storeFile(destination, file);
this.terminateConnection();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Sluit connectie met server die nu in lokale variabele ftpClient staat
*/
private void terminateConnection() {
try {
this.ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
this.ftpClient = null;
}
} |