FINAL JAVA Help

Discussion in 'General Off-Topic Chat' started by BloodWolfJW, Oct 9, 2011.

  1. BloodWolfJW
    OP

    BloodWolfJW Marotheit

    Member
    220
    5
    Feb 11, 2011
    United States
    Colorado Springs
    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?
     
  2. Psyfira

    Psyfira Credit: 0ml. Insert tea to continue

    Member
    3,887
    15
    Dec 31, 2003
    England
    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 )
     
  3. powered_by_tux

    powered_by_tux GBAtemp Regular

    Member
    167
    0
    Sep 27, 2008
    Gambia, The
    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.
     
  4. Psyfira

    Psyfira Credit: 0ml. Insert tea to continue

    Member
    3,887
    15
    Dec 31, 2003
    England
    Actually this makes far more sense :) My suggestion would result in a file of 150KB, not 10KB. Oops.
     
  5. BloodWolfJW
    OP

    BloodWolfJW Marotheit

    Member
    220
    5
    Feb 11, 2011
    United States
    Colorado Springs
    Neither of your suggestions worked, I'm afraid. The link did download the file from Office 2010, and neither did the -1. Back to the drawing board!
     
  6. BloodWolfJW
    OP

    BloodWolfJW Marotheit

    Member
    220
    5
    Feb 11, 2011
    United States
    Colorado Springs
    GOT IT! In the end, it was the link. Thanks for your help!