Results 1 to 4 of 4
  1. #1
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Question 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:

    Java 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Download large files from web

    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.

  3. #3
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default 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.

  4. #4
    Asolpdrag is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default 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:

    Java 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 ^^

Similar Threads

  1. Help reading large files
    By JavaLatte in forum New To Java
    Replies: 0
    Last Post: 05-11-2011, 02:29 PM
  2. Large Wave Files
    By dane1193 in forum New To Java
    Replies: 2
    Last Post: 02-18-2011, 10:59 PM
  3. Copying large files efficiently
    By bayan in forum New To Java
    Replies: 2
    Last Post: 10-27-2010, 05:01 PM
  4. Compare 2 large files
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-23-2008, 02:18 AM
  5. OutofMemory while downloading large files through FTP
    By deb_santanu in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2008, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •