Results 1 to 2 of 2
  1. #1
    JavaHead08 is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default Downloading part of a file with java? What have I done wrong?

    I'm trying to create a program that will download only a portion of a file. From everything I could find on the subject I'm doing it correctly, but the program still downloads the entire file. What have I done wrong? Is it possible to do what I'm trying to do with Java? Can someone please point me in the right direction? Thank you. The code is below.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    
    public class FileDownload2 {
    	public static void download() {
    		OutputStream out = null;
    		RandomAccessFile file = null;
    		URLConnection conn = null;
    		InputStream  in = null;
    		try {
    
    
    URL url = new URL("http://www32.megaupload.com/files/c7c6f4ce249c6ccad7583a8f6ea943a8/Kanye%20West%20Nas%20&%20KRS-One%20-%20Classic%20(Midas%20Touch%20Remix).mp3");
    			conn = url.openConnection();
    			conn.setRequestProperty("Range", "901600-");
    			in = conn.getInputStream();
    			byte[] buffer = new byte[1024];
    	 		out = new FileOutputStream("test");
    			
    			
    			int numRead;
    			long numWritten = 0;
    			while ((numRead = in.read(buffer)) != -1) {
    				
    				out.write(buffer, 0, numRead);
    				numWritten += numRead;
    			}
    			
    		} catch (Exception exception) {
    			exception.printStackTrace();
    		} finally {
    			try {
    				if (in != null) {
    					in.close();
    				}
    				if (out != null) {
    					out.close();
    				}
    			} catch (IOException ioe) {
    			}
    		}
    	}
    
    
    
    	public static void main(String[] args) {
    			download();
    		
    	}
    }

  2. #2
    vglass is offline Member
    Join Date
    Jul 2007
    Posts
    14
    Rep Power
    0

    Default

    I think the correct header is "Content-Range", not "Range" as you are using.

Similar Threads

  1. [SOLVED] Downloading jconn3.jar
    By DonCash in forum JDBC
    Replies: 1
    Last Post: 04-15-2008, 05:02 PM
  2. Payroll Part 2, Java
    By lplopez92 in forum New To Java
    Replies: 2
    Last Post: 03-24-2008, 01:55 PM
  3. Help with mathematical part in java
    By lenny in forum Advanced Java
    Replies: 3
    Last Post: 08-09-2007, 01:55 PM
  4. Problems with downloading Files from Applet
    By Albert in forum Java Applets
    Replies: 1
    Last Post: 07-05-2007, 05:09 AM
  5. Part time java Programmer-Indianapolis,IN
    By pegitha in forum Jobs Offered
    Replies: 0
    Last Post: 05-08-2007, 04:01 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
  •