• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting file size and type

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a JSP that displays some information including a link such as:

http://www.thehost.com/someSection/someSubSection/Presentation.doc for example. It could be a pdf, or ppt etc.

I want to display the size and the type of file. Is there a way to load a file from the URL and then get it's size and type. The type I could always get by parsing the url, but it's not too elegant.

Thanks.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the file within the bounds of your web app, or external?
 
Angelo Bonaparte
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is withing the same realm. In the future the docs might not all be. But for now I think they are.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if they're in the same web app and you are not running out of an unexpanded war file, you can use the ServletContext.getRealPath() method to find the file on disk and then use normal File I/O operations on it.

If not, well that's more difficult as you'll need to cooperation of whatever web app the files belong to.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For remote file type, you could parse the URL, but the catch is you may not get the correct file type.
http://www.thehost.com/someSection/someSubSection/Presentation.doc
this is actually an html type.

try doHeader() to get the content type and file size if the web container does support it (I haven't done it)

Receives an HTTP HEAD request from the protected service method and handles the request. The client sends a HEAD request when it wants to see only the headers of a response, such as Content-Type or Content-Length. The HTTP HEAD method counts the output bytes in the response to set the Content-Length header accurately.



Otherwise, use URLConnnetion to the page and inspect the response header.
For instance, conecting to this page http://wiki.apache.org/nutch-data/attachments/Presentations/attachments/serverside1.pdf , you could see

Type: application/pdf
Size: 124.57 KB (127,562 bytes)
[ March 05, 2007: Message edited by: vu lee ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic