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

User picture

Author: klystr

Revision: 369 («Previous)


File Size: 2.9 KB

(December 07, 2009 10:44 UTC) Over 2 years ago

Final

 
Show/hide line numbers
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;
	}
}