chrismair | 00dc7bd | 2014-05-11 21:21:28 +0000 | [diff] [blame] | 1 | /*
|
| 2 | * Copyright 2007 the original author or authors.
|
| 3 | *
|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 | * you may not use this file except in compliance with the License.
|
| 6 | * You may obtain a copy of the License at
|
| 7 | *
|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 9 | *
|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 | * See the License for the specific language governing permissions and
|
| 14 | * limitations under the License.
|
| 15 | */
|
| 16 | package org.mockftpserver.stub.example;
|
| 17 |
|
| 18 | import org.apache.commons.net.ftp.FTPClient;
|
| 19 |
|
| 20 | import java.io.ByteArrayOutputStream;
|
| 21 | import java.io.IOException;
|
| 22 |
|
| 23 | /**
|
| 24 | * Simple FTP client code example.
|
| 25 | *
|
| 26 | * @author Chris Mair
|
| 27 | * @version $Revision$ - $Date$
|
| 28 | */
|
| 29 | public class RemoteFile {
|
| 30 |
|
| 31 | public static final String USERNAME = "user";
|
| 32 | public static final String PASSWORD = "password";
|
| 33 |
|
| 34 | private String server;
|
| 35 | private int port;
|
| 36 |
|
| 37 | public String readFile(String filename) throws IOException {
|
| 38 |
|
| 39 | FTPClient ftpClient = new FTPClient();
|
| 40 | ftpClient.connect(server, port);
|
| 41 | ftpClient.login(USERNAME, PASSWORD);
|
| 42 |
|
| 43 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
| 44 | boolean success = ftpClient.retrieveFile(filename, outputStream);
|
| 45 | ftpClient.disconnect();
|
| 46 |
|
| 47 | if (!success) {
|
| 48 | throw new IOException("Retrieve file failed: " + filename);
|
| 49 | }
|
| 50 | return outputStream.toString();
|
| 51 | }
|
| 52 |
|
| 53 | /**
|
| 54 | * Set the hostname of the FTP server
|
| 55 | *
|
| 56 | * @param server - the hostname of the FTP server
|
| 57 | */
|
| 58 | public void setServer(String server) {
|
| 59 | this.server = server;
|
| 60 | }
|
| 61 |
|
| 62 | /**
|
| 63 | * Set the port number for the FTP server
|
| 64 | *
|
| 65 | * @param port - the port number
|
| 66 | */
|
| 67 | public void setPort(int port) {
|
| 68 | this.port = port;
|
| 69 | }
|
| 70 |
|
| 71 | // Other methods ...
|
| 72 | }
|