How to get the type of a file in java




















That is why probeContentType method throws IOException, in case an implementation of this API uses Path provided to it to actually try to open the file associated with it. Again, vanilla implementation of this the one that comes with JDK leaves a lot to be desired.

In some ideal world in a galaxy far, far away, all these libraries which try to solve this file-to-mime-type problem would simply implement java. FileTypeDetector , you would drop in the preferred implementing library's jar file into your classpath and that would be it.

In the real world, the one where you need TL,DR section, you should find the library with most stars next to it's name and use it. For this particular case, I don't need one yet ;. I tried several ways to do it, including the first ones said by Joshua Fox. The solution I found, as also is said by Joshua Fox in a superficial way, is to use MimeUtil2 , like this:. Second you should look to convert the first 4 bytes of your file to hexadecimal and then compare it with the magic numbers.

Then it will be a really secure way to check for file validations. You can do it with just one line: MimetypesFileTypeMap. Look the complete test code Java 7 :. I happened to observe that Wikipedia was wrong and that the 00 00 00 18 66 74 79 70 69 73 6F 6D file signature is not correct. EDIT: Here is a useful link if it is still online where you can find samples of many types. I don't own those videos, don't know who does either, but they're useful for testing the above code. A solution to detecting a file's Media Type 1 has the following parts:.

In the following code -1 means skip comparing the byte at that index; a -2 denotes end of file type signature. The code uses up to the first 11 "magic" bytes from the data source's header. Optimizations and improvements that shorten the logic are welcome. Notice that the file name extensions are mapped in MediaTypeExtension.

There's a dependency on Apache's FilenameUtils class for its getExtension function. This allows bidirectional lookup based on file name extensions. It uses pure Java, but requires you to define an enum of the types you want to detect.

If you want a reliable ie. It includes a bundled mime types database and basically inverts the logic of javax. That way the library-defined types always have precedence over what may be defined in unbundled resources. Actually, Apache Tika detector Tika. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 13 years, 4 months ago. Active 2 months ago. Viewed k times. How would you probe the file to determine its actual type not merely based on the extension? Dave Jarvis Lee Theobald Lee Theobald 8, 12 12 gold badges 46 46 silver badges 58 58 bronze badges. A good overview on available libraries is given at rgagnon.

I used the class that was posted as an answer here: stackoverflow. Tika should be the answer now. The other answers below make light of many dependencies with Tika, but I see none with tika-core. Add a comment. Active Oldest Votes. Chris Mowforth Chris Mowforth 6, 2 2 gold badges 22 22 silver badges 33 33 bronze badges. Be aware that Files. I have had a problem with software working on ubuntu but failing on windows.

We have three objects, obj , obj2 , and obj3 ; we use both the methods that we discussed in the above examples that are getClass and instanceOf. However, there differences between the type of obj2. The obj2 variable returned the type ObjectType4 while its class is ObjectType3.

It happens because we inherit the ObjectType4 class in the ObjectType3 and the instanceOf checks all the classes and subclasses. The files with no extensions show "null" for file type and the other listed file types match the files' extensions rather than their actual content.

For example, all three files with names starting with "dustin" are really the same single-sentence text file, but Files. My example uses the ones that come out-of-the-box with my JDK 8 installation. The next code listing demonstrates use of javax. The next screen snapshot demonstrates the output from running this example against the set of test files. We see also that, like the previously discussed approach, this approach in some cases uses the file's extension to determine the file type and so incorrectly reports the file's actual file type when that type is different than what its extension conventionally implies.

I will be covering three methods in URLConnection that support file type detection. The first is URLConnection. The file detection approach using URLConnection. Use of this static method is demonstrated in the next code listing and associated output screen snapshot. URLConnection 's guessContentTypeFromName String approach to file detection shows "null" for files without file extensions and otherwise returns file type String representations that closely mirror the files' extensions.

These results are very similar to those provided by the Files. A code listing employing this approach and associated output in a screen snapshot are shown next. All the file types are null! All of the examples of file detection covered in this post so far have been approaches provided by the JDK.

Table of Contents. Improve Article. Save Article. Like Article. Last Updated : 26 Aug, Output: Name of Field: class java. Month Name of Field: class java.



0コメント

  • 1000 / 1000