Here you can find the source of getFileExtensionFromUrl(String url)
Parameter | Description |
---|---|
url | a parameter |
public static String getFileExtensionFromUrl(String url)
//package com.java2s; //License from project: Apache License import java.util.regex.Pattern; public class Main { /**/*from w w w . java 2s . c o m*/ * Returns the file extension or an empty string if there is no extension. * This method is a convenience method for obtaining the extension of a url * and has undefined results for other Strings. * * @param url * @return The file extension of the given url. */ public static String getFileExtensionFromUrl(String url) { if (url != null && url.length() > 0) { int query = url.lastIndexOf('?'); if (query > 0) { url = url.substring(0, query); } int filenamePos = url.lastIndexOf('/'); String filename = 0 <= filenamePos ? url .substring(filenamePos + 1) : url; // if the filename contains special characters, we don't // consider it valid for our matching purposes: if (filename.length() > 0 && Pattern.matches("[a-zA-Z_0-9\\.\\-\\(\\)]+", filename)) { int dotPos = filename.lastIndexOf('.'); if (0 <= dotPos) { return filename.substring(dotPos + 1); } } } return ""; } }