[java] download a file and get size, progress

Pagina: 1
Acties:

  • Biet80
  • Registratie: Februari 2002
  • Laatst online: 00:59
Ik heb de volgende code om een file te downloaden, maar ik wil graag van te voren weten hoeveel regels of bytes ik op ga halen. Ik weet niet waar ik nu moet zoeken. Ik heb al geprobeerd te googlen " java download file size", maar met weinig resultaat. :r

Het doel is om straks progress bij te houden.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
            in = url.openStream();
            InputStreamReader inR = new InputStreamReader(in);
            BufferedReader buf = new BufferedReader(inR);
            PrintWriter out = new PrintWriter(new FileOutputStream(new File(
                    file2Write)));
            
            String line = "";
            System.out.println(new Date() + " Starting download...");
            
            while ((line = buf.readLine()) != null)
            {
                out.write(line + "\n");             
            }
            
            out.close();
            in.close();
            System.out.println(new Date() + " Download finished...");

Water is pas echt lekker als het bij de brouwerij is geweest...


Verwijderd

Je kunt met URLConnection gaan spelen.
Java:
1
2
URLConnection con = url.openConnection();
int contentLength = con.getContentLength();

  • Standeman
  • Registratie: November 2000
  • Laatst online: 21:40

Standeman

Prutser 1e klasse

Met ^^

Of:
Java:
1
2
File file = new File(uri);
long size = file.length();

The ships hung in the sky in much the same way that bricks don’t.


  • Biet80
  • Registratie: Februari 2002
  • Laatst online: 00:59
_/-\o_ Tnx, dit was precies wat ik zocht _/-\o_

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
            in = url.openStream();
            InputStreamReader inR = new InputStreamReader(in);
            BufferedReader buf = new BufferedReader(inR);
            PrintWriter out = new PrintWriter(new FileOutputStream(new File(
                    file2Write)));
            
            String line = "";
            System.out.println(new Date() + " Starting download...");
            URLConnection con = url.openConnection();
            int contentLength = con.getContentLength();
            int downloaded = 0;
            
            while ((line = buf.readLine()) != null && !which)
            {
                downloaded += line.getBytes().length;
                out.write(line + "\n");             
            }
            
            out.close();
            in.close();
            System.out.println(new Date() + " Download finished...");


Ps. hoe zet ik dit op 'opgelost' of gesloten ?

[ Voor 8% gewijzigd door Biet80 op 21-03-2006 12:05 ]

Water is pas echt lekker als het bij de brouwerij is geweest...


  • bvp
  • Registratie: Maart 2005
  • Laatst online: 23-02 12:02

bvp

Biet80 schreef op dinsdag 21 maart 2006 @ 12:03:
_/-\o_ Tnx, dit was precies wat ik zocht _/-\o_


Ps. hoe zet ik dit op 'opgelost' of gesloten ?
Hoeft niet, zo hebben andere ook wat aan dit topic ev. ;)

  • Robtimus
  • Registratie: November 2002
  • Laatst online: 18:30

Robtimus

me Robtimus no like you

Biet80 schreef op dinsdag 21 maart 2006 @ 12:03:
_/-\o_ Tnx, dit was precies wat ik zocht _/-\o_

code:
1
code


Ps. hoe zet ik dit op 'opgelost' of gesloten ?
Je beseft nu dat je 2 connecties naar je URL opent? Uit de API van URL.openStream:
This method is a shorthand for:

openConnection().getInputStream()
Je kunt dus beter EERST openConnection() aanroepen, en dan daarvan getInputStream() aanroepen.

Vergeet trouwens ook niet te controleren of getContentLength() -1 returned, dan kun je beter een indeterminate progress bar tonen omdat gewoon niet bekend is hoe groot de file is die je inleest.

[ Voor 18% gewijzigd door Robtimus op 21-03-2006 13:16 ]

More than meets the eye
There is no I in TEAM... but there is ME
system specs


  • Biet80
  • Registratie: Februari 2002
  • Laatst online: 00:59
bedankt IcemanX,
Als ik nu de inputstream sluit is dan de connectie ook gesloten ?
void java.io.InputStream.close()
Closes this input stream and releases any system resources associated with the stream.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
            URLConnection con = url.openConnection();
            int contentLength = con.getContentLength();
            in = con.getInputStream();
            InputStreamReader inR = new InputStreamReader(in);
            BufferedReader buf = new BufferedReader(inR);
            PrintWriter out = new PrintWriter(new FileOutputStream(new File(
                    file2Write)));
            
            String line = "";
            System.out.println(new Date() + " Starting download...");
            
            
            
            while ((line = buf.readLine()) != null)
            {
                downloaded += line.getBytes().length;
                out.write(line + "\n");             
            }
            
            out.close();
            in.close();
            System.out.println(new Date() + " Download finished...");

[ Voor 12% gewijzigd door Biet80 op 21-03-2006 13:34 ]

Water is pas echt lekker als het bij de brouwerij is geweest...


  • PhoneTech
  • Registratie: Mei 2000
  • Laatst online: 23-02 19:42
Ik raad de URLConnection class van Sun compleet af. Verschrikkelijk buggy, en dat ding cached alles eerst naar de heap voordat het een stream in gaat (of uit)

Als je bestanden gaat versturen over http wil je echt niet dat bij het uploaden de boel eerst in de heap gezet wordt om vervolgens als 1 brok verstuurd te worden. Bij het ontvangen precies hetzelfde.

Met Java 5 kan je inmiddels de chunked streamingmode gebruiken maar dat rammelt nog aan alle kanten -> serverCon.setChunkedStreamingMode(1024 * 4);

Als ik jou was, zou ik zo snel mogelijk naar Jakarta Commons Http Client switchen -> http://jakarta.apache.org/commons/httpclient/index.html
Deze biedt directe in en output naar streams zonder eerst naar de heap te streamen en vele performance optimalisaties.

  • Standeman
  • Registratie: November 2000
  • Laatst online: 21:40

Standeman

Prutser 1e klasse

PhoneTech schreef op dinsdag 21 maart 2006 @ 17:45:
Ik raad de URLConnection class van Sun compleet af. Verschrikkelijk buggy, en dat ding cached alles eerst naar de heap voordat het een stream in gaat (of uit)

Als je bestanden gaat versturen over http wil je echt niet dat bij het uploaden de boel eerst in de heap gezet wordt om vervolgens als 1 brok verstuurd te worden. Bij het ontvangen precies hetzelfde.

Met Java 5 kan je inmiddels de chunked streamingmode gebruiken maar dat rammelt nog aan alle kanten -> serverCon.setChunkedStreamingMode(1024 * 4);

Als ik jou was, zou ik zo snel mogelijk naar Jakarta Commons Http Client switchen -> http://jakarta.apache.org/commons/httpclient/index.html
Deze biedt directe in en output naar streams zonder eerst naar de heap te streamen en vele performance optimalisaties.
Of het maakt je gewoon geen moer uit omdat het een school / klooi / performance* project is..

Kan ook!

* doorhalen wat niet van toepassing is.

The ships hung in the sky in much the same way that bricks don’t.

Pagina: 1