Reputation: 18572
This method should be check if a file is MIME type. To achieve this goal I am using method probeContentType().
However maybe is another way to decide same question. Which are the other alternatives (if there are any)?
Code:
class ProbeContentTypeCheker implements Checker {
@Override
public boolean check(File fileCheck) {
try {
Path filePath = FileSystems.getDefault().getPath(
fileCheck.getAbsolutePath());
if ((Files.probeContentType(filePath) != null)) {
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}
Question:
Upvotes: 9
Views: 3118
Reputation: 20376
There are 2 approaches for getting a file mime type:
The following solution use the 1st approach:
The following solution use the 2nd approach:
For some more information see this post
Upvotes: 7
Reputation: 401
Another alternative is to Use URLConnection.guessContentTypeFromName(String fileName)
if you cannot use Java 7.
Note that if the content type cannot be guessed, the method will return null
.
Upvotes: 0
Reputation: 48807
You can use String mimeType = new MimetypesFileTypeMap().getContentType(theFile);
.
Note that if no MIME type is found, application/octet-stream
is returned instead of null
.
Upvotes: 1