Stefan Kendall
Stefan Kendall

Reputation: 67852

Validating files/getting true file type in Java/Groovy?

I need to determine whether a file is of a given set of true types, and I was wondering what the best way to do that was.

Essentially, I really only need to check for PDFs, images, and some microsoft word file types. I've heard of using imagemagick and catching exceptions to do image testing, but what about the others?

Any ideas?

Upvotes: 3

Views: 3929

Answers (3)

Priyanshu Chauhan
Priyanshu Chauhan

Reputation: 5545

MimetypesFileTypeMap works like a charm in groovy:

import javax.activation.MimetypesFileTypeMap

String contentType = new MimetypesFileTypeMap().getContentType("foo.gif")
println contentType

Result: image/gif

Upvotes: 1

Abdennour TOUMI
Abdennour TOUMI

Reputation: 93333

file.toURL().openConnection().getContentType()

File is an instance of java.io.File

That's it!

Upvotes: 0

tim_yates
tim_yates

Reputation: 171144

There are some methods for getting the MIME type on this page here

The first couple of examples guess the MIME type based on file extension, and as it says, a more robust method would be to use the Apache Tika framework, which supports many file types.

Here's an example of using Tika (in Groovy):

// Grab tika and all its dependencies...
// takes a while on first run, as it pulls in LOADS of dependencies
@Grab( 'org.apache.tika:tika-core:0.9' )
@Grab( 'org.apache.tika:tika-parsers:0.9' )
import org.apache.tika.Tika

println( new Tika().detect( new File( 'tim.tiff' ) ) )
println( new Tika().detect( new File( 'tim.renamedtiff' ) ) )

That outputs:

15:15:56 [tim_yates@mac] TikaTest $ groovy test.groovy 
image/tiff
image/tiff

Upvotes: 7

Related Questions