Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: downloaded file from HTTP look like damaged but file size in bytes is the same

  1. #1
    Junior Member VakhoQ's Avatar
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default downloaded file from HTTP look like damaged but file size in bytes is the same

    Hello, my friends!

    Downloaded file size and original file size on my HTTP is the same - everything is well. But When I open a downloaded mp3 file it has knocking pauses every 2 or 3 seconds, what's happening? when the file is in my computer(HDD), everything works well, but when it is on the internet (HTTP), there is this problem, It sounds like damaged mp3, but the file Size in Byte is the same

    thanks a lot! I love Java 'n I love forum of Java Developers

    PHP Code:
     InputStream in
            
    FileOutputStream out
            
    byte buffer[]; 
             
            try { 
                 
                
    URL url1 = new URL("http://teodore.ge/a.mp3");    //mp3 file     
                
    in url1.openStream(); // openStream in InputStream 
                
    out = new FileOutputStream("new.mp3"); // new mp3 File 

                /* I need HttpURLConnection for to request the file length from the Server */ 
                
    HttpURLConnection conn null;  
                
    conn = (HttpURLConnectionurl1.openConnection(); 
                
    conn.setRequestMethod("HEAD"); 
                
    conn.getInputStream(); 
                
    /* ****************** */ 
                
                
    int readedByte 0
                
    int avaiable conn.getContentLength();  
                 
                
    int bufferSize=1024
                
    buffer = new byte[bufferSize]; 
                while (
    readedByte <= conn.getContentLength() && readedByte >= 0)  
                { 
                    if (
    avaiable 1024// if there is less that 1024 byte left use new bufferSize 
                    

                        
    buffer = new byte[avaiable]; // Size of new bufferSize  
                        
    in.read(buffer); 
                        
    out.write(buffer); 
                        
    readedByte readedByte avaiable// Readed Bytes 
                        
    avaiable avaiable avaiable// left Byte 
                        
    break; 
                    } 
                    
    // if there is more that 1024 byte, use buferrSize 1024 
                    
    in.read(buffer); 
                    
    out.write(buffer); 
                    
    readedByte readedByte bufferSize// ReadedByte 
                    
    avaiable avaiable bufferSize// left Byte 
                     
                

         
            } catch (
    Exception e) { 
                
    e.printStackTrace(); 
            } 


  2. #2
    Junior Member VakhoQ's Avatar
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: downloaded file from HTTP look like damaged but file size in bytes is the same

    The same is in Photos too. For example this is Original Photo:
    1297013053-landscape_wallpaper_1600x900.jpg

    and this is Downloaded photo:
    New.jpg
    I love Open Source 'n I love Gnu/Linux, I love C++ 'n Java cheers me up;

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: downloaded file from HTTP look like damaged but file size in bytes is the same


  4. #4
    Junior Member VakhoQ's Avatar
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: downloaded file from HTTP look like damaged but file size in bytes is the same

    I have found my mistake!
    before i found mistake I had written:

    PHP Code:
    buffer = new byte[avaiable];
    in.read(buffer);
    out.write(buffer); 
    the main reason was that, "in.read(buffer);" sometimes was reading less then bufferSize length. I don't know why but the solution was very simbple:

    I should write:

    PHP Code:
    buffer = new byte[avaiable];
    count=in.read(buffer);
    out.write(buffer,0,count); 

    everything works very well

    thank you
    I love Open Source 'n I love Gnu/Linux, I love C++ 'n Java cheers me up;

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: downloaded file from HTTP look like damaged but file size in bytes is the same

    Yes, you need to consider the number of bytes that are read and not assume the buffer was always being filled.

Similar Threads

  1. Reading an int from bytes(binary file) - HELP
    By yogiyogi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 02:03 PM
  2. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  4. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM
  5. How to Get the size of a file in bytes
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM