Meaningless Drivel is fun!
The moose likes Servlets and the fly likes Size of the URL file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Size of the URL file" Watch "Size of the URL file" New topic

Size of the URL file

Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
I have made a Servlet that downloads a file from the remote host.
Input parameter to my Servlet is the absolute URL of the file.
It works, but I can't get the file size of the file I'm trying to download.
How I can obtain that information?

Here is a small excerpt from my code:

URL mp3_url = new URL(fileName);//fileName = absolute URL
InputStream input = mp3_url.openStream();
Ranch Hand

Joined: Dec 05, 2004
Posts: 48
What's the protocol you are using as part of the absolute URL?
If that is HTTP then you can try to open the connection using URL and you would get HTTPURLConnection object...using that you can find the content legnth....
I don't think using Inputstream you can get the size of the whole content before reading the stream completely.

Thanks,<br />Bhanu<br />SCEA Step-I<br />SCBCD for J2EE 1.3<br />SCJP1.4
Velika Srbija
Ranch Hand

Joined: Jan 15, 2002
Posts: 172
Yes, I have tried that too. But, there is no getSize() or length() methods. In fact, in order to get the size of the file, I have to read all the bytes and than count them. It is possible, but I want to do something different. I want to send what I have read immediately to the response (no cashing on the server).
I agree. Here's the link:
subject: Size of the URL file
It's not a secret anymore!