@@ -88,15 +88,11 @@ public void loadMimetypes(InputStream is) throws IOException {
8888 }
8989 }
9090 }
91-
91+
9292 public String getMimetype (String fileName ) {
93- int lastPeriodIndex = fileName .lastIndexOf ("." );
94- if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < fileName .length ()) {
95- String ext = fileName .substring (lastPeriodIndex + 1 ).toLowerCase ();
96- if (extensionToMimetypeMap .keySet ().contains (ext )) {
97- String mimetype = (String ) extensionToMimetypeMap .get (ext );
98- return mimetype ;
99- }
93+ String mimeType = getMimetypeByExt (fileName );
94+ if (mimeType != null ) {
95+ return mimeType ;
10096 }
10197 return DEFAULT_MIMETYPE ;
10298 }
@@ -110,26 +106,28 @@ public String getMimetype(File file, String key) {
110106 }
111107
112108 public String getMimetype (String primaryObject , String secondaryObject ) {
113- // primary
114- int lastPeriodIndex = primaryObject . lastIndexOf ( "." );
115- if ( lastPeriodIndex > 0 && lastPeriodIndex + 1 < primaryObject . length ()) {
116- String ext = primaryObject . substring ( lastPeriodIndex + 1 ). toLowerCase ();
117- if ( extensionToMimetypeMap . keySet (). contains ( ext )) {
118- String mimetype = ( String ) extensionToMimetypeMap . get ( ext );
119- return mimetype ;
120- }
109+ String mimeType = getMimetypeByExt ( primaryObject );
110+ if ( mimeType != null ) {
111+ return mimeType ;
112+ }
113+
114+ mimeType = getMimetypeByExt ( secondaryObject );
115+ if ( mimeType != null ) {
116+ return mimeType ;
121117 }
122- // secondary
123- lastPeriodIndex = secondaryObject .lastIndexOf ("." );
124- if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < secondaryObject .length ()) {
125- String ext = secondaryObject .substring (lastPeriodIndex + 1 ).toLowerCase ();
118+
119+ return DEFAULT_MIMETYPE ;
120+ }
121+
122+ private String getMimetypeByExt (String fileName ) {
123+ int lastPeriodIndex = fileName .lastIndexOf ("." );
124+ if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < fileName .length ()) {
125+ String ext = fileName .substring (lastPeriodIndex + 1 ).toLowerCase ();
126126 if (extensionToMimetypeMap .keySet ().contains (ext )) {
127127 String mimetype = (String ) extensionToMimetypeMap .get (ext );
128128 return mimetype ;
129129 }
130130 }
131- // default
132- return DEFAULT_MIMETYPE ;
131+ return null ;
133132 }
134-
135133}
0 commit comments