|
6 | 6 | import java.util.Locale; |
7 | 7 |
|
8 | 8 | //****************************************************************************** |
9 | | -//** File Class - By Peter Borissow |
| 9 | +//** File Class |
10 | 10 | //****************************************************************************** |
11 | 11 | /** |
12 | 12 | * Used to represent a single file on a file system. In many ways, this class |
@@ -984,107 +984,138 @@ private boolean isValidPath(String PathToFile){ |
984 | 984 | return true; |
985 | 985 |
|
986 | 986 | } |
987 | | - |
988 | | - |
989 | | - |
| 987 | + |
| 988 | + |
990 | 989 | //************************************************************************** |
991 | 990 | //** getContentType |
992 | 991 | //************************************************************************** |
993 | | - /** Returns the mime type associated with the file extension. This method |
994 | | - * only covers the most common/popular mime types. The returned mime type |
995 | | - * is NOT authoritative. |
| 992 | + /** Returns the mime type associated with the file extension found in a |
| 993 | + * given file name. This method only covers the most common/popular mime |
| 994 | + * types. The returned mime type is NOT authoritative. |
| 995 | + * @param file File name (e.g. hello.txt) |
| 996 | + * @return Content type (e.g. text/plain) |
996 | 997 | */ |
997 | | - public String getContentType(){ |
998 | | - |
999 | | - //TEXT |
1000 | | - if (this.extensionEquals("css")) return "text/css"; |
1001 | | - if (this.extensionEquals("dtd")) return "text/plain"; |
1002 | | - if (this.extensionEquals("htm,html")) return "text/html"; |
1003 | | - if (this.extensionEquals("java")) return "text/plain"; |
1004 | | - if (this.extensionEquals("js")) return "text/javascript"; |
1005 | | - if (this.extensionEquals("txt")) return "text/plain"; |
1006 | | - |
| 998 | + public static String getContentType(String file){ |
| 999 | + |
| 1000 | + //Get file extension |
| 1001 | + FileExtension ext = null; |
| 1002 | + if (file!=null){ |
| 1003 | + file = file.trim(); |
| 1004 | + int x = file.lastIndexOf("."); |
| 1005 | + if (x!=-1) ext = new FileExtension(file.substring(x+1)); |
| 1006 | + } |
| 1007 | + if (ext==null) return "application/octet-stream"; |
| 1008 | + |
| 1009 | + |
| 1010 | + //TEXT |
| 1011 | + if (ext.equals("css")) return "text/css"; |
| 1012 | + if (ext.equals("dtd")) return "text/plain"; |
| 1013 | + if (ext.equals("htm,html")) return "text/html"; |
| 1014 | + if (ext.equals("java")) return "text/plain"; |
| 1015 | + if (ext.equals("js")) return "text/javascript"; |
| 1016 | + if (ext.equals("txt")) return "text/plain"; |
| 1017 | + |
1007 | 1018 | //IMAGE |
1008 | | - if (this.extensionEquals("bmp")) return "image/bmp"; |
1009 | | - if (this.extensionEquals("gif")) return "image/gif"; |
1010 | | - if (this.extensionEquals("jp2,j2c,j2k,jpx")) return "image/jp2"; |
1011 | | - if (this.extensionEquals("jpg,jpe,jpeg,jfif,pjpeg,pjp")) return "image/jpeg"; |
1012 | | - if (this.extensionEquals("png")) return "image/png"; |
1013 | | - if (this.extensionEquals("psd")) return "image/x-photoshop"; |
1014 | | - if (this.extensionEquals("rgb")) return "image/x-rgb"; |
1015 | | - if (this.extensionEquals("tif,tiff")) return "image/tiff"; |
1016 | | - if (this.extensionEquals("xbm")) return "image/x-xbitmap"; |
1017 | | - if (this.extensionEquals("xpm")) return "image/x-xpixmap"; |
1018 | | - if (this.extensionEquals("ico")) return "image/vnd.microsoft.icon"; |
1019 | | - |
| 1019 | + if (ext.equals("bmp")) return "image/bmp"; |
| 1020 | + if (ext.equals("gif")) return "image/gif"; |
| 1021 | + if (ext.equals("jp2,j2c,j2k,jpx")) return "image/jp2"; |
| 1022 | + if (ext.equals("jpg,jpe,jpeg,jfif,pjpeg,pjp")) return "image/jpeg"; |
| 1023 | + if (ext.equals("png")) return "image/png"; |
| 1024 | + if (ext.equals("psd")) return "image/x-photoshop"; |
| 1025 | + if (ext.equals("rgb")) return "image/x-rgb"; |
| 1026 | + if (ext.equals("tif,tiff")) return "image/tiff"; |
| 1027 | + if (ext.equals("xbm")) return "image/x-xbitmap"; |
| 1028 | + if (ext.equals("xpm")) return "image/x-xpixmap"; |
| 1029 | + if (ext.equals("ico")) return "image/vnd.microsoft.icon"; |
| 1030 | + |
1020 | 1031 | //MICROSOFT OFFICE APPLICATIONS |
1021 | | - if (this.extensionEquals("doc,dot")) return "application/msword"; |
1022 | | - if (this.extensionEquals("xls,xlw,xla,xlc,xlm,xlt,xll")) return "application/vnd.ms-excel"; |
1023 | | - if (this.extensionEquals("ppt,pps,pot")) return "application/vnd.ms-powerpoint"; |
1024 | | - if (this.extensionEquals("mdb")) return "application/x-msaccess"; |
1025 | | - if (this.extensionEquals("mpp")) return "application/vnd.ms-project"; |
1026 | | - if (this.extensionEquals("pub")) return "application/x-mspublisher"; |
1027 | | - if (this.extensionEquals("wmz")) return "application/x-ms-wmz"; |
1028 | | - if (this.extensionEquals("wmd")) return "application/x-ms-wmd"; |
1029 | | - if (this.extensionEquals("one,onetoc2,onetmp,onepkg")) return "application/msonenote"; |
1030 | | - if (this.extensionEquals("docx")) return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; |
1031 | | - if (this.extensionEquals("dotx")) return "application/vnd.openxmlformats-officedocument.wordprocessingml.template"; |
1032 | | - if (this.extensionEquals("xlsx")) return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; |
1033 | | - if (this.extensionEquals("xltx")) return "application/vnd.openxmlformats-officedocument.spreadsheetml.template"; |
1034 | | - if (this.extensionEquals("pptx")) return "application/vnd.openxmlformats-officedocument.presentationml.presentation"; |
1035 | | - if (this.extensionEquals("ppsx")) return "application/vnd.openxmlformats-officedocument.presentationml.slideshow"; |
1036 | | - if (this.extensionEquals("potx")) return "application/vnd.openxmlformats-officedocument.presentationml.template"; |
1037 | | - if (this.extensionEquals("sldx")) return "application/vnd.openxmlformats-officedocument.presentationml.slide"; |
1038 | | - |
| 1032 | + if (ext.equals("doc,dot")) return "application/msword"; |
| 1033 | + if (ext.equals("xls,xlw,xla,xlc,xlm,xlt,xll")) return "application/vnd.ms-excel"; |
| 1034 | + if (ext.equals("ppt,pps,pot")) return "application/vnd.ms-powerpoint"; |
| 1035 | + if (ext.equals("mdb")) return "application/x-msaccess"; |
| 1036 | + if (ext.equals("mpp")) return "application/vnd.ms-project"; |
| 1037 | + if (ext.equals("pub")) return "application/x-mspublisher"; |
| 1038 | + if (ext.equals("wmz")) return "application/x-ms-wmz"; |
| 1039 | + if (ext.equals("wmd")) return "application/x-ms-wmd"; |
| 1040 | + if (ext.equals("one,onetoc2,onetmp,onepkg")) return "application/msonenote"; |
| 1041 | + if (ext.equals("docx")) return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; |
| 1042 | + if (ext.equals("dotx")) return "application/vnd.openxmlformats-officedocument.wordprocessingml.template"; |
| 1043 | + if (ext.equals("xlsx")) return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; |
| 1044 | + if (ext.equals("xltx")) return "application/vnd.openxmlformats-officedocument.spreadsheetml.template"; |
| 1045 | + if (ext.equals("pptx")) return "application/vnd.openxmlformats-officedocument.presentationml.presentation"; |
| 1046 | + if (ext.equals("ppsx")) return "application/vnd.openxmlformats-officedocument.presentationml.slideshow"; |
| 1047 | + if (ext.equals("potx")) return "application/vnd.openxmlformats-officedocument.presentationml.template"; |
| 1048 | + if (ext.equals("sldx")) return "application/vnd.openxmlformats-officedocument.presentationml.slide"; |
| 1049 | + |
1039 | 1050 | //OTHER APPLICATIONS |
1040 | | - if (this.extensionEquals("ai,eps,ps")) return "application/postscript"; |
1041 | | - if (this.extensionEquals("gz")) return "application/x-gzip"; |
1042 | | - if (this.extensionEquals("pdf")) return "application/pdf"; |
1043 | | - if (this.extensionEquals("xml")) return "application/xml"; //return "text/xml"; |
1044 | | - if (this.extensionEquals("z")) return "application/x-compress"; |
1045 | | - if (this.extensionEquals("zip")) return "application/zip"; |
1046 | | - |
| 1051 | + if (ext.equals("ai,eps,ps")) return "application/postscript"; |
| 1052 | + if (ext.equals("gz")) return "application/x-gzip"; |
| 1053 | + if (ext.equals("pdf")) return "application/pdf"; |
| 1054 | + if (ext.equals("xml")) return "application/xml"; //return "text/xml"; |
| 1055 | + if (ext.equals("z")) return "application/x-compress"; |
| 1056 | + if (ext.equals("zip")) return "application/zip"; |
| 1057 | + |
1047 | 1058 | //AUDIO |
1048 | | - if (this.extensionEquals("mid,midi")) return "audio/x-midi"; |
1049 | | - if (this.extensionEquals("mp1,mp2,mp3,mpa,mpega")) return "audio/x-mpeg"; |
1050 | | - if (this.extensionEquals("ra,ram")) return "audio/x-pn-realaudio"; |
1051 | | - if (this.extensionEquals("wav")) return "audio/x-wav"; |
1052 | | - if (this.extensionEquals("wma")) return "audio/x-ms-wma"; |
1053 | | - if (this.extensionEquals("wax")) return "audio/x-ms-wax"; |
1054 | | - if (this.extensionEquals("wmv")) return "audio/x-ms-wmv"; |
1055 | | - |
| 1059 | + if (ext.equals("mid,midi")) return "audio/x-midi"; |
| 1060 | + if (ext.equals("mp1,mp2,mp3,mpa,mpega")) return "audio/x-mpeg"; |
| 1061 | + if (ext.equals("ra,ram")) return "audio/x-pn-realaudio"; |
| 1062 | + if (ext.equals("wav")) return "audio/x-wav"; |
| 1063 | + if (ext.equals("wma")) return "audio/x-ms-wma"; |
| 1064 | + if (ext.equals("wax")) return "audio/x-ms-wax"; |
| 1065 | + if (ext.equals("wmv")) return "audio/x-ms-wmv"; |
| 1066 | + |
1056 | 1067 | //VIDEO |
1057 | | - if (this.extensionEquals("asf,asx")) return "video/x-ms-asf"; |
1058 | | - if (this.extensionEquals("avi")) return "video/msvideo"; |
1059 | | - if (this.extensionEquals("mov")) return "video/quicktime"; |
1060 | | - if (this.extensionEquals("mpe,mpeg,mpg")) return "video/mpeg"; |
1061 | | - if (this.extensionEquals("mpv2")) return "video/mpeg2"; |
1062 | | - if (this.extensionEquals("qt,mov,moov")) return "video/quicktime"; |
1063 | | - |
1064 | | - if (this.extensionEquals("wvx")) return "video/x-ms-wvx"; |
1065 | | - if (this.extensionEquals("wm")) return "video/x-ms-wm"; |
1066 | | - if (this.extensionEquals("wmx")) return "video/x-ms-wmx"; |
| 1068 | + if (ext.equals("asf,asx")) return "video/x-ms-asf"; |
| 1069 | + if (ext.equals("avi")) return "video/msvideo"; |
| 1070 | + if (ext.equals("mov")) return "video/quicktime"; |
| 1071 | + if (ext.equals("mpe,mpeg,mpg")) return "video/mpeg"; |
| 1072 | + if (ext.equals("mpv2")) return "video/mpeg2"; |
| 1073 | + if (ext.equals("qt,mov,moov")) return "video/quicktime"; |
| 1074 | + |
| 1075 | + if (ext.equals("wvx")) return "video/x-ms-wvx"; |
| 1076 | + if (ext.equals("wm")) return "video/x-ms-wm"; |
| 1077 | + if (ext.equals("wmx")) return "video/x-ms-wmx"; |
1067 | 1078 |
|
1068 | | - |
1069 | | - //DEFAULT |
1070 | 1079 | return "application/octet-stream"; |
1071 | 1080 | } |
1072 | | - |
1073 | | - |
| 1081 | + |
| 1082 | + private static class FileExtension { |
| 1083 | + private String ext; |
| 1084 | + public FileExtension(String ext){ |
| 1085 | + this.ext = ext.toLowerCase(); |
| 1086 | + } |
| 1087 | + |
| 1088 | + /** |
| 1089 | + * @param obj Comma Separated List Of File Extensions |
| 1090 | + */ |
| 1091 | + public boolean equals(Object obj){ |
| 1092 | + if (obj instanceof String){ |
| 1093 | + |
| 1094 | + String[] arr = ((String) obj).split(","); |
| 1095 | + for (int i=0; i<arr.length; i++){ |
| 1096 | + String str = arr[i].trim().toLowerCase(); |
| 1097 | + if (str.equals(ext)) return true; |
| 1098 | + } |
| 1099 | + |
| 1100 | + } |
| 1101 | + return false; |
| 1102 | + } |
| 1103 | + |
| 1104 | + public int hashCode(){ |
| 1105 | + return ext.hashCode(); |
| 1106 | + } |
| 1107 | + } |
| 1108 | + |
| 1109 | + |
1074 | 1110 | //************************************************************************** |
1075 | | - //** extensionEquals |
| 1111 | + //** getContentType |
1076 | 1112 | //************************************************************************** |
1077 | | - /** Used by the getContentType to compare file extensions. |
1078 | | - * @param FileExtension Comma Separated List Of File Extensions |
| 1113 | + /** Returns the mime type associated with the file extension. This method |
| 1114 | + * only covers the most common/popular mime types. The returned mime type |
| 1115 | + * is NOT authoritative. |
1079 | 1116 | */ |
1080 | | - private boolean extensionEquals(String FileExtension){ |
1081 | | - String ext = this.getExtension(); |
1082 | | - String[] arr = FileExtension.split(","); |
1083 | | - for (int i=0; i<arr.length; i++){ |
1084 | | - String str = arr[i].trim(); |
1085 | | - if (str.equalsIgnoreCase(ext)) return true; |
1086 | | - } |
1087 | | - return false; |
| 1117 | + public String getContentType(){ |
| 1118 | + return getContentType(this.getName()); |
1088 | 1119 | } |
1089 | 1120 |
|
1090 | 1121 |
|
|
0 commit comments