Joel Bee Hoon
Joel Bee Hoon

Reputation: 31

File download from URL stops when multiple clients try to download it

I'm creating a "patcher launcher" for my game which automatically downloads the latest version of the game when you launch it. However when more than one person tries to download the file at the same time, everyone's download freezes and stops and will only resume when everyone else closes their downloaded and only 1 is downloading at a time. Anyone can help? Here is the current code I use to download files from URL.

public void saveUrl(String filename, String urlString)
        throws MalformedURLException, IOException {
    System.out.println("Downloading " + urlString);
    BufferedInputStream in = null;
    FileOutputStream fout = null;

    int currentdlsize = 0;
    int dlsize = new URL(urlString).openConnection().getContentLength();
    System.out.println("DL Size " + dlsize);

    in = new BufferedInputStream(new URL(urlString).openStream());
    fout = new FileOutputStream(filename);

    byte data[] = new byte[1024];
    int count;
    while ((count = in.read(data, 0, 1024)) != -1) {
        fout.write(data, 0, count);
        currentdlsize += count;
        System.out.println("Downloaded " + currentdlsize + "/" + dlsize);
        prog.setMaximum(dlsize);
        prog.setValue(currentdlsize);
        progstat.setText("Downloading at " + currentdlsize + "/" + dlsize);
    }

    in.close();
    fout.close();

    System.out.println("Downloaded to " + filename);
}

Upvotes: 3

Views: 142

Answers (1)

Daniel S.
Daniel S.

Reputation: 6640

Your download code looks ok and I do not see any reason for a race condition, so without deeper analysis of your download code, I can tell you that this code (client side) is not the problem. It must be the server side which causes the problem, i.e. your webserver or a proxy or firewall which is in between.

Moreover, maybe you should look at JNLP, which is a technology designed exactly for your problem of keeping an application always updated and checking this at startup.

Upvotes: 2

Related Questions