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

365368
5
import java.io.InputStream;
5
import java.io.InputStream;
6
import java.net.SocketException;
6
import java.net.SocketException;
7
7
8
import org.apache.commons.net.ftp.FTPClient;
9
import org.apache.commons.net.ftp.FTPFile;
10
11
public class ESBFTP {
8
public class ESBFTP {
12
9
13
	private String ftpAddress, ftpUserName, ftpPassword;
10
	private String ftpAddress, ftpUserName, ftpPassword;
...
...
34
	}
31
	}
35
32
36
	/**
33
	/**
34
	 * Haalt bestanden uit directory
35
	 * 
36
	 * @param directory
37
	 *            doeldirectory
38
	 * @return Array van FTPFiles
39
	 */
40
	public FTPFile[] getFilesFromDir(String directory) {
41
		this.makeConnection();
42
		FTPFile[] output = null;
43
		try {
44
			output = this.ftpClient.listFiles(directory);
45
		} catch (IOException e) {
46
			e.printStackTrace();
47
		}
48
		this.terminateConnection();
49
		return output;
50
	}
51
52
	/**
37
	 * Maakt verbinding met de FTP-server en slaat deze verbinding op in de
53
	 * Maakt verbinding met de FTP-server en slaat deze verbinding op in de
38
	 * lokale variabele ftpClient
54
	 * lokale variabele ftpClient
39
	 */
55
	 */
...
...
50
	}
66
	}
51
67
52
	/**
68
	/**
53
	 * Sluit connectie met server die nu in lokale variabele ftpClient staat
69
	 * Haalt opgegeven bestand van de server er verwijderd deze.
70
	 * 
71
	 * @param filePath
72
	 *            Pad + bestandsnaam
73
	 * @return Bestandsinhoud
54
	 */
74
	 */
55
	private void terminateConnection() {
75
	public String pollFile(String filePath) {
76
		this.makeConnection();
77
		ByteArrayOutputStream outputStream = null;
56
		try {
78
		try {
57
			this.ftpClient.disconnect();
79
			outputStream = new ByteArrayOutputStream();
80
			this.ftpClient.retrieveFile(filePath, outputStream);
81
			// this.ftpClient.dele(filePath);
58
		} catch (IOException e) {
82
		} catch (IOException e) {
59
			e.printStackTrace();
83
			e.printStackTrace();
60
		}
84
		}
61
		this.ftpClient = null;
85
86
		this.terminateConnection();
87
		return outputStream.toString();
62
	}
88
	}
63
89
64
	/**
90
	/**
...
...
82
	}
108
	}
83
109
84
	/**
110
	/**
85
	 * Haalt bestanden uit directory
111
	 * Sluit connectie met server die nu in lokale variabele ftpClient staat
86
	 * 
87
	 * @param directory
88
	 *            doeldirectory
89
	 * @return Array van FTPFiles
90
	 */
112
	 */
91
	public FTPFile[] getFilesFromDir(String directory) {
113
	private void terminateConnection() {
92
		this.makeConnection();
93
		FTPFile[] output = null;
94
		try {
114
		try {
95
			output = this.ftpClient.listFiles(directory);
115
			this.ftpClient.disconnect();
96
		} catch (IOException e) {
116
		} catch (IOException e) {
97
			e.printStackTrace();
117
			e.printStackTrace();
98
		}
118
		}
99
		this.terminateConnection();
119
		this.ftpClient = null;
100
		return output;
101
	}
120
	}
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
	}
124
}
121
}