For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2006 > Question Re: URI, URL, and InputStream Classes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Question Re: URI, URL, and InputStream Classes
smad007

2006-05-25, 7:43 am

Hello,

I'm writing a little program to download files from servers via http. I make a new URI, then use this to get an InputStream by calling toURL().openStream(). I then use this to make a new BufferedInputStream and from this I can read from the stream byte by byte, eventually completely downloading the file denoted by the URI to a local directory.

Here is the code:

URI ur = new URI("http://foo.bar.ca/someDir/sfoo2004.pdf");
BufferedInputStream in = new BufferedInputStream(ur.toURL().openStream());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outRelPath + outFileName));
int b;
while ((b = in.read()) != -1)
{
    out.write(b);
    out.flush();
}
out.close();
in.close();

This works very well, but there is a problem! I want to find out what the file size is, in bytes, before I actually download the file. So my question is...Is this possible? I can't seem to find any obvious way to do this using either the URL, URI, or InputStream classes. Anyone have any suggestions?

Thanks in Advance,

Adam
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com