FINAL JAVA Help

  • Thread starter Thread starter BloodWolfJW
  • Start date Start date
  • Views Views 1,455
  • Replies Replies 5

BloodWolfJW

Marotheit
Member
Joined
Feb 11, 2011
Messages
220
Reaction score
5
Trophies
0
Location
Colorado Springs
XP
242
Country
United States
So, my first project is almost ready to ship, but I still have one last problem. Whenever I download a file, it limits the size to 10KB, from any URL. I've checked the URL, checked the file, reuploaded, but JAVA refuses to download more than 10KBs. Standby for Code:

Code:
/** Author: BloodWolfJW
* Project: ROMBox Lite v0.0.1
**/
import java.util.Scanner;
import java.net.*;
import java.io.*;

public class ROMBox_Lite {

public static void main(String[] args) {

Scanner ROM = new Scanner (System.in);
String Username;
Username = System.getProperty ("user.name");

System.out.print ("Hello! Welcome to ROMBox Lite. Please Enter the Desired ROM to Download: ");
String ROMBox = ROM.nextLine();
System.out.println ("");

System.out.print ("Now, Please Enter the Desired Name of the File: ");
String Filename = ROM.nextLine();
System.out.println ("");

try
{

System.out.print ("Connecting to the ROMBox Server...\n \n");

URL ROMDownload = new URL("http://www.fileden.com/files/2011/10/9/3206639/ROMs/" + ROMBox + ".zip");
ROMDownload.openConnection();
InputStream reader = ROMDownload.openStream();

System.out.print ("Connected! Downloading File...\n \n");

FileOutputStream writer = new FileOutputStream("C:/Users/" + Username + "/Downloads/" + Filename + ".zip");
byte[] buffer = new byte[153600];
int bytesRead = 0;

while ((bytesRead = reader.read(buffer)) > 0)

{writer.write(buffer, 0, bytesRead);}

System.out.print ("Done! Please Enjoy Your Downloaded File!");}

catch (MalformedURLException e){}
catch (IOException e){}

}

}

What needs fixing?
 
It looks like your while loop is exiting too early before the whole file has been read in.

According to the API, InputStreamReader's "read" method returns -1 when the end of the stream is reached, however your code currently exits the loop when that value is zero.

Try changing it to
Code:
while ((bytesRead = reader.read(buffer)) != -1)
(see http://www.roseindia.net/java/example/java/io/file-url-download.shtml )
 
Have you checked what the URL you are connecting to actually contains? Try opening it in a text editor.

Given the size of the file, I think it actually downloads an HTML page where the link to the actual downloads is.
 
Have you checked what the URL you are connecting to actually contains? Try opening it in a text editor.

Given the size of the file, I think it actually downloads an HTML page where the link to the actual downloads is.
Actually this makes far more sense :) My suggestion would result in a file of 150KB, not 10KB. Oops.
 

Site & Scene News

Popular threads in this forum