Identifying file types in Java
•
Java
Please help me find out the type of file being uploaded
MimeType returns the same way for both files Please help.
Solution
I use Apache Tika, which uses magic byte pattern and globbing hint (file extension) to identify file types to detect MIME types It also supports other parsing of file content (I don't really use it)
The following is a simple and dirty example of how to use Tika to detect file types without performing any additional parsing of files:
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import org.apache.tika.Metadata.HttpHeaders; import org.apache.tika.Metadata.Metadata; import org.apache.tika.Metadata.TikaMetadataKeys; import org.apache.tika.mime.MediaType; import org.apache.tika.parser.AutoDetectParser; import org.apache.tika.parser.ParseContext; import org.apache.tika.parser.Parser; import org.xml.sax.helpers.DefaultHandler; public class Detector { public static void main(String[] args) throws Exception { File file = new File("/pats/to/file.xls"); AutoDetectParser parser = new AutoDetectParser(); parser.setParsers(new HashMap<MediaType,Parser>()); Metadata Metadata = new Metadata(); Metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY,file.getName()); InputStream stream = new FileInputStream(file); parser.parse(stream,new DefaultHandler(),Metadata,new ParseContext()); stream.close(); String mimeType = Metadata.get(HttpHeaders.CONTENT_TYPE); System.out.println(mimeType); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码