コード例 #1
0
  /** Lee del flujo de entrada todos lo bytes y los tira. */
  private void tirarBytes() throws IOException {
    cFtp ftp = cFtp.getFTP();

    try {
      this.file = new File("nulo");

      ftp.insertRecepcionString(
          "Transfer just initiated. Waiting next transmission to join...", "icono_informacion");
      while (protocolcFTP.esActiva() && (this.file != null)) {

        byte[] bytes = new byte[this.id_socketIn.available()];
        // Log.log("Bytes disponibles: "+bytes.length,"");
        int iBytesLeidos = this.id_socketIn.read(bytes);

        // FIN DE FLUJO???...
        if (iBytesLeidos == -1) {
          Log.log("tirar bytes : FIN DE FLUJO**************************", "");
          return;
        }
      }
    } finally {
      this.file = null;
    }
  }
コード例 #2
0
  /** Recibir UN FICHERO */
  private void receiveFile() throws IOException {
    this.file = null;

    // Log.log("receiveFile()","");

    // ---------------------------------
    // Recibir ID...
    if (!receiveIDFTPMulticast()) {
      tirarBytes();
      return;
    }

    // Recibir FileSize...
    lFileSize = this.receiveFileSize();
    if (lFileSize <= 0) {
      tirarBytes();
      return;
    }

    // Recibir FileName...
    sFileName = this.receiveFileName();
    if (sFileName == null) {
      tirarBytes();
      return;
    }

    // Fichero temporal..
    this.file = File.createTempFile("org.clusternet.ftp" + System.currentTimeMillis(), ".tmp");

    // Comprobar si ya existe.
    if (this.file.exists()) {
      if (!this.file.delete())
        throw new IOException(
            "The file could not deleted "
                + sFileName
                + newline
                + "1. Check you hace privilegies."
                + newline
                + "2.Check the file is not used by another application.");
    }

    // Crear el fichero...
    if (!this.file.createNewFile()) {
      // mensajeErrorEscritura();
      throw new IOException(
          "The file could not be closed "
              + sFileName
              + newline
              + "1. Check you hace privilegies."
              + newline
              + "2.Check the file is not used by another application.");
    }

    // Comprobar si se puede escribir.
    if (!this.file.canWrite()) {
      // mensajeErrorEscritura();
      throw new IOException(
          "The file could not be writed  "
              + sFileName
              + newline
              + "1. Check you hace privilegies."
              + newline
              + "2.Check the file is not used by another application.");
    }

    // Flujo de salida al fichero...
    this.fileOutputStream = new FileOutputStream(file);

    // Iniciar JDialogRecepcion
    this.jDialogRecepcion =
        new JDialogRecepcion(
            this,
            null,
            "Receiving " + this.id_socketIn.getID_Socket(),
            false,
            sFileName,
            lFileSize,
            null,
            this.id_socketIn.getID_Socket());
    this.jDialogRecepcion.show();

    try {
      if (protocolcFTP.getModo() == ClusterNet.MODE_DELAYED_RELIABLE
          || protocolcFTP.getModo() == ClusterNet.MODE_RELIABLE) {
        cFtp.getFTP().insertRecepcionString("Initializing reception of " + sFileName, null);

        lBytesLeidos = 0;
        byte[] bytes = new byte[1024 * 2];

        while (protocolcFTP.esActiva() /* && (lBytesLeidos < lFileSize) */ && (this.file != null)) {

          // if(this.id_socketIn.available() > 0)
          // {
          // Log.log("Bytes disponibles: "+bytes.length,"");
          int iBytesLeidos = this.id_socketIn.read(bytes);

          // FIN DE FLUJO???...
          if (iBytesLeidos == -1) {
            Log.log("FILERECPECION -> RECEIVEFILE : FIN DE FLUJO*", "");
            break;
          }

          // Ajustar tamaño...
          lBytesLeidos += iBytesLeidos;
          this.jDialogRecepcion.setBytesRecibidos(lBytesLeidos);
          try {
            this.fileOutputStream.write(bytes, 0, iBytesLeidos);
          } catch (IOException e) {
            mensajeErrorEscribiendo(e.getMessage());
            throw e;
          }

          // }
          // else
          //  ClusterTimer.sleep(10);
        }

        // Mostrar resumen de recepción...
        this.resumenRecepcion();

      } else {
        // NUNCA DEBE DE ENTRAR AQUI.
      }
    } finally {

      // Eliminar este objeto del treemap en la clase ProtocolcFTP
      if (this.protocolcFTP.getModo() == ClusterNet.MODE_DELAYED_RELIABLE
          || protocolcFTP.getModo() == ClusterNet.MODE_RELIABLE) {
        this.protocolcFTP.removeFileRecepcion(this.id_socketIn);
      } else {
        this.protocolcFTP.removeFileRecepcion(this.id_socket);
      }

      // desactivar el diálogo de recepción...
      if (this.jDialogRecepcion != null) this.jDialogRecepcion.setVisible(false);

      // Cerrar  Flujos...
      this.id_socketIn.close();
      this.id_socketIn = null;

      if (this.fileOutputStream != null) this.fileOutputStream.close();

      if (protocolcFTP.esActiva() && this.file != null) {
        // Cambiar Localización y Nombre del fichero...
        File MFTPfile = new File(sFileName);
        if (MFTPfile.exists()) {
          if (this.mensajeFileExists()) {
            if (!MFTPfile.delete()) {
              this.protocolcFTP
                  .getFTP()
                  .error(
                      "The file can't be deleted: "
                          + sFileName
                          + newline
                          + "1. Check you hace privilegies."
                          + newline
                          + "2.Check the file is not used by another application.");
              // Eliminar temporal
              this.file.delete();
              this.file = null;
              return;
            }

            if (!file.renameTo(MFTPfile)) {
              this.protocolcFTP
                  .getFTP()
                  .error(
                      "The file can't be renamed:"
                          + sFileName
                          + newline
                          + "1. Check you hace privilegies."
                          + newline
                          + "2.Check the file is not used by another application.");
              // Eliminar temporal
              this.file.delete();
              this.file = null;
            }
          }
        } else if (!file.renameTo(MFTPfile)) {
          this.protocolcFTP
              .getFTP()
              .error(
                  "The file can't be renamed:"
                      + sFileName
                      + newline
                      + "1. Check you hace privilegies."
                      + newline
                      + "2.Check the file is not used by another application.");
          // Eliminar temporal
          this.file.delete();
          this.file = null;
        }
      }
    }
  }