wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Checking File Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Checking File Type" Watch "Checking File Type" New topic
Author

Checking File Type

Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
Hi,

I am uploading a file, how can I ensure that the file being uploaded is only in text format. i.e if the user changes the file extension of exe to txt, still then , my program should point out that this file is not a txt it is an exe. any one have any idea.

My requiremtn is to allow only txt,html or image files.

A.B
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
For text files you can check that the file does not contain any characters that are not part of the character sets you want to allow. Often that means only characters below 256 are allowed, but Uncode (or whatever other encoding might be used) complicate the matter a bit.

HTML files should have a DOCTYPE declaration at the beginning, although not all do.

You can test for image files of a particular type by something like the ImageInfo class.


Ping & DNS - updated with new look and Ping home screen widget
Awais Bajwa
Ranch Hand

Joined: Jan 16, 2001
Posts: 191
Thanks for your post, I believe there must be some all in one utility to accomplish the task. I am already using Jakarta Common, may be this requiremetn is already present in the Commons.. i need to check.

A.B
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18168
    
    8

One utility to identify file types by looking at the contents? After you look at this extremely comprehensive site you may think differently. There are a LOT of different file types, as you will see by browsing that site a bit, and the vast majority of them don't have a documented format that can be used to identify them. But you could certainly scratch the surface and attempt to identify some of the more common formats.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking File Type
 
Similar Threads
allow upload txt files only
upload txt files only
ProcessBuilder - file types to be executed
run exe in client sid
Installation Problem J2SE 1.4.1 on windows XP