|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
[java] riprendere download
Seguendo questo esempio sto realizzando un piccolo download manager in java. Ho un problema pero': se chiudo e riapro il programma la coda di download va persa. Ho provato a serializzare i vari oggetti "Download" ma riaprendo il programma ottengo un stream error... Come posso risolvere?
Grazie |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Per quale motivo non la dovrebbe perdere ?
|
|
|
|
|
|
#3 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Cioè come puoi pretendere di interrompere così una connessione http e di poterla riprendere semplicemente riprendendo lo stato dell'oggetto da dove ti eri fermato ? Non credi che il server vada in timeout ?
Per implementare il resume suppongo che tu debba farti a mano la richiesta http verso il server inserendo nell'header il punto dal quale vuoi ricominciare il download. Per l'ftp esiste un comando chiamato REST, |
|
|
|
|
|
#5 | |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
Quote:
Ecco cosa mi sfuggiva, ora ho capito Certe volte mi stupisco della mia stupidita' ^^' Grazie Ultima modifica di afsdfdlecosdfsfdcco : 13-12-2007 alle 22:15. |
|
|
|
|
|
|
#6 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
Ok, sono riuscito a far riprendere "correttamente" il download anche dopo aver chiuso/riaperto il programma. Semplicemente ho memorizzato in un file xml la dimensione del file da scaricare ed il punto in cui sono arrivato (oltre all'url ovviamente).
Ora ho un'altro problema: tutto cio' che scarico in questo modo mi da problemi (cioe' e' un file danneggiato)! Facendo un po' di prove e controlli ho scoperto che (nel caso chiudo e riapro il download) il totale di byte scaricati e' maggiore rispetto alla dimensione del file: Codice:
<root>
<download>
<url>http://img412.imageshack.us/img412/1803/0041hx2vf4.gif</url>
<downloaded>42048</downloaded>
<size>41024</size>
</download>
</root>
|
|
|
|
|
|
#7 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
Facendo qualche altra prova sembra che che al momento della pausa si perda qualche byte (o me li scarica 2 volte)... Non capisco come mai pero'! Se stoppo e faccio ripartire (senza uscire) tutto ok, se stoppo, esco, rietro e faccio ripartire al termine del download mi trovo un file danneggiato!
Ho notato poi che un download senza la chiusura del programma termina quando stream.read(buffer) mi restituisce -1 mentre un download ripreso dopo la chiusura del programma non mi restituisce mai -1 ma alla fine restituisce per qualche ciclo 0 e poi supera la dimensione di partenza... boh Qualcuno sa aiutarmi? Grazie ^^ |
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Mi dici le istruzioni che usi per riprendere il download ?
Ultima modifica di cionci : 15-12-2007 alle 02:38. |
|
|
|
|
|
#9 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
Allora, questo e' il metodo che si occupa del download (funziona in parallelo):
Codice:
public void run()
{
RandomAccessFile file = null;
InputStream stream = null;
try
{
// Open connection to URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// Connect to server.
connection.connect();
// Make sure response code is in the 200 range.
if (connection.getResponseCode() / 100 != 2)
{
error();
}
// Check for valid content length.
int contentLength = connection.getContentLength();
if (contentLength < 1)
{
error();
}
// Set the size for this download if it hasn't been already set.
if (size == -1)
{
size = contentLength;
stateChanged();
}
// Open file and seek to the end of it.
file = new RandomAccessFile(getFileName(url), "rw");
file.seek(downloaded);
stream = connection.getInputStream();
while (status == DOWNLOADING)
{
/* Size buffer according to how much of the file is left to download. */
byte buffer[];
if (size - downloaded > MAX_BUFFER_SIZE)
{
buffer = new byte[MAX_BUFFER_SIZE];
}
else
{
buffer = new byte[size - downloaded];
}
// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
{
break;
}
// Write buffer to file.
file.write(buffer, 0, read);
downloaded += read;
stateChanged();
}
/* Change status to complete if this point was
reached because downloading has finished. */
if (status == DOWNLOADING)
{
status = COMPLETE;
stateChanged();
}
}
catch (Exception e)
{
error();
}
finally
{
// Close file.
if (file != null)
{
try
{
file.close();
}
catch (Exception e)
{
}
}
// Close connection to server.
if (stream != null)
{
try
{
stream.close();
}
catch (Exception e)
{
}
}
}
}
Questo invece e' il costruttore usato quando carico i dati dal file xml (per sapere dove sono arrivato): Codice:
public Download(URL url, int downloaded, int size)
{
this.url = url;
this.downloaded = downloaded;
this.size = size;
status = DOWNLOADING;
// Begin the download.
download(); // crea e fa partire il thread per il download
}
Ultima modifica di afsdfdlecosdfsfdcco : 15-12-2007 alle 12:23. |
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Ad occhio mi sembra che torni. Prova a scaricare un file di testo bello lungo, in questo modo riesci a capire più o meno cosa succede.
Prova inoltre a verificare se il content-length ritornato la seconda volta più la quantità di byte già scaricata è uguale al content-length ritornato la prima volta. |
|
|
|
|
|
#11 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
|
|
|
|
|
|
#12 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
Tutto ok un corno :|
Ho aggiunto un po' di println. Prima esecuzione: faccio partire il download, interrompo ed esco. CONTENT LENGTH/DOWNLOADED = 21894/0 Downloaded = 973/21894 Downloaded = 1997/21894 Downloaded = 2433/21894 Downloaded = 3457/21894 Downloaded = 3893/21894 PAUSE Downloaded = 4917/21894 Sul file viene scritto questo: Codice PHP:
CONTENT LENGTH/DOWNLOADED = 16977/4917 Downloaded = 5941/16977 Downloaded = 6377/16977 CONTENT LENGTH/DOWNLOADED = 16977/6377 Downloaded = 7401/16977 Downloaded = 7837/16977 Downloaded = 8861/16977 Downloaded = 9297/16977 Downloaded = 10321/16977 Downloaded = 10757/16977 Downloaded = 11781/16977 Downloaded = 12217/16977 Downloaded = 13241/16977 Downloaded = 13677/16977 Downloaded = 14701/16977 Downloaded = 15137/16977 Downloaded = 16161/16977 Downloaded = 16597/16977 Downloaded = 16977/16977 Downloaded = 16977/16977 Downloaded = 16977/16977 Downloaded = 16977/16977 [...] Downloaded = 16977/16977 Downloaded = 16977/16977 Downloaded = 16977/16977 Downloaded = 16977/16977 Downloaded = 18001/16977 Downloaded = 18001/16977 E qui stoppo. Sul file viene scritto questo: Codice PHP:
Intanto grazie ^^ Ah, questo e' run con i println: Codice:
// Download file.
public void run()
{
RandomAccessFile file = null;
InputStream stream = null;
try
{
// Open connection to URL.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// Connect to server.
connection.connect();
// Make sure response code is in the 200 range.
if (connection.getResponseCode() / 100 != 2)
{
error();
}
// Check for valid content length.
int contentLength = connection.getContentLength();
System.out.println("CONTENT LENGTH/DOWNLOADED = " + contentLength + "/" + downloaded);
if (contentLength < 1)
{
error();
}
// Set the size for this download if it hasn't been already set.
if (size == -1)
{
size = contentLength;
stateChanged();
}
// Open file and seek to the end of it.
file = new RandomAccessFile(getFileName(url), "rw");
file.seek(downloaded);
stream = connection.getInputStream();
while (status == DOWNLOADING)
{
/* Size buffer according to how much of the file is left to download. */
byte buffer[];
if (size - downloaded > MAX_BUFFER_SIZE)
{
buffer = new byte[MAX_BUFFER_SIZE];
}
else
{
buffer = new byte[size - downloaded];
}
// Read from server into buffer.
int read = stream.read(buffer);
if (read == -1)
{
break;
}
// Write buffer to file.
file.write(buffer, 0, read);
downloaded += read;
System.out.println("Downloaded = " + downloaded + "/" + size);
stateChanged();
}
/* Change status to complete if this point was
reached because downloading has finished. */
if (status == DOWNLOADING)
{
status = COMPLETE;
stateChanged();
}
}
catch (Exception e)
{
error();
}
finally
{
// Close file.
if (file != null)
{
try
{
file.close();
}
catch (Exception e)
{
}
}
// Close connection to server.
if (stream != null)
{
try
{
stream.close();
}
catch (Exception e)
{
}
}
}
}
|
|
|
|
|
|
#13 |
|
Registered User
Iscritto dal: Jul 2007
Messaggi: 915
|
RISOLTO!!!!
Era una stupidata: praticamente dovevo azzerare il valore di "downloaded" dopo che l'ho usato per posizionarmi correttamente nel file da scaricare! Grazie per la pazienza |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:42.




















