Jump to content

URL FileSize does not match to the Real File size

- - - - -

  • Please log in to reply
20 replies to this topic

#1
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Hello, how are you? :)


A have just uploaded my "a.mp3" to my ftp;.
In my Computer it is 8,888,890 bytes, and the same is shown in a ftp file client.
I've downloaded the file and it's really 8,888,890 bytes. ok, ok , everything is all right but... but,

Now I've tested it in a Java:



                InputStream in; // read from the HTTP

		FileInputStream in2; // read from my HDD

		

		

		try{

			in2 = new FileInputStream("a.mp3"); // that's my file

			URL url1 = new URL("http://something.ge/a.mp3"); // my file in my web page

			in = (url1.openStream());

			System.out.println(in.available() +", "+ in2.available());

                   }catch(Exception e){

			System.out.println("Ooops!");

		}


output is this:
8475, 8888890



And when I try to download it from Java, it's less that it is really;

 

		FileInputStream in2;

		FileOutputStream out;

		

                       	try{

			URL url1 = new URL("http://somethin/a.mp3");

			in = (url1.openStream());

			out = new FileOutputStream("new.mp3");

			

			byte[] buffer = new byte[1024];

			int r=0;

			while(r<in.available()){

			in.read(buffer);

			out.write(buffer);

			}

			

		}catch(Exception e){

			 System.out.println("Oooooo");

		}



what's happening here, could you help me please?
GNU/Linux Is the Best.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I highlighted it :)

Quote

public int available()
throws IOException
Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
In some cases, a non-blocking read (or skip) may appear to be blocked when it is merely slow, for example when reading large files over slow networks.

Overrides:
available in class InputStream
Returns:
an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking.
Throws:
IOException - if this file input stream has been closed by calling close or an I/O error occurs.

For your while loop, just keep on reading untill you read null instead of checking with available();

#3
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Also you need to use the value returned by the read() method. It's possible to read fewer bytes than the size of the array.

#4
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
wim DC
Norm

thanks for this answer. :)

I'm writing a Download Manager Like FashGet or Internet Download Manager. it splits the bytes of a file and downloads partition of the file together (each partition in each Thread);

So I must know the real file size, before I start downloading. how can i do this, could you tell me please?
GNU/Linux Is the Best.

#5
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Does the server have a way you can ask it the size of the file?
If you are using HTTP, the content-length field in the response header might have the length.

#6
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

Norm said:

Does the server have a way you can ask it the size of the file?
If you are using HTTP, the content-length field in the response header might have the length.

I'dont Know how to check this. Could you tell me, please? I want the method that will work everywhere :)
that's my Audio on HTTP:


GNU/Linux Is the Best.

#7
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
When you send a HTTP request to a server, the server's response starts with a header block followed by the data. The header contains a field: content-length. You should get that field and look at its value to see if it is what you need.

The URL class has several methods that could help you get the contents of the response header. Try using them and printing what is returned.

I have a utility program I use to download files. When I executed it I got this message:
Copying: a.mp3 ... 8888890 bytes

It must have gotten the length from the response header. I'll check.

#8
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

Norm said:

The URL class has several methods that could help you get the contents of the response header. Try using them and printing what is returned. I have a utility program I use to download files. When I executed it I got this message:
Copying: a.mp3 ... 8888890 bytes
It must have gotten the length from the response header. I'll check.

Yes, that's real size of my file. Could you tell me one of the method that sends me the real bytes of the file?
GNU/Linux Is the Best.

#9
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Did you read the API doc for the URL class. I'm sure that it is there.

#10
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Oh, I've done it!


HttpURLConnection conn = null;

        try {

            URL url = new URL("Http://teodore.ge/a.mp3");

			conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("HEAD");

            conn.getInputStream();

            System.out.println(conn.getContentLength());

        } catch (IOException e) {

            

        } finally {

        	conn.disconnect();

        }


Result is: 8888890


:)
GNU/Linux Is the Best.

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You should be able to split it into partitions using the estimated size, but let the thread that reads the final bit make sure it reads it fully, not caring what the guessed size was.

Edit: okay didn't quite refresh my page in the past half hour to see the responses I missed ^^

#12
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Thanks everyone! I love codecall.net.
I'll soon finish my Project, and I'll show you :)
GNU/Linux Is the Best.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users