Getting progress of download java

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
I have a SwingWorker where I download a file using FTP. It writes the data to an output stream. How can I track how much data has been downloaded so I can update a progress bar?

Why would http://pastebin.com/me89f548 not work (IndexOutOfBounds)
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
What line is the exception being thrown on/what's the stack? Where is size set?
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
What line is the exception being thrown on/what's the stack? Where is size set?

line 18.
2010-feb-03 21:59:11 simpleide.DownloadThread downloadFile
ALLVARLIG: null
java.lang.IndexOutOfBoundsException
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:88)
at simpleide.DownloadThread.downloadFile(DownloadThread.java:67)
at simpleide.DownloadThread.doInBackground(DownloadThread.java:44)
at simpleide.DownloadThread.doInBackground(DownloadThread.java:21)
at javax.swing.SwingWorker$1.call(SwingWorker.java:278)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at javax.swing.SwingWorker.run(SwingWorker.java:317)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

Line 67 == line 18 in the paste.

size is set from where I call it, but I removed it for now...
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Change line 18 to:
Code:
out.write(buffer, 0, bytesRead);
I'm sure you'll realize why and smack yourself in the head.
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Ok that didn't work. If the file was larger than a 32-bit integer can hold it doesn't work...

I got

Code:
 try {
            Main.bar.setIndeterminate(true);
            FileOutputStream out = new FileOutputStream(new File(localFile));
            ftp.connect(host, port);
            ftp.login(username, password);

            ftp.retrieveFile(file, out);
           
            Main.bar.setIndeterminate(false);
            

        } catch (Exception ex) {
            Logger.getLogger(DownloadThread.class.getName()).log(Level.SEVERE, null, ex);
        }

now... Can I somehow track progress anyways? I could use ftp.retriveFileInputStream() to get an inputstream, but I don't see that leading anywhere?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Ok that didn't work. If the file was larger than a 32-bit integer can hold it doesn't work...
How big are the files you're downloading? 32 bits for a signed integer gets you up to 2 GiB.

If size and current are overflowing, try changing them to longs. How are you setting size?
 
Top