private String guessImageFormat ( String contentType , String imagePath ) throws IOException { String format = "png" ; if ( contentType == null ) { contentType = URLConnection . guessContentTypeFromName ( imagePath ) ; } if ( contentType != null ) { format = contentType . replaceFirst ( "^image/([^;]+)[;]?.*$" , "$1" ) ; } else { int queryStringIndex = imagePath . indexOf ( ' ' ) ; if ( queryStringIndex != - 1 ) { imagePath = imagePath . substring ( 0 , queryStringIndex ) ; } String [ ] pathTokens = imagePath . split ( "\\." ) ; if ( pathTokens . length > 1 ) { format = pathTokens [ pathTokens . length - 1 ] ; } } return format ; }
Attempt to obtain the image format used to write the image from the contentType or the image s file extension .