Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente bannato
    Registrato dal
    Jul 2003
    Messaggi
    404

    Impostare Ciclo Timeout

    Salve amici del Forum.
    Avrei bisogno di un piccolo aiutino da parte Vostra.
    La cosa che più mi interessa e impostare un ciclo (While o For) per un tempo massimo per esempio di 10 secondi.
    Mi spiego meglio ad un certo punto del mio programma mi collego al server FTP per prelevare un file, fino ad ora ho fatto un esempio che il file esiste sempre quindi tutto bene, se il file non esiste il while mi va in loop, quindi dovrei impostare un limite max di secondi e se non trova il file specificato mi esce e ritorna false.
    Vi posto la mia routine magari se potete darmi un aiuto:
    public boolean download(String nomeFile){
    boolean trovato = false;

    InputStream inStream=null;
    URL url;

    try {
    url = new URL(urlPath + nomeFile);
    }
    catch (MalformedURLException e) {
    return false;
    }
    catch (IOException ie) {
    return false;
    }
    //In pratica qui dovrei impostare il ciclo di TimeOut
    while (!trovato) {
    try {
    inStream = url.openStream();
    if (inStream.available() > 0)
    trovato = true;
    else
    trovato = false;
    }
    catch (IOException ie) {
    trovato = false;
    }

    }
    try {
    FileOutputStream outStream = new FileOutputStream(percorsoCartine + nomeFile);

    int numByte;
    byte buffer[] = new byte[2048];
    while ((numByte = inStream.read(buffer,0,buffer.length))>=0) {
    outStream.write(buffer,0,numByte);
    }
    outStream.close();
    inStream.close();
    }
    catch (IOException e) {
    return false;
    }
    return true;
    }

  2. #2
    Utente di HTML.it L'avatar di Angelo1974
    Registrato dal
    Feb 2003
    Messaggi
    1,107
    Ciao; scusami am se non trova il file non ti genera una IOException io fossi in te farei:

    Codice PHP:
    public boolean download(String nomeFile){ 
    boolean trovato false

    InputStream inStream=null
    URL url

    try { 

    url = new URL(urlPath nomeFile); 
    while (!
    trovato) { 

    inStream url.openStream(); 
    if (
    inStream.available() > 0
    trovato true
    else 
    trovato false
    }

    catch (
    MalformedURLException e) { 
    return 
    false

    catch (
    IOException ie) { 
    return 
    false
    }

    try { 
    FileOutputStream outStream = new FileOutputStream(percorsoCartine nomeFile); 

    int numByte
    byte buffer[] = new byte[2048]; 
    while ((
    numByte inStream.read(buffer,0,buffer.length))>=0) { 
    outStream.write(buffer,0,numByte); 

    outStream.close(); 
    inStream.close(); 

    catch (
    IOException e) { 
    return 
    false

    return 
    true

    Se vuoi trovare l'arcobaleno, devi sopportare la pioggia

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.