fork(1) download
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		ArrayList<String> urls = new ArrayList<String>();
		urls.add(null);
		urls.add("");
		urls.add("file:///home/user/test.html");
		urls.add("file:///home/user/test.html?id=902");
		urls.add("file:///home/user/test.html#footer");
		urls.add("http://e...content-available-to-author-only...e.com");
		urls.add("http://w...content-available-to-author-only...e.com");
		urls.add("http://w...content-available-to-author-only...e.txt");
		urls.add("http://e...content-available-to-author-only...e.com/");
		urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html");
		urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html?param=value");
		urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor");
		urls.add("http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor?param=value");
		
		for (String url : urls) {
			System.out.println("Input: \"" + url + "\" Output: \"" + getFileNameFromURL(url) + "\"");
		}

	}
	
	public static String getFileNameFromURL(String url) {
		if (url == null) {
			return "";
		}
		try {
			URL resource = new URL(url);
			String host = resource.getHost();
			if (host.length() > 0 && url.endsWith(host)) {
				// handle ...example.com
				return "";
			}
		}
		catch(MalformedURLException e) {
			return "";	
		}
		
		int startIndex = url.lastIndexOf('/') + 1;
		int length = url.length();
		
		// find end index for ?
		int lastQMPos = url.lastIndexOf('?');
		if (lastQMPos == -1) {
			lastQMPos = length;	
		}
		
		// find end index for #
		int lastHashPos = url.lastIndexOf('#');
		if (lastHashPos == -1) {
			lastHashPos = length;	
		}
		
		// calculate the end index
		int endIndex = Math.min(lastQMPos, lastHashPos);
		return url.substring(startIndex, endIndex);
	}
}
Success #stdin #stdout 0.1s 27676KB
stdin
Standard input is empty
stdout
Input: "null" Output: ""
Input: "" Output: ""
Input: "file:///home/user/test.html" Output: "test.html"
Input: "file:///home/user/test.html?id=902" Output: "test.html"
Input: "file:///home/user/test.html#footer" Output: "test.html"
Input: "http://e...content-available-to-author-only...e.com" Output: ""
Input: "http://w...content-available-to-author-only...e.com" Output: ""
Input: "http://w...content-available-to-author-only...e.txt" Output: ""
Input: "http://e...content-available-to-author-only...e.com/" Output: ""
Input: "http://e...content-available-to-author-only...e.com/a/b/c/test.html" Output: "test.html"
Input: "http://e...content-available-to-author-only...e.com/a/b/c/test.html?param=value" Output: "test.html"
Input: "http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor" Output: "test.html"
Input: "http://e...content-available-to-author-only...e.com/a/b/c/test.html#anchor?param=value" Output: "test.html"