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; |
| | 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 | } |