root/Weektaken2/Weektaak3/ESB/src/hanze/ga/wt3/ftp/ESBFTP.java

357361
1
package hanze.ga.wt3.ftp;
1
package hanze.ga.wt3.ftp;
2
2
3
import java.io.ByteArrayOutputStream;
3
import java.io.IOException;
4
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.InputStream;
5
import java.net.SocketException;
6
import java.net.SocketException;
6
7
7
import org.apache.commons.net.ftp.FTPClient;
8
import org.apache.commons.net.ftp.FTPClient;
9
import org.apache.commons.net.ftp.FTPFile;
8
10
9
public class ESBFTP {
11
public class ESBFTP {
10
12
11
	private String ftpAddress, ftpUserName, ftpPassword;
13
	private String ftpAddress, ftpUserName, ftpPassword;
12
	int ftpPort;
14
	int ftpPort;
15
	private FTPClient ftpClient;
13
16
17
	/**
18
	 * Klasse maakt FTP-handelingen mogelijk
19
	 * 
20
	 * @param ftpAddress
21
	 *            IP-adres of hostnaam van FTP-server
22
	 * @param ftpUserName
23
	 *            FTP-username
24
	 * @param ftpPassword
25
	 *            FTP-password
26
	 * @param ftpPort
27
	 *            FTP-poort (standaard 21)
28
	 */
14
	public ESBFTP(String ftpAddress, String ftpUserName, String ftpPassword, int ftpPort) {
29
	public ESBFTP(String ftpAddress, String ftpUserName, String ftpPassword, int ftpPort) {
15
		this.ftpAddress = ftpAddress;
30
		this.ftpAddress = ftpAddress;
16
		this.ftpUserName = ftpUserName;
31
		this.ftpUserName = ftpUserName;
...
...
18
		this.ftpPort = ftpPort;
33
		this.ftpPort = ftpPort;
19
	}
34
	}
20
35
21
	public void store(InputStream file, String destination) {
36
	/**
22
		FTPClient ftpClient = new FTPClient();
37
	 * Maakt verbinding met de FTP-server en slaat deze verbinding op in de
38
	 * lokale variabele ftpClient
39
	 */
40
	private void makeConnection() {
23
		try {
41
		try {
24
			// Naar FTP
42
			ftpClient = new FTPClient();
25
			ftpClient.connect(this.ftpAddress, this.ftpPort);
43
			ftpClient.connect(this.ftpAddress, this.ftpPort);
26
			ftpClient.login(this.ftpUserName, this.ftpPassword);
44
			ftpClient.login(this.ftpUserName, this.ftpPassword);
27
			ftpClient.storeFile(destination, file);
28
			ftpClient.disconnect();
29
		} catch (SocketException e) {
45
		} catch (SocketException e) {
30
			e.printStackTrace();
46
			e.printStackTrace();
31
		} catch (IOException e) {
47
		} catch (IOException e) {
...
...
33
		}
49
		}
34
	}
50
	}
35
51
52
	/**
53
	 * Sluit connectie met server die nu in lokale variabele ftpClient staat
54
	 */
55
	private void terminateConnection() {
56
		try {
57
			this.ftpClient.disconnect();
58
		} catch (IOException e) {
59
			e.printStackTrace();
60
		}
61
		this.ftpClient = null;
62
	}
63
64
	/**
65
	 * Transporteert bestand naar de server
66
	 * 
67
	 * @param file
68
	 *            Stream van het bestand
69
	 * @param destination
70
	 *            Pad + bestandsnaam
71
	 */
72
	public void store(InputStream file, String destination) {
73
		try {
74
			this.makeConnection();
75
			this.ftpClient.storeFile(destination, file);
76
			this.terminateConnection();
77
		} catch (SocketException e) {
78
			e.printStackTrace();
79
		} catch (IOException e) {
80
			e.printStackTrace();
81
		}
82
	}
83
84
	/**
85
	 * Haalt bestanden uit directory
86
	 * 
87
	 * @param directory
88
	 *            doeldirectory
89
	 * @return Array van FTPFiles
90
	 */
91
	public FTPFile[] getFilesFromDir(String directory) {
92
		this.makeConnection();
93
		FTPFile[] output = null;
94
		try {
95
			output = this.ftpClient.listFiles(directory);
96
		} catch (IOException e) {
97
			e.printStackTrace();
98
		}
99
		this.terminateConnection();
100
		return output;
101
	}
102
103
	/**
104
	 * Haalt opgegeven bestand van de server er verwijderd deze.
105
	 * 
106
	 * @param filePath
107
	 *            Pad + bestandsnaam
108
	 * @return Bestandsinhoud
109
	 */
110
	public String pollFile(String filePath) {
111
		this.makeConnection();
112
		ByteArrayOutputStream outputStream = null;
113
		try {
114
			outputStream = new ByteArrayOutputStream();
115
			this.ftpClient.retrieveFile(filePath, outputStream);
116
			this.ftpClient.dele(filePath);
117
		} catch (IOException e) {
118
			e.printStackTrace();
119
		}
120
121
		this.terminateConnection();
122
		return outputStream.toString();
123
	}
36
}
124
}