Home arrow static arrow Java Programming [Archive] - getting contentlength
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - getting contentlength
This topic has 22 replies on 2 pages.    1 | 2 | Next »

Posts:45
Registered: 7/13/04
getting contentlength  
Jul 20, 2004 3:23 AM



 
we use HTTPClient for our download client.In that for URLs with no resume support,we get the content length as follows

int contentLength = rsp.getHeaderAsInt("ContentLength");
where rsp is instance of HTTPResponse.class in HTTPClient.
but we are not getting contentlength.

we are getting contentlength for URLs with reume support.

Why is it so??
plz help..

Thank you
priya
 

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 20, 2004 3:49 AM (reply 1 of 22)



 
It's my belief that Servlets have to set that header explicityly and is not provided by default.
 

Posts:45
Registered: 7/13/04
Re: getting contentlength  
Jul 20, 2004 4:05 AM (reply 2 of 22)



 
What to do now??is any other way round???

we need to get content length of URL with no resume support.

can u help me out??

thank u

 

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 20, 2004 4:08 AM (reply 3 of 22)



 
When you say contentlength of an URL, do you mean

a) the length of the URL
b) the length of the content that is transmitted from the webserver
 

Posts:45
Registered: 7/13/04
Re: getting contentlength  
Jul 20, 2004 4:15 AM (reply 4 of 22)



 
i mean the length of the content that is transmitted from the webserver
then only we can set the threads to download
 

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 20, 2004 4:40 AM (reply 5 of 22)



 
Why not read until you hit -1?

int read = IntputStream.read(somebytebuffer,0,somelength);if(read == 0)      throw new IOException("Read 0 bytes.");elseif(read == -1)      stopReading();else        somebuffer.append(someCharset.decode(ByteBuffer.wrap(somebytebuffer,0,read)));
 

Posts:45
Registered: 7/13/04
Re: getting contentlength  
Jul 20, 2004 4:46 AM (reply 6 of 22)



 

How can i read the entire file???i need to get only the content length and then download the content using multiple threads.
so i cant just read until -1???
 

Posts:21,719
Registered: 98-02-20
Re: getting contentlength  
Jul 20, 2004 4:48 AM (reply 7 of 22)



 
How could you not know the length of the content you're sending back? After all, your servlet is generating it.

%
 

Posts:45
Registered: 7/13/04
Re: getting contentlength  
Jul 20, 2004 4:53 AM (reply 8 of 22)



 
we connect to HTTPserver thru HTTPConnection.then using HTTPResponse we can get content length of the URL right.

rsp.getHeaderAsInt("ContentLength"); where rsp instance of HTTPResponse.

we r using HTTPClient to make connections to server.

we need to get file contentlength from the response.
 

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 20, 2004 5:00 AM (reply 9 of 22)



 
But is that HEADER is not set, then you'll have to read until EOF.
 

Posts:45
Registered: 7/13/04
Re: getting contentlength  
Jul 20, 2004 5:07 AM (reply 10 of 22)



 

i think the header was not set when i connected to a non-resumable URL. because i tried to print
System.out.println("contentlength "+rsp.getHeaderAsInt("ContentLength"))
and didnt get any!!!

so i may have to read until EOF right?? can we see whether the header is set or not??
any suggestions...
 

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 20, 2004 5:23 AM (reply 11 of 22)



 
Yes, or the problem might be that you're not using the correct header.

The correct header is "Content-Length" not "ContentLength".

http://www.w3.org/Protocols/HTTP/Object_Headers.html#content-length
 

Posts:21,719
Registered: 98-02-20
Re: getting contentlength  
Jul 20, 2004 5:28 AM (reply 12 of 22)



 
No information about the response is sent to you - you generate it and send it back to the client.

What's the big deal, anyway? If you have a dynamic JSP sent back as the response you don't sent the content length. You simply write until the stream is finished. Why can't you do that with your file? Open the file, write the contents to the response stream, close the input file, done. What's the problem?

%
 

Posts:45
Registered: 7/13/04
Re: getting contentlength  
Jul 20, 2004 5:39 AM (reply 13 of 22)



 
As per ur suggestion i gave like this

byte [] byteBuffer = new byte[14550] ;
Charset cset ;
int readBytes = ipStream.read(byteBuffer,0,1024);
if(readBytes==0)throw new IOException("read 0 bytes");
else if(readBytes == -1)
break;
else byteBuffer.append(cset.decode(ByteBuffer.wrap(byteBuffer,0,readBytes)));

but i get the error "method decode(ByteBuffer) undefined for Charset.why so??
i havent used Charset so far. so dunno the reason.
Also i had tried "Content-Length" but in vain.
plz help
 

Posts:123
Registered: 5/24/04
Re: getting contentlength  
Jul 20, 2004 5:45 AM (reply 14 of 22)



 
You should probably initialize the charset as per
Charset cset = Charset.forName(someHTTPResponse.getHeader("Content-Encoding"));if(cset == null)  cset = Charset.forName("UTF-8");

And, a Charset-object definately has a

decode(ByteBuffer buf)
method.
 
This topic has 22 replies on 2 pages.    1 | 2 | Next »