RemoteFile.java revision 4ca3386623ce60063f27955ad1b2b1b6cbba8b09
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 */
16package org.mockftpserver.stub.example;
17
18import java.io.ByteArrayOutputStream;
19import java.io.IOException;
20import java.net.SocketException;
21
22import org.apache.commons.net.ftp.FTPClient;
23
24/**
25 * Simple FTP client code example.
26 *
27 * @version $Revision$ - $Date$
28 *
29 * @author Chris Mair
30 */
31public class RemoteFile {
32
33    private String server;
34
35    public String readFile(String filename) throws SocketException, IOException {
36
37        FTPClient ftpClient = new FTPClient();
38        ftpClient.connect(server);
39
40        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
41        boolean success = ftpClient.retrieveFile(filename, outputStream);
42        ftpClient.disconnect();
43
44        if (!success) {
45            throw new IOException("Retrieve file failed: " + filename);
46        }
47        return outputStream.toString();
48    }
49
50    /**
51     * Set the hostname of the FTP server
52     * @param server - the hostname of the FTP server
53     */
54    public void setServer(String server) {
55        this.server = server;
56    }
57
58    // Other methods ...
59}
60