Java Utililty Methods URL to File Name

List of utility methods to do URL to File Name

Description

The list of methods to do URL to File Name are organized into topic(s).

Method

StringgetFileName(HttpURLConnection conn)
get File Name
String fileName = conn.getHeaderField("Content-Disposition");
Matcher matcher = REGEX_FILE_NAME.matcher(fileName);
if (matcher.find()) {
    return matcher.group(1);
return null;
StringgetFileName(String urlStr)
get File Name
URL url;
try {
    url = new URL(urlStr);
} catch (MalformedURLException ex) {
    System.err.println("Invalid URL: " + urlStr); 
    assert false;
    return null;
String[] urlParts = url.getPath().split("\\/");
return urlParts[urlParts.length - 1];
StringgetFilename(String urlString)
get Filename
String[] urlpaths;
try {
    URL url = new URL(urlString);
    urlpaths = url.getPath().split("/");
} catch (Exception e) {
    return null;
return urlpaths[urlpaths.length - 1];
...
StringgetFileName(String urlString)
get File Name
URL url = null;
try {
    url = new URL(urlString);
    return new File(url.getFile()).getName();
} catch (MalformedURLException e) {
    return new File(urlString).getName();
StringgetFileName(URL extUrl)
get File Name
String filename = "";
String path = extUrl.getPath();
String[] pathContents = path.split("[\\\\/]");
if (pathContents != null) {
    int pathContentsLength = pathContents.length;
    System.out.println("Path Contents Length: " + pathContentsLength);
    for (int i = 0; i < pathContents.length; i++) {
        System.out.println("Path " + i + ": " + pathContents[i]);
...
StringgetFileName(URL url)
get File Name
String s = url.getFile();
return decodeURL(s, "UTF8");
StringgetFileName(URL url)
returns the last element of the URL's path
String[] pathElements = url.getPath().split("/");
return pathElements[pathElements.length - 1];
StringgetFileName(URL url)
get File Name
return new File(URLDecoder.decode(url.getFile(), "UTF-8")).getName();
StringgetFileName(URL url)
get File Name
String fileName = url.getFile();
if (fileName.lastIndexOf('?') != -1) {
    fileName = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('?'));
} else {
    fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
return URLDecoder.decode(fileName);
StringgetFileName(URL url)
Returns the file name (without the path) of the resource specified by the given url.
return url.getPath().substring(stripResource(url.getPath()).length());