File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes validate a file extension Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "validate a file extension" Watch "validate a file extension" New topic

validate a file extension

Mike Phillip
Ranch Hand

Joined: Dec 05, 2007
Posts: 37
Hi, I doing a server that upload and saves files, it's working fine.

But, I need to let only some extension (like txt and jpg), I'm using the file extension, if it's different I refuse, but a user can rename the file extension before the upload and bypass it.

I found a software called JMimeMagic, it's good, but works with most of thw files, but some time doesn't validate some real txt.

works like this:

any good solution/software for that?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I'm not sure how JMimeMagic works, but if it's image formats you need to deal with, have a look at the ImageInfo class. It identifies a number of image formats based on the contents, not on the name or MIME type.
Mike Phillip
Ranch Hand

Joined: Dec 05, 2007
Posts: 37
thanks for reply.

Well, it's most image and text. With images the JMimemagic is great, the problem is with txt, some times it fails.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

How does it fail?
Accept a file as txt which isn't?
Deny a file, which is a txt-file?

How is it identified, if not as txt?
I agree. Here's the link:
subject: validate a file extension
jQuery in Action, 3rd edition