0

I need to know if there is a way to retrieve the real file extension without parse the filename.

In my code I split the file using the function split(filename, ".") then i get the last element of the array that the function returns.

Now, if I create a .pdf file called, for example test_file.pdf, the previous method works perfectly, but if i rename my file to test_file(without extension) I cannot retrieve the extension even if I know that the file is a PDF.

For example, if i rename test_file.pdf to text_file.jpg how can I recognize that the file is still a pdf and not an image file with .jpg extension?

I would like to know if there is a way to obtain this information, maybe using file metadata or other information related to the file.

I'm looking for a Javascript solution because I have to check the extension when I upload the file using a form (client side) but even a Java solution could be fine, can you help me?

Thank you in advance!

slartidan
  • 20,403
  • 15
  • 83
  • 131
Ema.jar
  • 2,370
  • 1
  • 33
  • 43
  • 4
    possible duplicate of [Get real file extension -Java code](http://stackoverflow.com/questions/2091014/get-real-file-extension-java-code) – JFPicard Jul 23 '15 at 13:53
  • 5
    It sounds like you're interested in the type of content in the file, regardless of the filename. Your question is currently confusing when you talk about the "file extension" because that *is* part of the filename. – Jon Skeet Jul 23 '15 at 13:53
  • I think he means (if I and we understood him correctly) that if he uses a proper format then the file is locatable, but searching for "test_file" doesn't net any result even though it should. All I could say OP is see what output your splitter does and see if it even saves the test_file name. – SomeStudent Jul 23 '15 at 14:00

1 Answers1

-1

Look at this post and the marked answer: Get real file extension -Java code

I guess it's just what you need.

Community
  • 1
  • 1
PWPlayDE
  • 44
  • 6
  • A link to another SO question or answer does not make a good answer in itself. Next time, you should flag the question as a duplicate. – E_net4 Jul 23 '15 at 14:01
  • Oh, I'm sorry. I'm new to this. Didn't know how to flag as a duplicate :O – PWPlayDE Jul 24 '15 at 08:53