FINAL JAVA Help

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

Oct 9, 2011

FINAL JAVA Help by BloodWolfJW at 6:05 AM (591 Views / 0 Likes) 5 replies

  1. BloodWolfJW
    OP

    Member BloodWolfJW Marotheit

    Joined:
    Feb 11, 2011
    Messages:
    220
    Location:
    Colorado Springs
    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?
     
  2. Psyfira

    Member Psyfira Credit: 0ml. Insert tea to continue

    Joined:
    Dec 31, 2003
    Messages:
    3,887
    Location:
    England
    Country:
    United Kingdom
    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

    Member powered_by_tux GBAtemp Regular

    Joined:
    Sep 27, 2008
    Messages:
    167
    Country:
    Germany
    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

    Member Psyfira Credit: 0ml. Insert tea to continue

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

    Member BloodWolfJW Marotheit

    Joined:
    Feb 11, 2011
    Messages:
    220
    Location:
    Colorado Springs
    Country:
    United States
    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

    Member BloodWolfJW Marotheit

    Joined:
    Feb 11, 2011
    Messages:
    220
    Location:
    Colorado Springs
    Country:
    United States
    GOT IT! In the end, it was the link. Thanks for your help!
     

Share This Page