FileUtils { public static String getNameFromURI(URI u) { if ("file".equalsIgnoreCase(u.getScheme())) { return FilenameUtils.removeExtension(new File(u.getPath()).getName()); } else { if("jdbc".equalsIgnoreCase(u.getScheme())) { try { u = URI.create(u.getSchemeSpecificPart()); } catch (IllegalArgumentException ex) { } } String q = u.getQuery(); if (q != null && !q.isEmpty()) { String[] pat = q.split("&"); for (String aPat : pat) { if (aPat.toLowerCase().startsWith(TABLE_PARAM)) { return aPat.substring(TABLE_PARAM.length()); } else if (aPat.toLowerCase().startsWith(TABLE_PARAM_JDBC)) { return aPat.substring(TABLE_PARAM_JDBC.length()); } } } String path = u.getPath(); if (path != null && !path.isEmpty()) { String[] paths = path.split("/"); if(paths.length>=1) { if(!paths[paths.length-1].isEmpty()) { return paths[paths.length-1]; } } } } throw new UnsupportedOperationException(); } private  FileUtils(); @Deprecated static void copyDirsRecursively(File sourceDir, File destDir); @Deprecated static boolean deleteFile(File file); static void deleteSHPFiles(File fileShp); @Deprecated static boolean deleteFileInDir(File dir); @Deprecated static boolean deleteDir(File dir); @Deprecated static void copyFileToDirectory(File file, File destDir); @Deprecated static long copy(File input, File output); @Deprecated static long copy(File input, File output, byte[] copyBuffer); @Deprecated static long copy(InputStream in, File outputFile); @Deprecated static long copy(InputStream in, OutputStream out); @Deprecated static long copy(InputStream in, OutputStream out, byte[] copyBuffer); @Deprecated static void download(URL url, File file); static void zip(File toZip, File outFile); static void unzip(File zipFile, File destDir); static String getRelativePath(File base, File file); @Deprecated static byte[] getContent(File file); @Deprecated static byte[] getContent(InputStream fis); static byte[] getMD5(File file); @Deprecated static String toHexString(byte[] messageDigest); @Deprecated static void setContents(File file, String content); @Deprecated static String getFileNameWithoutExtensionU(File file); static File getFileWithExtension(File file, final String extension); static String getNameFromURI(URI u);  }
@Test public void testNameForURI() { File f = new File("/home/me/toto.shp"); assertEquals("toto", FileUtils.getNameFromURI(f.toURI())); URI u = URI.create("http: assertEquals("mytable", FileUtils.getNameFromURI(u)); URI exoticURI = URI.create("pgsql: assertEquals("g4districts98", FileUtils.getNameFromURI(exoticURI)); URI uJDBC = URI.create("postgresql: assertEquals("bat", FileUtils.getNameFromURI(uJDBC)); u = URI.create("jdbc: assertEquals("helloworld", FileUtils.getNameFromURI(u)); u = URI.create("jdbc:h2:/home/user/OrbisGIS/database?catalog=&schema=&table=LANDCOVER2000"); assertEquals("LANDCOVER2000", FileUtils.getNameFromURI(u)); u = URI.create("../src/test/resources/data/landcover2000.shp"); assertEquals("landcover2000.shp", FileUtils.getNameFromURI(u)); }