Java_ftp_ftpclient下载文件

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

/**

 * ftpclient下载文件

 */

public class Test {

  public static void main(String[] args) throws Exception {

    FTPClient ftpClient = new FTPClient();

    String hostName = "www.java2000.net";

    String userName = "username";

    String password = "password";

    String remoteDir = "/";

    try {

      ftpClient.connect(hostName, 21);

      ftpClient.setControlEncoding("UTF-8");

      ftpClient.login(userName, password);

      ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

      FTPFile[] files = ftpClient.listFiles(remoteDir);

      for (int i = 0; i < files.length; i++) {

        System.out.println(files[i].getName());

      }

      File file = new File("d://index.jsp");

      FileOutputStream fos = new FileOutputStream(file);

      ftpClient.retrieveFile(remoteDir + "/index.jsp", fos);

      fos.close();

    } catch (SocketException e) {

      e.printStackTrace();

    } catch (IOException e) {

      e.printStackTrace();

    }

  }

}

你可能感兴趣的:(FTPClient)