-3

How to check whether the uploaded file is a .png file or not, we can chang extension of the jpeg file to png file, but it takes a white background. I have to upload the transparent png file in my project my code is a JAVA is this possible or not. Please Help me

Tushar
  • 182
  • 3
  • 19
  • 1
    Too broad. First, you need to define "transparent" in this context. Changing the extension of a file doesn't change its contents. A JPEG file is still a JPEG file; it's just that most decoder implementations don't bother looking at the extension and will detect the file type based on content. As far as "transparent" goes, that could mean "completely invisible", it could mean "some pixels are completely transparent", or it could mean "some pixels are not completely opaque". Whatever you mean, the solution is to decode the image, examine the alpha channel of the pixels and decide. – Peter Duniho Jul 13 '17 at 05:39
  • 1
    Possible duplicate of [Java check if an image has transparency](https://stackoverflow.com/questions/10223241/java-check-if-an-image-has-transparency) – Tavo Jul 13 '17 at 05:40
  • Thank you @PeterDuniho i just want to check it is it possible or not – Tushar Jul 13 '17 at 07:13
  • As pointed out [here] (https://dzone.com/articles/determining-file-types-java), You can use standard jdk api or Apache tika library to grab the type of file to make judgement of it is `png` or not. – Rishikesh Darandale Aug 08 '17 at 04:50

1 Answers1

1

As suggested here:

BufferedImage img = ImageIO.read(...);
img.getColorModel().hasAlpha(); //transparent
Tavo
  • 3,087
  • 5
  • 29
  • 44