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

FileUpload and File type

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a servlet to do file upload using the commons jar. This part is working fine and I'm able to upload files successfully to the server.

My question is How do I determine the file type of each file? Users can upload files with any extensions as long as the content is text.

Eg,

The user can upload test1.txt, test2.abc, test3.fa, if all 3 files are text-based.
On the other hand, I want to stop the upload if the file is an image file, even if the filename is image.txt.


So, is there a way to check and validate the content of the file and determining the file type without restricting the file extensions?
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's sometimes possible to tell the type of a file based on its content. One way is to check the magic number, though not all file types have one, and there's no guarantee there won't be conflicts (though it's statistically improbable). For example, GIF files all start with the ASCII string GIF87a or GIF89a. A text file or pure binary file might also begin with this combination, but it's unlikely. Wikipedia will give you a place to start.
 
Sheriff
Posts: 22808
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except if it's a text file discussing the GIF89a format
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic