0

I've done a bit of googling but haven't found an answer so far.

Problem: On a java backend, check that a file is really what you expect it to be. For example, a jpg file is uploaded with a pdf extension ... is there a jar available that will allow me to validate whether the file is actually the type of file indicated by it's extension?

mortsahl
  • 602
  • 6
  • 18
  • I think this might be a duplicate question to [Getting A File's Mime Type In Java](https://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java) – mrsteffenjo Aug 23 '17 at 21:12

3 Answers3

1

You can try to use Java ProbeContentType

Minh Tuan Nguyen
  • 1,026
  • 8
  • 13
1

Take a look at Apache Tika, it appears to be what you want.

DwB
  • 37,124
  • 11
  • 56
  • 82
1

Apache Tika seams to be the best. example:

import java.io.File;
import java.io.FileInputStream;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser; 
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

public class Main {

  public static void main(String args[]) throws Exception {

   FileInputStream is = null;
    try {
      File f = new File("C:/Temp/mime/test.docx");
      is = new FileInputStream(f);

  ContentHandler contenthandler = new BodyContentHandler();
  Metadata metadata = new Metadata();
  metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
  Parser parser = new AutoDetectParser();
  // OOXMLParser parser = new OOXMLParser();
  parser.parse(is, contenthandler, metadata);
  System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
  System.out.println("Title: " + metadata.get(Metadata.TITLE));
  System.out.println("Author: " + metadata.get(Metadata.AUTHOR));
  System.out.println("content: " + contenthandler.toString());
}
catch (Exception e) {
  e.printStackTrace();
}
  finally {
    if (is != null) is.close();
   }
 } 
}

Example taken from: http://www.rgagnon.com/javadetails/java-0487.html

And there you can find more examples, and they also give a zip with all the jasr for the example ;) http://www.rgagnon.com/examples/demo-tika-libs.zip

jcoder8
  • 163
  • 1
  • 9