各位大虾:
    机器A上有一目录,如/home/myfile,之下有一些文件,我想把这些文件通过java编写的程序ftp到机器B上。可以吗?能否给点思路?    谢谢先

解决方案 »

  1.   

    用 sun.net.ftp.FtpClient 。
    FtpClient ftp = new FtpClient();    
    ftp.openServer(Address,Port));
    ftp.login(User,Password);ftp.cd(FTPDirectory);
    ftp.put(FTPFileName);
    得到一个文件流
    就可以传文件了。
      

  2.   

    下面是我寫過關於FTPClient,看對你有沒有一點幫助package com.XXXX;import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;import java.io.*;
    import java.net.SocketException;public class myFtpClient {
      protected FTPClient FTP_;
      protected String host;
      protected int port = 21;
      protected String userID;
      protected String password;  public myFtpClient() {
        FTP_ = new FTPClient();
      }  public myFtpClient(String host, String userID, String password) {
        this.host = host;
        this.userID = userID;
        this.password = password;
        FTP_ = new FTPClient();
      }  public myFtpClient(String host, int port, String userID, String password) {
        this.host = host;
        this.port = port;
        this.userID = userID;
        this.password = password;
        FTP_ = new FTPClient();
      }  /**
       * set Info
       *
       * @param host
       * @param port
       * @param userID
       * @param password
       */
      public void setInfo(String host, int port, String userID, String password) {
        this.host = host;
        this.port = port;
        this.userID = userID;
        this.password = password;
      }  /**
       * open connection
       *
       * @return boolean
       * @throws java.io.IOException
       * @throws SocketException
       */
      public boolean connect() throws Exception {
        FTP_.setDefaultPort(this.port);
        FTP_.setDataTimeout(120000);    //timeout為120秒
        FTP_.connect(this.host);
        if (!FTP_.isConnected()) {
          throw new Exception("NOT CONNECT FTP");
        }
        if (!FTPReply.isPositiveCompletion(FTP_.getReplyCode())) {
          FTP_.disconnect();
          System.out.println("Connection refused.");
          return false;
        }
        if (FTP_.login(this.userID, this.password)) {
          return true;
        } else {
          throw new Exception("NOT LOGIN");
        }
      }  /**
       * open connection by info
       *
       * @param host
       * @param userID
       * @param password
       * @return boolean
       * @throws IOException
       * @throws SocketException
       */
      public boolean connect(String host, String userID, String password)
          throws IOException, SocketException {
        try {
          FTP_.connect(host);
          if (!FTPReply.isPositiveCompletion(FTP_.getReplyCode())) {
            FTP_.disconnect();
            System.out.println("Connection refused.");
            return false;
          }
          FTP_.login(userID, password);
          return true;
        } catch (SocketException e) {
          throw e;
        } catch (IOException e) {
          throw e;
        }
      }
      

  3.   

    /**
       * close connection
       *
       * @throws IOException
       */
      public void disconnect() throws IOException {
        FTP_.logout();
        FTP_.disconnect();
      }  /**
       * retrieve one File from FTP
       *
       * @param remoteFileDir
       * @param localDir
       * @param remoteFileName
       * @return boolean
       */
      public boolean retrieveFile(String remoteFileDir, String localDir, String remoteFileName) throws Exception {
        FileOutputStream output = null;
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          output = new FileOutputStream(localDir + "/" + remoteFileName);
          FTP_.retrieveFile(remoteFileName, output);
        } catch (Exception ex) {
          ex.printStackTrace();
          throw ex;
          //return false;
        } finally {
          if (output != null) {
            try {
              output.close();
            } catch (Exception e) {
            }
          }
        }
        return true;
      }  /**
       * retrieve one directory file from FTP
       *
       * @param remoteFileDir
       * @param localDir
       * @return boolean
       */
      public boolean retrieveAllFile(String remoteFileDir, String localDir) {
        FTPFile[] files = null;
        FileOutputStream output = null;
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }      FTP_.changeWorkingDirectory(remoteFileDir);
          files = FTP_.listFiles();
          if (files != null) {
            for (int i = 0; i < files.length; i++) {
              if (files[i].isFile()) {
                output = new FileOutputStream(localDir + "/" + files[i].getName());
                FTP_.retrieveFile(files[i].getName(), output);
                output.flush();
              }
            }
          }
          return true;
        } catch (Exception ex) {
          ex.printStackTrace();
          return false;
        }
      }  /**
       * store one File to FTP
       *
       * @param remoteFileDir
       * @param localDir
       * @param localFileName
       * @return boolean
       */
      public boolean storeFile(String remoteFileDir, String localDir, String localFileName) {
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          FileInputStream input = new FileInputStream(localDir + "/" + localFileName);
          FTP_.storeFile(localFileName, input);
          input.close();
        } catch (Exception ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  /**
       * store one Directory File to FTP
       *
       * @param remoteFileDir
       * @param localDir
       * @return boolean
       */
      public boolean storeAllFile(String remoteFileDir, String localDir) {
        File ofile = new File(localDir);
        String[] sFileName = ofile.list();
        FileInputStream input = null;
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          for (int i = 0; i < sFileName.length; i++) {
            input = new FileInputStream(localDir + "/" + sFileName[i]);
            FTP_.storeFile(sFileName[i], input);
          }
        } catch (Exception ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  /**
       * delete current directory FTP file
       *
       * @param pathname
       * @return b
       */
      public boolean deleteFile(String pathname) {
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.deleteFile(pathname);
        } catch (IOException ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  /**
       * delete designate directory FTP file
       *
       * @param remoteFileDir
       * @param pathname
       * @return b
       */
      public boolean deleteDirFile(String remoteFileDir, String pathname) {
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          FTP_.deleteFile(pathname);
        } catch (IOException ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  /**
       * delete all designate directory FTP file
       *
       * @param remoteFileDir
       * @return b
       */
      public boolean deleteAllFile(String remoteFileDir) {
        FTPFile[] files = null;
        try {
          if (!FTP_.isConnected()) {
            System.out.println("connetion is closed.....");
            return false;
          }
          FTP_.changeWorkingDirectory(remoteFileDir);
          files = FTP_.listFiles();
          if (files != null) {
            for (int i = 0; i < files.length; i++) {
              if (files[i].isFile()) {
                FTP_.deleteFile(files[i].getName());
              }
            }
          }
        } catch (IOException ex) {
          ex.printStackTrace();
          return false;
        }
        return true;
      }  public boolean changeWorkDir(String dir) {
        try {
          return FTP_.changeWorkingDirectory(dir);
        } catch (IOException e) {
          return false;
        }
      }  /**
       * Resume file
       *
       * @param remoteFileDir
       * @param localDir
       * @param remoteFileName
       * @return b
       */
      public boolean fileTransferResume(String remoteFileDir, String localDir, String remoteFileName) {    if (!FTP_.isConnected()) {
          System.out.println("connetion is closed.....");
          return false;
        }
        File file = new File(localDir + "/" + remoteFileName);
        RandomAccessFile rafile = null;
        InputStream datStream = null;
        byte[] buf = new byte[20480];
        try {
          rafile = new RandomAccessFile(file, "rw");
          long length = rafile.length();
          rafile.seek(length);      datStream = FTP_.retrieveFileStream(remoteFileName);      int read = -1;
          do {
            read = datStream.read(buf);
            if (read != -1)
              rafile.write(buf, 0, read);
          } while (read != -1);
          rafile.close();
          return true;    } catch (Exception e) {
          e.printStackTrace();
        }    return true;
      }  public FTPFile[] listFiles() throws IOException {
        return FTP_.listFiles();
      }  public FTPClient getFTP_() {
        return FTP_;
      }  public void setFTP_(FTPClient connect) {
        FTP_ = connect;
      }  public String getHost() {
        return host;
      }  public void setHost(String host) {
        this.host = host;
      }  public String getPassword() {
        return password;
      }  public void setPassword(String password) {
        this.password = password;
      }  public int getPort() {
        return port;
      }  public void setPort(int port) {
        this.port = port;
      }  public String getUserID() {
        return userID;
      }  public void setUserID(String userID) {
        this.userID = userID;
      }}
      

  4.   

    好强啊。org里我怎么没找到这个包啊,我是新手,JDK难到官方的不全吗,还是ORG包不是官方的