TelnetInputStream out=ftpClient.get("hello.txt");
RandomAccessFile ra=new RandomAccessFile("G:\\ftp\\111.txt","rw");这个就是从服务器上下载hello.txt,存为111.txt

解决方案 »

  1.   

    package bean;import sun.net.ftp.*;
    import java.io.*;
    import java.io.IOException;
    import java.util.StringTokenizer;
    import sun.net.ftp.*;
    import java.util.ArrayList;
    import sun.net.TelnetOutputStream;
    /**
     * 一个连接FTP的类
     * 只有上传文件
     * 通过测试
     *
     *
     * @author  jiaoyan
     * @version 1.0
     */
    public class Ftpupload {
      FtpClient client;
      private String host="123.456.789.133";//FTP服务器IP
      private String username="anonymous";//FTP用户名字
      private String password=" ";//FTP密码
      private String path = "/wanglei";  //文件要放哪个目录
      private int port = 21;           //FTP端口
      /**
       * 连接服务器方法
       */
      public void connect() {
        try {
          client = new FtpClient(host);
          client.login(username, password);
          //设置成2进制传输
          client.binary();
          client.cd(path);
          System.out.println("登陆成功");
        }
        catch (FtpLoginException e) {
          System.out.println("无权限相连接" + e.getMessage());
        }
        catch(IOException e){
          System.out.println("连接失败"+e.getMessage());
        }
        catch(SecurityException e){
          System.out.println("用户名字或者密码不对");
        }  }
      /**
       * 上传文件
       * @param fileName String 文件名字
       * @param filePath String 文件路径
       * @return boolean
       */
      public boolean upLoad(String fileName,String filePath) {
        File localFile = new File(filePath);    try {
          TelnetOutputStream os = client.put(fileName);
          java.io.File file_in = new java.io.File(filePath);
          FileInputStream is = new FileInputStream(file_in);
          byte[] bytes = new byte[1024];
          int c;
          while ( (c = is.read(bytes)) != -1) {
            os.write(bytes, 0, c);
          }
          is.close();
          os.close();
          System.out.println("传送成功");    }
        catch (Exception e) {
          System.out.println("上传文件出错"+e.getMessage());
          return false;
        }
        return true;
      }
      public void closeServer()throws Exception{
        if(client!=null){
          client.closeServer();
          System.out.println("FTP已经关闭");
        }
      }
    }
      

  2.   

    very good!import sun.net.ftp.*;
    import java.io.*;
    import java.io.IOException;
    import java.util.StringTokenizer;
    import sun.net.ftp.*;
    import java.util.ArrayList;
    import sun.net.TelnetOutputStream;
    import sun.net.TelnetInputStream;/**
     * 一个连接FTP的类
     * 只有上传文件
     * 通过测试
     *
     *
     * @author  jiaoyan
     * @version 1.0
     */
    public class Ftp{
      FtpClient client;
      Config cfg = Config.getConfig();  private String host = cfg.getProperty("account.ftp.ip"); //FTP服务器IP
      private String username = cfg.getProperty("account.ftp.username"); //FTP用户名字
      private String password = cfg.getProperty("account.ftp.password"); //FTP密码
      private String path = cfg.getProperty("account.ftp.path"); //文件要放哪个目录
      private int port = new Integer(cfg.getProperty("account.ftp.port")).intValue(); //FTP端口
      /**
       * 连接服务器方法
       */
      public void connect() {
        try {
          client = new FtpClient(host);
          client.login(username, password);
          //设置成2进制传输
          client.binary();
          client.cd(path);
          System.out.println("登陆成功");
        }
        catch (FtpLoginException e) {
          System.out.println("无权限相连接" + e.getMessage());
        }
        catch (IOException e) {
          System.out.println("连接失败" + e.getMessage());
        }
        catch (SecurityException e) {
          System.out.println("用户名字或者密码不对");
        }  }  /**
       * 上传文件
       * @param fileName String 文件名字
       * @param filePath String 文件路径
       * @return boolean
       */
      public boolean upLoad(String fileName, String filePath) {
        File localFile = new File(filePath);    try {
          TelnetOutputStream os = client.put(fileName);
          java.io.File file_in = new java.io.File(filePath);
          FileInputStream is = new FileInputStream(file_in);
          byte[] bytes = new byte[1024];
          int c;
          while ( (c = is.read(bytes)) != -1) {
            os.write(bytes, 0, c);
          }
          is.close();
          os.close();
          System.out.println("传送成功");    }
        catch (Exception e) {
          System.out.println("上传文件出错" + e.getMessage());
          return false;
        }
        return true;
      }  public boolean download(String fileName, String filePath) {
        try {
          TelnetInputStream out = client.get(fileName);
          RandomAccessFile ra = new RandomAccessFile(filePath, "rw");
        }
        catch (Exception e) {
          System.out.println("下载文件出错" + e.getMessage());
          return false;
        }
        return true;  }  public void closeServer() throws Exception {
        if (client != null) {
          client.closeServer();
          System.out.println("FTP已经关闭");
        }
      }
    }