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;
}