5

I have requirement to get from user input file which should be image only of specified type. E.g. only JPEGs. Other files must be rejected.

So I implemented naive basic check for file

fileName.toLowerCase().endsWith(".jpg") || fileName.toLowerCase().endsWith(".jpeg")

So if user uploaded file.png, this file was rejected. But then users realized, that if they simply rename file.png to file.jpg, the file will be uploaded.

Is there way to detect if supplied file is valid image of given type using only JAVA SDK without external jars? (note I'm still working with JDK6). Thanks.

user3195535
  • 73
  • 1
  • 4
  • I think this is what you want : http://stackoverflow.com/questions/9643228/test-if-file-is-an-image – Ritz Jan 15 '14 at 09:39
  • http://stackoverflow.com/questions/15539696/check-if-file-is-jpg – Zyga Jan 15 '14 at 09:39
  • You might want to rethink your user experience here. Does it really matter if the image is a JPG or a PNG? – Tim B Jan 15 '14 at 09:40
  • @Tim B Well, it is an requirement and I can only obey it. There are another processes, that rely on specified file format. – user3195535 Jan 15 '14 at 09:55
  • You can use ImageIO to transcode the PNG to JPG. If you really need to restrict them though then the answers below should cover it. – Tim B Jan 15 '14 at 09:58

2 Answers2

7

It should be relatively easy to do some naive checking to separate a JPEG from a PNG. According to Wikipedia, JPEG files always start with FF D8. PNGs however seem to start with 89 50, so one can easily distinguish between the two from just reading a byte or two from the file.

I would recommend looking up the file formats of the most important image types just to make sure there isn't some other mainstream one starting with FF D8. It seems unlikely however.

Edit: JPEG, PNG, GIF, BMP and TIFF all seem to have magic values at the start of the files to tell them apart form other types.

BambooleanLogic
  • 6,088
  • 2
  • 26
  • 51
2

Decode the file as jpeg and see if it throws an Exception or returns an image?

You can use JDK ImageIO to do this (http://docs.oracle.com/javase/6/docs/api/javax/imageio/ImageIO.html). Start by obtaining ImageReader for jpeg, then let that reader attempt to read the image.

Durandal
  • 19,415
  • 2
  • 32
  • 62
  • Thanks for your response. I will use your suggestion as a "backup solution", because I think that using exceptions in this way is generally not the best approach. As I was told by a wise man, exception processing should be used only if there is no other way to prevent exception to ocurr, in other words, Exceptions should be used only when situation is exceptional. – user3195535 Jan 15 '14 at 09:50
  • 1
    However invalid user input is a valid exceptional situation. – Tim B Jan 15 '14 at 09:58
  • 2
    @user3195535 You're taking the "exceptions for exceptional sitatuions" out of scope by taking it too literally. The decisive point is that an exception should be used to indicate that the operation to be performed can't be completed normally - the *context* that is *requesting* the operation is irrelevant for deciding if an exception is appropiate or not. If you think your logic through to the end, exceptions were only allowed to be thrown when the world ends. – Durandal Jan 15 '14 at 11:09
  • @Durandal I agree with your comment. My comment was ment a bit different. Maybe little example will make my thoughts clear. Consider operations with File. I would preffer to use file.exists() test instead of processing FileNotFoundException because I can prevent this exception to raise by testing that existence. – user3195535 Jan 15 '14 at 16:58