Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import java.nio.file.*;
  2. public class MimeTypes {
  3. public static void main(String[] args) {
  4. Path path;
  5. try {
  6. path = Paths.get("/etc");
  7. System.out.println( path + " : " + Files.probeContentType(path) );
  8.  
  9. path = Paths.get("/dev/null");
  10. System.out.println( path + " : " + Files.probeContentType(path) );
  11.  
  12. path = Paths.get("/var/log/syslog.2.gz");
  13. System.out.println( path + " : " + Files.probeContentType(path) );
  14.  
  15. path = Paths.get("/var/run/rpcbind.sock");
  16. System.out.println( path + " : " + Files.probeContentType(path) );
  17.  
  18. path = Paths.get("abc.mp4");
  19. System.out.println( path + " : " + Files.probeContentType(path) );
  20.  
  21. path = Paths.get("MimeTypes.java");
  22. System.out.println( path + " : " + Files.probeContentType(path) );
  23. } catch (Exception x) {
  24. }
  25. }
  26. }
  27.  
  28. # java MimeTypes
  29. /etc : inode/directory
  30. /dev/null : inode/chardevice
  31. /var/log/syslog.2.gz : application/x-gzip
  32. /var/run/rpcbind.sock : inode/socket
  33. abc.mp4 : video/mp4
  34. MimeTypes.java : text/x-java
  35.  
  36. public static void main(String args[]) throws Exception {
  37.  
  38. FileInputStream is = null;
  39. try {
  40. File f = new File("C:/Temp/mime/abc.mp4");
  41. is = new FileInputStream(f);
  42.  
  43. ContentHandler contenthandler = new BodyContentHandler();
  44. Metadata metadata = new Metadata();
  45. metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
  46. Parser parser = new AutoDetectParser();
  47. // OOXMLParser parser = new OOXMLParser();
  48. parser.parse(is, contenthandler, metadata);
  49. System.out.println("Mime: " + metadata.get(Metadata.CONTENT_TYPE));
  50. System.out.println("Title: " + metadata.get(Metadata.TITLE));
  51. System.out.println("Author: " + metadata.get(Metadata.AUTHOR));
  52. System.out.println("content: " + contenthandler.toString());
  53. }
  54. catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57. finally {
  58. if (is != null) is.close();
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement