2

I know I can get this by doing this

String ext = FilenameUtils.getExtension("/path/to/file/foo.txt");

But what if someone tries to upload a foo.exe file by just changing the extension type to foo.doc. Is there any way through which I can get the actual extension type without reading the content of file

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Vipul Jain
  • 1,395
  • 1
  • 14
  • 28

1 Answers1

2
File file = new File("filename.asgdsag");
InputStream is = new BufferedInputStream(new FileInputStream(file));
String mimeType = URLConnection.guessContentTypeFromStream(is);

From this post : Get real file extension -Java code


Also using java7, you should check out this :

public static String probeContentType(Path path)
                           throws IOException

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#probeContentType%28java.nio.file.Path%29

Community
  • 1
  • 1
Alexandre Beaudet
  • 2,774
  • 2
  • 20
  • 29