Download large files from web
I have a problem, when I want to download a big file, with this code I only download a small part of this file:
Code:
int DownloadProgress = 0;
File file = new File("/" + ZipName);
boolean exists = file.exists();
if (Update == true) {
if (!exists) {
try {
URL GameURL = new URL(ZipURL);
GameURL.openConnection();
InputStream InputFile = GameURL.openStream();
FileOutputStream OutputFolder = new FileOutputStream(ZipName);
byte[] buffer = new byte[153600];
int totalBytesRead = 0;
int bytesRead = 0;
ProgressMonitor progressMonitor = new ProgressMonitor(null,
"Downloading...",
"Preogress", 0, 100);
while ((bytesRead = InputFile.read(buffer)) > 0) {
OutputFolder.write(buffer, 0, bytesRead);
buffer = new byte[153600];
totalBytesRead += bytesRead;
DownloadProgress += (100 / InputFile.read(buffer));
if (progressMonitor.isCanceled()) {
progressMonitor.close();
CleanDownload = true;
return;
}
}
OutputFolder.close();
InputFile.close();
extract();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Couldn't get server's game files");
}
} else {
extract();
}
}
Note: The file I want to be downloaded can be over 1 gb
I also tried to make a progress bar, so if someone can tell me how I can make it work, I would love him/her.
Re: Download large files from web
Quote:
with this code I only download a small part of this file:
Is that what you want to do or are you having a problem reading the whole file?
How much of the file does this code read? Is it always the same amount?
You don't have to create a new byte array in the loop. You can continue to use the one created outside of the loop.
Re: Download large files from web
I have a zip file for testing wiith 10 vids in my server, when I use that code to download the zip file, it only downloads a random number of mb form 250mb to 350mb. The file in my server is over 500mb. A strange thing is that when I open the zip file it let me view the inside, and in the inside there is only one vid from the 10 vids of the original file. When I open the downloaded zip, the only error I get is the "unexpected end" error.
What I want is to download the whole zip file.
I have removed the byte array inside the loop, but I get the same results.
Re: Download large files from web
Sorry about the double post. I have tried with some other code and it seems to work. If someone in the future has the same problem, here is the code I've just used:
Code:
//Modify this variables
private static String ZipName = "last.zip";
private static String ZipURL = "http://localhost:8080/game/last.zip";
private static void download() {
try {
URL url = new URL(ZipURL);
URLConnection urlCon = url.openConnection();
System.out.println(urlCon.getContentType());
InputStream is = urlCon.getInputStream();
FileOutputStream fos = new FileOutputStream(ZipName);
byte[] buffer = new byte[1000];
int bytesRead = is.read(buffer);
while (bytesRead > 0) {
fos.write(buffer, 0, bytesRead);
bytesRead = is.read(buffer);
}
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks anyway. If I find any problem with this new code I will post it ^^