我正在为我的游戏创建一个“修补程序启动器”,它会在您启动游戏时自动下载最新版本的游戏。但是,当多人同时尝试下载文件时,每个人的下载都会冻结并停止,并且只有在其他人关闭下载并且一次只有 1 个正在下载时才会恢复。任何人都可以帮忙吗?这是我用来从 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);
}