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

  • Home
  • SEARCH
  • 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 8163133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:57:09+00:00 2026-06-06T18:57:09+00:00

I am trying to download an exe file with following code, any idea why

  • 0

I am trying to download an exe file with following code, any idea why it downloads only ~30% of the file? At least it doesn’t throw any exceptions.

My main method looks like this: new DownloadWorker().execute();

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.SwingWorker;

public final class DownloadWorker extends SwingWorker<Object, Object> {

    @Override
    protected Object doInBackground() throws Exception {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;

        try {
            URL url = new URL("http://download.piriform.com/ccsetup320.exe");
            URLConnection conn = url.openConnection();
            conn.connect();

            int fileLength = conn.getContentLength();

            in = new BufferedInputStream(url.openStream());
            out = new BufferedOutputStream(new FileOutputStream("ccsetup320.exe"));

            byte[] buffer = new byte[4096];
            long total = 0;
            int bytesRead = 0;
            while ( (bytesRead = in.read(buffer)) != -1 ) {
                total += bytesRead;
                System.out.println((int) (total * 100 / fileLength));
                out.write(buffer, 0, bytesRead);
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        } finally {
            if ( out != null ) {
                out.flush();
                out.close();
            }
            if ( in != null ) {
                in.close();
            }
        }

        return null;
    }

    @Override
    protected void done() {

    }

}

Thanks.

  • 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-06-06T18:57:11+00:00Added an answer on June 6, 2026 at 6:57 pm

    Have you tried using Java NIO seen as you have Java 7 installed:

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.nio.channels.Channels;
    import java.nio.channels.ReadableByteChannel;
    
    
    public class TestApp {
    
        public static void main(String[] args) {
            try {
                URL url = new URL("http://download.piriform.com/ccsetup320.exe");
                ReadableByteChannel rbc = Channels.newChannel(url.openStream());
                FileOutputStream fos = new FileOutputStream("c:/ccsetup320.exe");
                fos.getChannel().transferFrom(rbc, 0, 1 << 24);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
    }
    

    The above code is tested and works just fine, I got it from a similar question found here we can see how the NIO makes coding so much easier 🙂

    EDIT:

    I have updated the code to use a swingworker and it downloads the file without a problem:

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.nio.channels.Channels;
    import java.nio.channels.ReadableByteChannel;
    import javax.swing.SwingWorker;
    
    public class JavaApplication174 {
    
        public static void main(String[] args) {
            SwingWorker worker = new SwingWorker() {
    
                @Override
                protected Object doInBackground() throws Exception {
    
                    try {
                        URL google = new URL("http://download.piriform.com/ccsetup320.exe");
                        ReadableByteChannel rbc = Channels.newChannel(google.openStream());
                        FileOutputStream fos = new FileOutputStream("c:/ccsetup320.exe");
                        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    return this;
                }
            };
            worker.run();
        }
    }
    

    EDIT 2:

    You call execute() on your workers instance use run() instead works for me!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have customers who are trying to download a setup.exe file over mobile connections
I am trying to write a code to download file from server. i am
I'm trying to upload an entire folder for download to Google code. The exe
I am trying to download a file from a website using python and mechanize.
I am trying to download a PDF file with HttpClient. I am able to
I'm trying to download the XLS file from this page: http://www.nordpoolspot.com/Market-data1/Elspot/Area-Prices/ALL1/Hourly/ (click on Export
Any idea whats wrong with my code? when i execute glCompressedTexImage2D() the program just
Consider the follow code that downloads a file from internet using Indy components: procedure
im trying to have one link for a file download and at the same
I'm trying to download a database plist file from the internet. I later load

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.