Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.file.*;
- public class MimeTypes {
- public static void main(String[] args) {
- Path path;
- try {
- path = Paths.get("/etc");
- System.out.println( path + " : " + Files.probeContentType(path) );
- path = Paths.get("/dev/null");
- System.out.println( path + " : " + Files.probeContentType(path) );
- path = Paths.get("/var/log/syslog.2.gz");
- System.out.println( path + " : " + Files.probeContentType(path) );
- path = Paths.get("/var/run/rpcbind.sock");
- System.out.println( path + " : " + Files.probeContentType(path) );
- path = Paths.get("abc.mp4");
- System.out.println( path + " : " + Files.probeContentType(path) );
- path = Paths.get("MimeTypes.java");
- System.out.println( path + " : " + Files.probeContentType(path) );
- } catch (Exception x) {
- }
- }
- }
- # java MimeTypes
- /etc : inode/directory
- /dev/null : inode/chardevice
- /var/log/syslog.2.gz : application/x-gzip
- /var/run/rpcbind.sock : inode/socket
- abc.mp4 : video/mp4
- MimeTypes.java : text/x-java
- public static void main(String args[]) throws Exception {
- FileInputStream is = null;
- try {
- File f = new File("C:/Temp/mime/abc.mp4");
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement