在sun的jdk里,sun包里有一个ftp类。

解决方案 »

  1.   

    我的程序出现错误,这里是执行结果:
    开始登录移动FTP服务器User xh logged in.
    成功登录移动FTP服务器,设置传送方式为ASCIIASCII Mode设置本地文件路径为d:\remoteLocal Directory Is Now: d:\Remote获取指定的文件PORT :501 PORT not allowed after EPSV ALL
    文件传送完毕!关闭服务器连接!
    以下是程序代码:
    import sun.net.ftp.*;
    import java.io.*;public class SimpleFTP {
      public static final int BINARY = 0;
      public static final int ASCII = 1;
      private FtpClient client = null;
      private File localDirectory = null;
      public SimpleFTP(String server, int port) {
        try {
          localDirectory = new File(".").getAbsoluteFile().getParentFile();
          client = new FtpClient();
          client.openServer(server, port);
        }
        catch (IOException ioe) {
          System.out.println(ioe.getMessage());
        }
      }  public boolean login(String user, String password) {
        try {
          client.login(user, password);
          System.out.println(client.welcomeMsg);
          return true;
        }
        catch (IOException ioe) {
          System.out.println(ioe.getMessage());
          return false;
        }
      }  public void changeMode(int mode) {
        try {
          if (mode == BINARY) {
            client.binary();
            System.out.println("Binary Mode");
          }
          else if (mode == ASCII) {
            client.ascii();
            System.out.println("ASCII Mode");
          }
          else {
            System.out.println("Unknown Mode");
          }
        }
        catch (IOException ioe) {
          System.out.println(ioe.getMessage());
        }
      }  public void changeDirectory(String direc) {
        try {
          client.cd(direc);
          System.out.println("Directory is now:" + direc);
        }
        catch (IOException ioe) {
          System.out.println(ioe.getMessage());
        }
      }  public void put(String filename) {
        try {
          BufferedOutputStream out = new BufferedOutputStream
              (client.put(filename));
          File outfile = new File(localDirectory, filename);
          BufferedInputStream in = new BufferedInputStream(new FileInputStream(
              outfile));
          final int CHUNK = 1024 * 1024;
          byte[] buffer = new byte[CHUNK];
          int read = 0;
          while ( (read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
          }
          in.close();
          out.close();
          System.out.println("Finished Putting: " +
                             outfile.getAbsoluteFile().toString() + " On To Server");
        }
        catch (IOException ioe) {
          System.out.println(ioe.getMessage());
        }
      }  public void get(String filename) {
        try {
          BufferedInputStream in = new BufferedInputStream(client.get(filename));
          File outfile = new File(localDirectory, filename);
          BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(
              outfile));
          int read = 0;
          final int CHUNK = 1024;
          byte[] buffer = new byte[CHUNK];
          while ( (read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
          }
          in.close();
          out.close();
          System.out.println("Finished Getting: " + outfile.toString() +
                             " From Server");
        }
        catch (IOException ioe) {
          System.out.println(ioe.getMessage());
        }
      }  public void changeLocalDirectory(String localDirec) {
        localDirectory = new File(localDirec);
        System.out.println("Local Directory Is Now: " + localDirectory.toString());
      }  public void closeServer() {
        try {
          client.closeServer();
        }
        catch (IOException ioe) {
          System.out.println(ioe.getMessage());
        }
      }  //Main Process
      public static void main(String args[]) {
        SimpleFTP sftp = new SimpleFTP("10.0.0.1", 21);
        System.out.println("开始登录FTP服务器");
        sftp.login("username", "password");
        System.out.println("成功登录FTP服务器,设置传送方式为ASCII");
        sftp.changeMode(ASCII);
        System.out.println("设置本地文件路径为d:\\remote");
        //sftp.changeDirectory("D:\\Remote");
        //sftp.put("file.name");
        sftp.changeLocalDirectory("d:\\Remote");
        System.out.println("获取指定的文件");
        sftp.get("STATE_20040630012.txt");
        System.out.println("文件传送完毕!");
        sftp.closeServer();
        System.out.println("关闭服务器连接!");
      }
    }高手帮忙看看..有人碰到过同样的问题嘛?
    谢谢!
    错误原因:sun.net.ftp.FtpProtocolException: PORT :501 PORT not allowed after EPSV ALL
      

  2.   

    注:以上程序在连接本地其他机器时候一切正常,连接远程服务器就出现了:
    FtpProtocolException: PORT :501 PORT not allowed after EPSV ALL
    错误..
    有没有人碰到过类似的问题,请帮忙看看,谢谢!
      

  3.   

    远程服务器的端口应该没有被限制,我可以直接通过ftp工具连接,也可以用命令行下进行操作。到底怎么回事呢,着急啊..
      

  4.   

    501 PORT not allowed after EPSV ALL~~~说明还是网络的问题,我对网络不熟~~~比如用ftp工具的时候,有些ftp站点必须采用被动模式才能连接。下面的你看看吧,或许有用,找网管看看应该使用哪种方式访问***************************************************
    3. EPSV命令
    EPSV请求服务器在一个数据端口上侦听等待连接,它可以带参数。对它的响应是TCP端口号。响应的格式与EPRT参数的很象。这对实现上有很大的方便。而且响应还留下了网络协议和/或网络地址的空位,可以供以后使用。使用扩展地址进行被动模式的响应码必须是229,对它的解释如下:
    2yz 主动完成
    x2z 连接
    xy9 进行扩展的被动模式 响应的格式如下:
    <指示服务器已经进入初扩展的被动模式> 
    (<d><d><d><TCP端口><d>)
    包括在括号内的字符串必须是EPRT打开数据连接的端口。具体如上所未,这里就不多说了。数据连接使用的协议必须和控制连接使用的协议和地址一致,下面是响应的一个例子: 
    Entering Extended Passive Mode (|||6446|)
    标准错误代码500和501对EPSV已经足够了。在EPSV命令没有使用参数时,服务器会基于控制连接所使用的协议选择数据连接使用的网络协议。但是在有代理的情况下,这种机制可能不合适。因此客户也需要能够要求一个指定协议。如果服务器返回说明它在指定端口不支持此协议,客户必须发送ABOR(放弃)命令使服务器关闭连接,然后客户再使用EPSV命令要求使用特定的网络协议,具体格式如下:
    EPSV<空格><网络协议>
    如果请求的网络协议是服务器支持的,那就必须使用此协议;如果不支持,则返回522。最后,EPSV命令可以使用参数"ALL"通过网络地址翻译器,EPRT命令不再使用。下面是例子:EPSV<空格>ALL
    接收到此命令后,服务器要拒绝除了EPSV以外所有建立连接的命令。在下一节我们将详细说明此命令的功能。
    **********************************************************************
      

  5.   

    我也想可能ftp协议中的传送协议问题导致。但在sun.net.ftp的类库中,并没有提供设置命令的方法。上面的程序中,使用二进制和ASCII传送是相同的错误提示。
      

  6.   

    FTP 数字代码的意义 110 重新启动标记应答。 
    120 服务在多久时间内ready。 
    125 数据链路埠开启,准备传送。 
    150 文件状态正常,开启数据连接端口。 
    200 命令执行成功。 
    202 命令执行失败。 
    211 系统状态或是系统求助响应。 
    212 目录的状态。 
    213 文件的状态。 
    214 求助的讯息。 
    215 名称系统类型。 
    220 新的联机服务ready。 
    221 服务的控制连接埠关闭,可以注销。 
    225 数据连结开启,但无传输动作。 
    226 关闭数据连接端口,请求的文件操作成功。 
    227 进入passive mode。 
    230 使用者登入。 
    250 请求的文件操作完成。 
    257 显示目前的路径名称。 
    331 用户名称正确,需要密码。 
    332 登入时需要账号信息。 
    350 请求的操作需要进一部的命令。 
    421 无法提供服务,关闭控制连结。 
    425 无法开启数据链路。 
    426 关闭联机,终止传输。 
    450 请求的操作未执行。 
    451 命令终止:有本地的错误。 
    452 未执行命令:磁盘空间不足。 
    500 格式错误,无法识别命令。 
    501 参数语法错误。 
    502 命令执行失败。 
    503 命令顺序错误。 
    504 命令所接的参数不正确。 
    530 未登入。 
    532 储存文件需要账户登入。 
    550 未执行请求的操作。 
    551 请求的命令终止,类型未知。 
    552 请求的文件终止,储存位溢出。 
    553 未执行请求的的命令,名称不正确。