0

All,

In my project, I have different attachment types like image, audio file, Video file and PDF. I want to sort them based on the extension like .mp4 is video, .pdf is documented like that, How can we write in Java,

One method we can follow gets the extension and compare with extensions required, Is there any better way to do it.

kiri
  • 1,977
  • 5
  • 27
  • 55
  • 1
    URLs don't necessarily have an extension that indicates the content. The only way to know what the content is is through the `Content-Type:` header. – RealSkeptic Mar 05 '19 at 11:53
  • not too much related but this will help you to get file extension http://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FilenameUtils.html#getExtension(java.lang.String) – Akash Shah Mar 05 '19 at 11:57
  • Use `Apache commons-io` https://stackoverflow.com/questions/605696/get-file-name-from-url –  Mar 05 '19 at 11:57
  • I don’t understand what you’re sorting. `List`? `List`? `List`? Something other than a List? I’m also not clear on what you’re asking. Are you asking if there’s a better way to determine file type than just examining the extension? – VGR Mar 05 '19 at 13:56

1 Answers1

0

You can use a mime database. nginx for example uses this one by default.

But this is not always enough. For example, mp4 mime type is video/mp4 but mp4 COULD be an audio file. and you can know for sure without downloading it.

szatmary
  • 29,969
  • 8
  • 44
  • 57