Results 1 to 4 of 4
  1. #1
    schenker is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default In Java, how do i ensure that an uploaded file is an image?

    I don't want to use getContentType as it may not be so accurate if a user change a non-image file extension to jpg.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I guess it might be possible to try to read the image file after it has been uploaded. Such as to use one of these Java Image API things (I don't know off hand).

    Another idea, if you are expecting the image to be a specific format, you might be able to open the file and read the first bytes of the header, looking for the presence of the expected file format header bytes.

    And then if the image reading API throws an exception, or if the header bytes are not found where expected, then return the error to the user to have them upload again.
    Last edited by travishein; 04-18-2010 at 07:13 PM.

  3. #3
    schenker is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    Hi, Thanks for the reply. I think going by the header bytes should work. I will update when i get it to work, for the benefit of those who might be interested in this thread.

  4. #4
    schenker is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    After hours of reading and experimenting, finally got it! I read the first byte of the file to determine the file type. you can easily cheat contentType into believing that a file that you are uploading is what it is supposed to be, but by reading header information there is no way to cheat....Ahha Finally!!

Similar Threads

  1. Receive uploaded image / video (byte array) from j2me on jsp.HOW???
    By angelicsign in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-29-2010, 07:21 PM
  2. How can i draw a circle on an image uploaded ?
    By motress in forum New To Java
    Replies: 1
    Last Post: 04-15-2010, 05:55 PM
  3. how to specify location of file to be uploaded?
    By anthrax in forum New To Java
    Replies: 11
    Last Post: 01-13-2010, 03:48 PM
  4. uploaded image not shown
    By java_srinivasan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-05-2008, 01:40 PM
  5. ensure connectivity
    By perdoname in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 03:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •