Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3756814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:54:36+00:00 2026-05-19T09:54:36+00:00

How can I use the library to download a file and print out bytes

  • 0

How can I use the library to download a file and print out bytes saved? I tried using

import static org.apache.commons.io.FileUtils.copyURLToFile;
public static void Download() {

        URL dl = null;
        File fl = null;
        try {
            fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
            dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
            copyURLToFile(dl, fl);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

but I cannot display bytes or a progress bar. Which method should I use?

public class download {
    public static void Download() {
        URL dl = null;
        File fl = null;
        String x = null;
        try {
            fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
            dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
            OutputStream os = new FileOutputStream(fl);
            InputStream is = dl.openStream();
            CountingOutputStream count = new CountingOutputStream(os);
            dl.openConnection().getHeaderField("Content-Length");
            IOUtils.copy(is, os);//begin transfer

            os.close();//close streams
            is.close();//^
        } catch (Exception e) {
            System.out.println(e);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:54:36+00:00Added an answer on May 19, 2026 at 9:54 am

    If you are looking for a way to get the total number of bytes before downloading, you can obtain this value from the Content-Length header in http response.

    If you just want the final number of bytes after the download, it is easiest to check the file size you just write to.

    However if you want to display the current progress of how many bytes have been downloaded, you might want to extend apache CountingOutputStream to wrap the FileOutputStream so that everytime the write methods are called it counts the number of bytes passing through and update the progress bar.

    Update

    Here is a simple implementation of DownloadCountingOutputStream. I am not sure if you are familiar with using ActionListener or not but it is a useful class for implementing GUI.

    public class DownloadCountingOutputStream extends CountingOutputStream {
    
        private ActionListener listener = null;
    
        public DownloadCountingOutputStream(OutputStream out) {
            super(out);
        }
    
        public void setListener(ActionListener listener) {
            this.listener = listener;
        }
    
        @Override
        protected void afterWrite(int n) throws IOException {
            super.afterWrite(n);
            if (listener != null) {
                listener.actionPerformed(new ActionEvent(this, 0, null));
            }
        }
    
    }
    

    This is the usage sample :

    public class Downloader {
    
        private static class ProgressListener implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // e.getSource() gives you the object of DownloadCountingOutputStream
                // because you set it in the overriden method, afterWrite().
                System.out.println("Downloaded bytes : " + ((DownloadCountingOutputStream) e.getSource()).getByteCount());
            }
        }
    
        public static void main(String[] args) {
            URL dl = null;
            File fl = null;
            String x = null;
            OutputStream os = null;
            InputStream is = null;
            ProgressListener progressListener = new ProgressListener();
            try {
                fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
                dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
                os = new FileOutputStream(fl);
                is = dl.openStream();
    
                DownloadCountingOutputStream dcount = new DownloadCountingOutputStream(os);
                dcount.setListener(progressListener);
    
                // this line give you the total length of source stream as a String.
                // you may want to convert to integer and store this value to
                // calculate percentage of the progression.
                dl.openConnection().getHeaderField("Content-Length");
    
                // begin transfer by writing to dcount, not os.
                IOUtils.copy(is, dcount);
    
            } catch (Exception e) {
                System.out.println(e);
            } finally {
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(is);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any library out there that I can use to create epub files
I am using the SSHJ library ( https://github.com/shikhar/sshj ). I can use sshClient.newSCPFileTransfer(); to
Is there a C# library or something I can use to download credit card/bank
How can I use Enterprise Library to do a Data Access Layer in c#
I know I can use the RODBC library for accessing excel (.xls) docs from
What is a library I can use for CSS webkit support across multiple browsers
Is there a good C library that I can use in my client application
In python we can use multiprocessing modules. If there is a similar library in
I am wondering if there is a library one can use that calculates elapsed
I have turned a normal PHP class into a library so I can use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.