Java tutorial
//package com.java2s; public class Main { public static String getName(String pathOrUrl) { return getName(pathOrUrl, false); } /** * Gets name. * * @param pathOrUrl the path or url * @param useHash the use hash * @return the name */ public static String getName(String pathOrUrl, boolean useHash) { if (useHash) { return pathOrUrl.replace("/", "_") + "." + getExtension(pathOrUrl); } int pos = pathOrUrl.lastIndexOf('/'); if (0 <= pos) { return pathOrUrl.substring(pos + 1); } else { return String.valueOf(System.currentTimeMillis()) + "." + getExtension(pathOrUrl); } } public static String getExtension(String pathOrUrl) { int dotPos = pathOrUrl.lastIndexOf('.'); if (0 <= dotPos) { return pathOrUrl.substring(dotPos + 1); } else { return "ext"; } } }