Java URL to File Name getFileName(URL url)

Here you can find the source of getFileName(URL url)

Description

Returns the file name (without the path) of the resource specified by the given url.

License

LGPL

Parameter

Parameter Description
url the url to get the filename out of

Return

String containing the name of the file, zero-length if none

Declaration

public static String getFileName(URL url) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.net.URL;

public class Main {
    /**/*from  ww w .jav  a 2s. c  o m*/
     * Returns the file name (without the path) of the resource specified
     * by the given url.
     * @param url the url to get the filename out of
     * @return String containing the name of the file, zero-length if none
     */
    public static String getFileName(URL url) {
        return url.getPath().substring(stripResource(url.getPath()).length());
    }

    /**
     * returns the resource path without the resource.
     * @param path the path to the resource
     * @return path without the resource itself
     */
    public static String stripResource(String path) {
        String result = null;
        if (path != null) {
            int pos = path.lastIndexOf("/");
            result = path.substring(0, pos + 1);
        }
        return result;
    }
}

Related

  1. getFileName(URL extUrl)
  2. getFileName(URL url)
  3. getFileName(URL url)
  4. getFileName(URL url)
  5. getFileName(URL url)
  6. getFileName(URLConnection urlC)
  7. getFileName2(String url)
  8. getFileNameArray(String url)
  9. getFileNameFromContentDisposition(URLConnection connection)