• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

How to get the file length before download

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All
I am trying to get the size of a file before downloading it. I cannot use the URL method GetContentLength because I'm using Sockets not URLs so is there any other way to do this?
Thanks
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!
Moving this to the Sockets forum...
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say you're using sockets, do you mean a custom client and server, or do you mean you're using raw sockets to talk to an HTTP or FTP (or other) server?
 
Zac Nahas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the object Socket to do basic connection to download a file but I need to know the size of the file ahead of time. So is there any socket function of HTTP function that can find the file size?
 
Zac Nahas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the object Socket to do basic connection to download a file but I need to know the size of the file ahead of time. So is there any socket function OR HTTP function that can find the file size?
PS: I posted again to correct the or. Sorry for the typo.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you didn't quite answer my question, but I'll assume that you're using plain Sockets to connect to a Web server to download the file.
If you use HTTP 1.0 commands to fetch the file, you'll get a "Content-Length:" header which you can parse to get the length of the file -- i.e., like the below. The line "GET / HTTP/1.0" is the key thing you have to send; the Content-Length header will come back and you can parse it.
It's a lot easier to use URLConnection, though!
$ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.0
HTTP/1.1 200 OK
Date: Wed, 25 Feb 2004 22:13:40 GMT
Server: Apache/2.0.48 (Unix) DAV/2 SVN/1.0.0
Last-Modified: Mon, 23 Feb 2004 21:22:09 GMT
ETag: "50421c-ae5-a0490240"
Accept-Ranges: bytes
Content-Length: 2789
Connection: close
Content-Type: text/html; charset=ISO-8859-1
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
 
Zac Nahas
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your respone.Your assumption is correct. However, the solution you suggested didn't work well
I used:
telnet yahoo.com 80
The response was:
Trying 66.218.71.198...
Connected to yahoo.com.
Escape character is '^]'.
I then typed:
GET / HTTP/1.0
No response:
If I used instead
GET /HTTP/1.0
I get the index.html file by the GET command. So what is the problem?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look back carefull at the transcript I showed you. When you use the HTTP/1.0 form, you have to enter a blank line before you'll get a response. That's because HTTP/1.0 requests can themselves include headers; the extra blank line indicates the end of the request.
I'm guessing that asking Yahoo for a non-existent page just returns their index page; smooshing the "/" and "HTTP/1.0" together makes it look like an HTTP 0.9 reqeust for a non-existent page.
 
reply
    Bookmark Topic Watch Topic
  • New Topic