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

📄 ftpconnection.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    throws IOException  {    send(CDUP);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 250:        return true;      case 550:        return false;      default:        throw new FTPException(response);      }  }  /**   * Terminates an authenticated login.   * If file transfer is in progress, it remains active for result response   * only.   */  public void reinitialize()    throws IOException  {    send(REIN);    FTPResponse response = getResponse();    switch (response.getCode())      {      case 220:        if (dtp != null)          {            dtp.complete();            dtp = null;          }        break;      default:        throw new FTPException(response);      }  }  /**   * Terminates the control connection.   * The file transfer connection remains open for result response only.   * This connection is invalid and no further commands may be issued.   */  public void logout()    throws IOException  {    send(QUIT);    try      {        getResponse();            // not required      }    catch (IOException e)      {      }    if (dtp != null)      {        dtp.complete();        dtp = null;      }    try      {        socket.close();      }    catch (IOException e)      {      }  }    /**   * Initialise the data transfer process.   */  protected void initialiseDTP()    throws IOException  {    if (dtp != null)      {        dtp.complete();        dtp = null;      }        InetAddress localhost = socket.getLocalAddress();    if (passive)      {        send(PASV);        FTPResponse response = getResponse();        switch (response.getCode())          {          case 227:            String message = response.getMessage();            try              {                int start = message.indexOf(',');                char c = message.charAt(start - 1);                while (c >= 0x30 && c <= 0x39)                  {                    c = message.charAt((--start) - 1);                  }                int mid1 = start;                for (int i = 0; i < 4; i++)                  {                    mid1 = message.indexOf(',', mid1 + 1);                  }                int mid2 = message.indexOf(',', mid1 + 1);                if (mid1 == -1 || mid2 < mid1)                  {                    throw new ProtocolException("Malformed 227: " +                                                 message);                  }                int end = mid2;                c = message.charAt(end + 1);                while (c >= 0x30 && c <= 0x39)                  {                    c = message.charAt((++end) + 1);                  }                                String address =                  message.substring(start, mid1).replace(',', '.');                int port_hi =                  Integer.parseInt(message.substring(mid1 + 1, mid2));                int port_lo =                  Integer.parseInt(message.substring(mid2 + 1, end + 1));                int port = (port_hi << 8) | port_lo;                                /*System.out.println("Entering passive mode: " + address +                  ":" + port);*/                dtp = new PassiveModeDTP(address, port, localhost,                                          connectionTimeout, timeout);                break;              }            catch (ArrayIndexOutOfBoundsException e)              {                throw new ProtocolException(e.getMessage() + ": " +                                             message);              }            catch (NumberFormatException e)              {                throw new ProtocolException(e.getMessage() + ": " +                                             message);              }          default:            throw new FTPException(response);          }      }    else      {        // Get the local port        int port = socket.getLocalPort() + 1;        int tries = 0;        // Bind the active mode DTP        while (dtp == null)          {            try              {                dtp = new ActiveModeDTP(localhost, port,                                         connectionTimeout, timeout);                /*System.out.println("Listening on: " + port);*/              }            catch (BindException e)              {                port++;                tries++;                if (tries > 9)                  {                    throw e;                  }              }          }                // Send PORT command        StringBuffer buf = new StringBuffer(PORT);        buf.append(' ');        // Construct the address/port string form        byte[] address = localhost.getAddress();        for (int i = 0; i < address.length; i++)          {            int a =(int) address[i];            if (a < 0)              {                a += 0x100;              }            buf.append(a);            buf.append(',');          }        int port_hi =(port & 0xff00) >> 8;        int port_lo =(port & 0x00ff);        buf.append(port_hi);        buf.append(',');        buf.append(port_lo);        send(buf.toString());        // Get response        FTPResponse response = getResponse();        switch (response.getCode())          {          case 200:                // OK            break;          default:            dtp.abort();            dtp = null;            throw new FTPException(response);          }      }    dtp.setTransferMode(transferMode);  }    /**   * Set passive mode.   * @param flag true if we should use passive mode, false otherwise   */  public void setPassive(boolean flag)    throws IOException  {    if (passive != flag)      {        passive = flag;        initialiseDTP();      }  }    /**   * Returns the current representation type of the transfer data.   * @return TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY   */  public int getRepresentationType()  {    return representationType;  }  /**   * Sets the desired representation type of the transfer data.   * @param type TYPE_ASCII, TYPE_EBCDIC, or TYPE_BINARY   */  public void setRepresentationType(int type)    throws IOException  {    StringBuffer buf = new StringBuffer(TYPE);    buf.append(' ');    switch (type)      {      case TYPE_ASCII:        buf.append('A');        break;      case TYPE_EBCDIC:        buf.append('E');        break;      case TYPE_BINARY:        buf.append('I');        break;      default:        throw new IllegalArgumentException(Integer.toString(type));      }    //buf.append(' ');    //buf.append('N');    send(buf.toString());    FTPResponse response = getResponse();    switch (response.getCode())      {      case 200:        representationType = type;        break;      default:        throw new FTPException(response);      }  }  /**   * Returns the current file structure type.   * @return STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE   */  public int getFileStructure()  {    return fileStructure;  }  /**   * Sets the desired file structure type.   * @param structure STRUCTURE_FILE, STRUCTURE_RECORD, or STRUCTURE_PAGE   */  public void setFileStructure(int structure)    throws IOException  {    StringBuffer buf = new StringBuffer(STRU);    buf.append(' ');    switch (structure)      {      case STRUCTURE_FILE:        buf.append('F');        break;      case STRUCTURE_RECORD:        buf.append('R');        break;      case STRUCTURE_PAGE:        buf.append('P');        break;      default:        throw new IllegalArgumentException(Integer.toString(structure));      }    send(buf.toString());    FTPResponse response = getResponse();    switch (response.getCode())      {      case 200:        fileStructure = structure;        break;      default:        throw new FTPException(response);      }  }  /**   * Returns the current transfer mode.   * @return MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED   */  public int getTransferMode()  {    return transferMode;  }  /**   * Sets the desired transfer mode.   * @param mode MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED   */  public void setTransferMode(int mode)    throws IOException  {    StringBuffer buf = new StringBuffer(MODE);    buf.append(' ');    switch (mode)      {      case MODE_STREAM:        buf.append('S');        break;      case MODE_BLOCK:        buf.append('B');        break;      case MODE_COMPRESSED:        buf.append('C');        break;      default:        throw new IllegalArgumentException(Integer.toString(mode));      }    send(buf.toString());    FTPResponse response = getResponse();    switch (response.getCode())      {      case 200:        transferMode = mode;        if (dtp != null)          {            dtp.setTransferMode(mode);          }        break;      default:        throw new FTPException(response);      }  }    /**   * Retrieves the specified file.   * @param filename the filename of the file to retrieve   * @return an InputStream containing the file content   */  public InputStream retrieve(String filename)    throws IOException  {    if (dtp == null || transferMode == MODE_STREAM)      {        initialiseDTP();      }    /*       int size = -1;       String cmd = SIZE + ' ' + filename;       send(cmd);       FTPResponse response = getResponse();       switch (response.getCode())       {       case 213:       size = Integer.parseInt(response.getMessage());       break;       case 550: // File not found       default:       throw new FTPException(response);       }     */    String cmd = RETR + ' ' + filename;    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 stream for uploading a file.   * If a file with the same filename already exists on the server, it will   * be overwritten.   * @param filename the name of the file to save the content as   * @return an OutputStream to write the file data to   */  public OutputStream store(String filename)    throws IOException  {    if (dtp == null || transferMode == MODE_STREAM)      {        initialiseDTP();      }    String cmd = STOR + ' ' + filename;    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.getOutputStream();      default:        throw new FTPException(response);      }  }  /**   * Returns a stream for uploading a file.   * If a file with the same filename already exists on the server, the   * content specified will be appended to the existing file.   * @param filename the name of the file to save the content as   * @return an OutputStream to write the file data to   */  public OutputStream append(String filename)    throws IOException  {    if (dtp == null || transferMode == MODE_STREAM)      {        initialiseDTP();      }    String cmd = APPE + ' ' + filename;    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.getOutputStream();      default:        throw new FTPException(response);      }  }

⌨️ 快捷键说明

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