⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpconnection.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /**   * This command may be required by some servers to reserve sufficient   * storage to accommodate the new file to be transferred.   * It should be immediately followed by a <code>store</code> or   * <code>append</code>.   * @param size the number of bytes of storage to allocate   */  public void allocate(long size)    throws IOException  {    String cmd = ALLO + ' ' + size;    send(cmd);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 200:                  // OK      case 202:                  // Superfluous        break;      default:        throw new FTPException(response);      }  }    /**   * Renames a file.   * @param oldName the current name of the file   * @param newName the new name   * @return true if successful, false otherwise   */  public boolean rename(String oldName, String newName)    throws IOException  {    String cmd = RNFR + ' ' + oldName;    send(cmd);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 450:                  // File unavailable      case 550:                  // File not found        return false;      case 350:                 // Pending        break;      default:        throw new FTPException(response);      }    cmd = RNTO + ' ' + newName;    send(cmd);    response = getResponse();    switch (response.getCode())      {      case 250:                  // OK        return true;      case 450:      case 550:        return false;      default:        throw new FTPException(response);      }  }    /**   * Aborts the transfer in progress.   * @return true if a transfer was in progress, false otherwise   */  public boolean abort()    throws IOException  {    send(ABOR);    FTPResponse response = getResponse();    // Abort client DTP    if (dtp != null)      {        dtp.abort();      }    switch (response.getCode())      {      case 226:                  // successful abort        return false;      case 426:                 // interrupted        response = getResponse();        if (response.getCode() == 226)          {            return true;          }        // Otherwise fall through to throw exception      default:        throw new FTPException(response);      }  }    /**   * Causes the file specified to be deleted at the server site.   * @param filename the file to delete   */  public boolean delete(String filename)    throws IOException  {    String cmd = DELE + ' ' + filename;    send(cmd);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 250:                  // OK        return true;      case 450:                 // File unavailable      case 550:                 // File not found        return false;      default:        throw new FTPException(response);      }  }    /**   * Causes the directory specified to be deleted.   * This may be an absolute or relative pathname.   * @param pathname the directory to delete   */  public boolean removeDirectory(String pathname)    throws IOException  {    String cmd = RMD + ' ' + pathname;    send(cmd);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 250:                  // OK        return true;      case 550:                 // File not found        return false;      default:        throw new FTPException(response);      }  }  /**   * Causes the directory specified to be created at the server site.   * This may be an absolute or relative pathname.   * @param pathname the directory to create   */  public boolean makeDirectory(String pathname)    throws IOException  {    String cmd = MKD + ' ' + pathname;    send(cmd);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 257:                  // Directory created        return true;      case 550:                 // File not found        return false;      default:        throw new FTPException(response);      }  }    /**   * Returns the current working directory.   */  public String getWorkingDirectory()    throws IOException  {    send(PWD);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 257:        String message = response.getMessage();        if (message.charAt(0) == '"')          {            int end = message.indexOf('"', 1);            if (end == -1)              {                throw new ProtocolException(message);              }            return message.substring(1, end);          }        else          {            int end = message.indexOf(' ');            if (end == -1)              {                return message;              }            else              {                return message.substring(0, end);              }          }      default:        throw new FTPException(response);      }  }    /**   * Returns a listing of information about the specified pathname.   * If the pathname specifies a directory or other group of files, the   * server should transfer a list of files in the specified directory.   * If the pathname specifies a file then the server should send current   * information on the file.  A null argument implies the user's   * current working or default directory.   * @param pathname the context pathname, or null   */  public InputStream list(String pathname)    throws IOException  {    if (dtp == null || transferMode == MODE_STREAM)      {        initialiseDTP();      }    if (pathname == null)      {        send(LIST);      }    else      {        String cmd = LIST + ' ' + pathname;        send(cmd);      }    FTPResponse response = getResponse();    switch (response.getCode())      {      case 125:                  // Data connection already open; transfer starting      case 150:                  // File status okay; about to open data connection        return dtp.getInputStream();      default:        throw new FTPException(response);      }  }    /**   * Returns a directory listing. The pathname should specify a   * directory or other system-specific file group descriptor; a null   * argument implies the user's current working or default directory.   * @param pathname the directory pathname, or null   * @return a list of filenames(strings)   */  public List nameList(String pathname)    throws IOException  {    if (dtp == null || transferMode == MODE_STREAM)      {        initialiseDTP();      }    if (pathname == null)      {        send(NLST);      }    else      {        String cmd = NLST + ' ' + pathname;        send(cmd);      }    FTPResponse response = getResponse();    switch (response.getCode())      {      case 125:                  // Data connection already open; transfer starting      case 150:                  // File status okay; about to open data connection        InputStream in = dtp.getInputStream();        in = new BufferedInputStream(in);        in = new CRLFInputStream(in);     // TODO ensure that TYPE is correct        LineInputStream li = new LineInputStream(in);        List ret = new ArrayList();        for (String line = li.readLine();             line != null;             line = li.readLine())          {            ret.add(line);          }        li.close();        return ret;      default:        throw new FTPException(response);      }  }    /**   * Returns the type of operating system at the server.   */  public String system()    throws IOException  {    send(SYST);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 215:        String message = response.getMessage();        int end = message.indexOf(' ');        if (end == -1)          {            return message;          }        else          {            return message.substring(0, end);          }      default:        throw new FTPException(response);      }  }    /**   * Does nothing.   * This method can be used to ensure that the connection does not time   * out.   */  public void noop()    throws IOException  {    send(NOOP);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 200:        break;      default:        throw new FTPException(response);      }  }  // -- I/O --  /**   * Sends the specified command line to the server.   * The CRLF sequence is automatically appended.   * @param cmd the command line to send   */  protected void send(String cmd)    throws IOException  {    byte[] data = cmd.getBytes(US_ASCII);    out.write(data);    out.writeln();    out.flush();  }  /**   * Reads the next response from the server.   * If the server sends the "transfer complete" code, this is handled here,   * and the next response is passed to the caller.   */  protected FTPResponse getResponse()    throws IOException  {    FTPResponse response = readResponse();    if (response.getCode() == 226)      {        if (dtp != null)          {            dtp.transferComplete();          }        response = readResponse();      }    return response;  }  /**   * Reads and parses the next response from the server.   */  protected FTPResponse readResponse()    throws IOException  {    String line = in.readLine();    if (line == null)      {        throw new ProtocolException( "EOF");      }    if (line.length() < 4)      {        throw new ProtocolException(line);      }    int code = parseCode(line);    if (code == -1)      {        throw new ProtocolException(line);      }    char c = line.charAt(3);    if (c == ' ')      {        return new FTPResponse(code, line.substring(4));      }    else if (c == '-')      {        StringBuffer buf = new StringBuffer(line.substring(4));        buf.append('\n');        while(true)          {            line = in.readLine();            if (line == null)              {                throw new ProtocolException("EOF");              }            if (line.length() >= 4 &&                line.charAt(3) == ' ' &&                parseCode(line) == code)              {                return new FTPResponse(code, line.substring(4),                                        buf.toString());              }            else              {                buf.append(line);                buf.append('\n');              }          }      }    else      {        throw new ProtocolException(line);      }  }    /*   * Parses the 3-digit numeric code at the beginning of the given line.   * Returns -1 on failure.   */  static final int parseCode(String line)  {    char[] c = { line.charAt(0), line.charAt(1), line.charAt(2) };    int ret = 0;    for (int i = 0; i < 3; i++)      {        int digit =((int) c[i]) - 0x30;        if (digit < 0 || digit > 9)          {            return -1;          }        // Computing integer powers is way too expensive in Java!        switch (i)          {          case 0:            ret +=(100 * digit);            break;          case 1:            ret +=(10 * digit);            break;          case 2:            ret += digit;            break;          }      }    return ret;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -