Home | History | Annotate | Download | only in example
      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 java.io.ByteArrayOutputStream;
     19 import java.io.IOException;
     20 import java.net.SocketException;
     21 
     22 import 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  */
     31 public class RemoteFile {
     32 
     33     private String server;
     34     private int port;
     35 
     36     public String readFile(String filename) throws SocketException, IOException {
     37 
     38         FTPClient ftpClient = new FTPClient();
     39         ftpClient.connect(server, port);
     40 
     41         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
     42         boolean success = ftpClient.retrieveFile(filename, outputStream);
     43         ftpClient.disconnect();
     44 
     45         if (!success) {
     46             throw new IOException("Retrieve file failed: " + filename);
     47         }
     48         return outputStream.toString();
     49     }
     50 
     51     /**
     52      * Set the hostname of the FTP server
     53      * @param server - the hostname of the FTP server
     54      */
     55     public void setServer(String server) {
     56         this.server = server;
     57     }
     58 
     59     /**
     60      * Set the port number for the FTP server
     61      * @param port - the port number
     62      */
     63     public void setPort(int port) {
     64         this.port = port;
     65     }
     66 
     67     // Other methods ...
     68 }
     69