Example usage for java.net URL getFile

List of usage examples for java.net URL getFile

Introduction

In this page you can find the example usage for java.net URL getFile.

Prototype

public String getFile() 

Source Link

Document

Gets the file name of this URL .

Usage

From source file:Main.java

public static <T> Set<Class<T>> findClassesAssignableFrom(String packageName, Class<T> assignableFrom)
        throws IOException, ClassNotFoundException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Set<Class<T>> classes = new HashSet<Class<T>>();
    String path = packageName.replace('.', '/');
    URL resource = loader.getResource(path);
    if (resource != null) {
        String filePath = resource.getFile();
        if (filePath != null && new File(filePath).isDirectory()) {
            for (String file : new File(filePath).list()) {
                if (file.endsWith(".class")) {
                    String name = packageName + '.' + file.substring(0, file.indexOf(".class"));
                    Class<T> clazz = (Class<T>) Class.forName(name);
                    if (assignableFrom.isAssignableFrom(clazz))
                        classes.add(clazz);
                }/*from  www  .  jav  a 2 s. c om*/
            }
        }
    }
    return classes;
}

From source file:Main.java

private static void recurse(List<Class<?>> classes, String packageName, Class<? extends Annotation> a)
        throws ClassNotFoundException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    String path = packageName.replace('.', '/');
    URL resource = loader.getResource(path);
    if (resource != null) {
        String filePath = resource.getFile();
        if (filePath != null && new File(filePath).isDirectory()) {
            for (String file : new File(filePath).list()) {
                if (file.endsWith(".class")) {
                    String name = packageName + '.' + file.substring(0, file.indexOf(".class"));
                    Class<?> clazz = Class.forName(name);
                    if (clazz.isAnnotationPresent(a))
                        classes.add(clazz);
                } else if (new File(filePath, file).isDirectory()) {
                    recurse(classes, packageName + "." + file, a);
                }/*from   w  w w  .  j a  v a  2s  .  c om*/
            }
        }
    }
}

From source file:costumetrade.common.util.PdfUtils.java

public static PDDocument createImagePdf(String urlString) {

    File file = null;/*from w w w .  j av a2 s  .  c o  m*/
    try {
        URL url = new URL(urlString);
        file = new File(path + url.getFile());
        FileUtils.copyURLToFile(url, file);
        return createImagePdf(file);

    } catch (IOException e) {
        throw new RuntimeException("?PDF", e);
    } finally {
        if (file != null) {
            file.delete();
        }
    }

}

From source file:com.microsoft.webapp.util.WebAppUtils.java

public static Image getImage(String entry) {
    Image image = null;/*from w ww .  j av  a2 s  . co  m*/
    try {
        URL imgUrl = Activator.getDefault().getBundle().getEntry(entry);
        URL imgFileURL = FileLocator.toFileURL(imgUrl);
        URL path = FileLocator.resolve(imgFileURL);
        String imgpath = path.getFile();
        image = new Image(null, new FileInputStream(imgpath));
    } catch (Exception e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
    return image;
}

From source file:me.cavar.pg2tei.Gutenberg2TEI.java

/**
 *
 * @param catalogURLStr// www  .j a  v a2s .  c o m
 * @param outputFolder
 * @param catalogOutFN
 */
public static void fetchRDF(String catalogURLStr, String outputFolder, String catalogOutFN) {
    Fetcher myFetcher = new Fetcher(catalogURLStr, outputFolder, catalogOutFN);
    try {
        URL catalogURL = new URL(catalogURLStr);
        File tmpFile = new File(catalogURL.getFile());
        File zipFile = new File(outputFolder, tmpFile.getName());
        File rdfFile = new File(outputFolder, catalogOutFN);

        if (!rdfFile.exists()) {
            if (!zipFile.exists()) {
                // get the catalog.rdf.zip file
                System.out.print("Fetching Gutenberg Catalog-file as RDF-Zip... writing to ");
                System.out.println(zipFile);
                myFetcher.getCatalog(catalogURL, zipFile);
                System.out.println("Done");
            }
            // unzip the catalog.rdf.zip file to catalog.rdf
            System.out.print("Unzipping the Catalog-file... ");
            myFetcher.unZip(zipFile);
            System.out.println("Done");
        }
    } catch (IOException e) {
        System.out.println("IOError");
    }
}

From source file:Main.java

private static URL getRootUrlForClass(Class<?> cls) {
    ClassLoader classLoader = cls.getClassLoader();
    String resource = cls.getName().replace('.', '/') + ".class";
    if (classLoader == null) {
        // A null class loader means the bootstrap class loader. In this case we use the
        // system class loader. This is safe since we can assume that the system class
        // loader uses parent first as delegation policy.
        classLoader = ClassLoader.getSystemClassLoader();
    }//w w  w  .ja v a2  s  .c o m
    URL url = classLoader.getResource(resource);
    if (url == null) {
        return null;
    }
    String file = url.getFile();
    if (file.endsWith(resource)) {
        try {
            return new URL(url.getProtocol(), url.getHost(), url.getPort(),
                    file.substring(0, file.length() - resource.length()));
        } catch (MalformedURLException ex) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:com.github.walterfan.util.http.URLHelper.java

public static void URL2File(String strUrl, String filename) {
    OutputStream output = null;//from  ww w . j  av a2s .  co  m
    try {
        if (null == filename) {
            URL url = new URL(strUrl);
            filename = FilenameUtils.getName(url.getFile());
        }
        System.out.println("write " + filename);
        File file = new File(filename);
        output = new FileOutputStream(file);
        URL2Stream(strUrl, output);

    } catch (MalformedURLException ex) {
        System.err.println(ex);
    } catch (FileNotFoundException ex) {
        System.err.println("Failed to open stream to URL: " + ex);
    } catch (IOException ex) {
        System.err.println("Error reading URL content: " + ex);
    } finally {
        IOUtils.closeQuietly(output);
    }
}

From source file:com.oculusinfo.binning.io.impl.ZipResourcePyramidSource.java

static ZipResourcePyramidSource getZipSource(String rootpath, String extension) {
    Pair<String, String> key = new Pair<>(rootpath, extension);
    if (!_zipfileCache.containsKey(key)) {
        synchronized (_zipfileCache) {
            if (!_zipfileCache.containsKey(key)) {
                if (rootpath.startsWith("file://")) {
                    rootpath = rootpath.substring(7);
                } else if (rootpath.startsWith("res://")) {
                    rootpath = rootpath.substring(6);
                    URL zipFile = PyramidIOFactory.class.getResource(rootpath);
                    rootpath = zipFile.getFile();
                }/* www  . ja va2s  .co  m*/

                ZipResourcePyramidSource source = new ZipResourcePyramidSource(rootpath, extension);
                _zipfileCache.put(key, source);
            }
        }
    }
    return _zipfileCache.get(key);
}

From source file:com.shelfmap.stepsfinder.CandidateStepsFactory.java

public static URL codeLocationFromParentPackage(Class<?> codeLocationClass) {
    String simpleName = codeLocationClass.getSimpleName() + ".class";
    String pathOfClass = codeLocationClass.getName().replace(".", "/") + ".class";
    URL classResource = codeLocationClass.getClassLoader().getResource(pathOfClass);
    String codeLocationPath = removeEnd(classResource.getFile(), simpleName);
    return codeLocationFromPath(codeLocationPath);
}

From source file:ResourceUtils.java

/**
 * Extract the URL for the actual jar file from the given URL
 * (which may point to a resource in a jar file or to a jar file itself).
 * @param jarUrl the original URL//from   w ww .  ja  va 2s .  c  o m
 * @return the URL for the actual jar file
 * @throws MalformedURLException if no valid jar file URL could be extracted
 */
public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException {
    String urlFile = jarUrl.getFile();
    int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR);
    if (separatorIndex != -1) {
        String jarFile = urlFile.substring(0, separatorIndex);
        try {
            return new URL(jarFile);
        } catch (MalformedURLException ex) {
            // Probably no protocol in original jar URL, like "jar:C:/mypath/myjar.jar".
            // This usually indicates that the jar file resides in the file system.
            if (!jarFile.startsWith("/")) {
                jarFile = "/" + jarFile;
            }
            return new URL(FILE_URL_PREFIX + jarFile);
        }
    } else {
        return jarUrl;
    }
}