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:com.dianping.resource.io.util.ResourceUtils.java

/**
 * Resolve the given resource URL to a {@code java.io.File},
 * i.e. to a file in the file system./*from  w w w.  j a va  2 s . c o  m*/
 * @param resourceUrl the resource URL to resolve
 * @param description a description of the original resource that
 * the URL was created for (for example, a class path location)
 * @return a corresponding File object
 * @throws java.io.FileNotFoundException if the URL cannot be resolved to
 * a file in the file system
 */
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
    Assert.notNull(resourceUrl, "Resource URL must not be null");
    if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
        throw new FileNotFoundException(description + " cannot be resolved to absolute file path "
                + "because it does not reside in the file system: " + resourceUrl);
    }
    try {
        return new File(toURI(resourceUrl).getSchemeSpecificPart());
    } catch (URISyntaxException ex) {
        // Fallback for URLs that are not valid URIs (should hardly ever happen).
        return new File(resourceUrl.getFile());
    }
}

From source file:fm.last.commons.io.LastFileUtils.java

/**
 * Searches for a file on local filesytem, classpath etc.
 * /*w  ww  .  j a v a2s.  c o m*/
 * @param fileName Name of file to find.
 * @param classToLoadFrom Class to use as a base for finding the file via it's classloader, if necessary.
 * @return The file if found on the file system.
 * @throws FileNotFoundException If the File could not be found.
 */
public static File getFile(String fileName, Class<?> classToLoadFrom) throws FileNotFoundException {
    File file = new File(fileName); // first try the path directly
    if (!file.exists()) {
        URL fileURL = classToLoadFrom.getResource(fileName);// next try the class's classpath
        if (fileURL == null) {
            fileURL = classToLoadFrom.getClassLoader().getResource(fileName);// next try the class' classloader's classpath
            if (fileURL == null) {
                fileURL = ClassLoader.getSystemClassLoader().getResource(fileName); // finally try the system classloader's
                // classpath
                if (fileURL == null) {
                    throw new FileNotFoundException(
                            "Could not find " + fileName + " on path, classpath " + "or system classpath");
                }
            }
        }
        file = new File(fileURL.getFile());
    }
    log.debug("Path to file located is " + file.getAbsolutePath());
    return file;
}

From source file:com.linkedin.pinot.util.TestUtils.java

public static String getFileFromResourceUrl(@Nonnull URL resourceUrl) {
    // For maven cross package use case, we need to extract the resource from jar to a temporary directory.
    String resourceUrlStr = resourceUrl.toString();
    if (resourceUrlStr.contains("jar!")) {
        try {/*from  w  w  w .  j  a  va 2  s  . c om*/
            String extension = resourceUrlStr.substring(resourceUrlStr.lastIndexOf('.'));
            File tempFile = File.createTempFile("pinot-test-temp", extension);
            String tempFilePath = tempFile.getAbsolutePath();
            LOGGER.info("Extracting from " + resourceUrlStr + " to " + tempFilePath);
            FileUtils.copyURLToFile(resourceUrl, tempFile);
            return tempFilePath;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        return resourceUrl.getFile();
    }
}

From source file:com.icloud.framework.http.URLUtil.java

/**
 * <p>//from w w w .j ava2  s  .  c  o m
 * Given two urls, a src and a destination of a redirect, it returns the
 * representative url.
 * <p>
 * 
 * <p>
 * This method implements an extended version of the algorithm used by the
 * Yahoo! Slurp crawler described here:<br>
 * <a href=
 * "http://help.yahoo.com/l/nz/yahooxtra/search/webcrawler/slurp-11.html">
 * How does the Yahoo! webcrawler handle redirects?</a> <br>
 * <br>
 * <ol>
 * <li>Choose target url if either url is malformed.</li>
 * <li>If different domains the keep the destination whether or not the
 * redirect is temp or perm</li>
 * <ul>
 * <li>a.com -> b.com*</li>
 * </ul>
 * <li>If the redirect is permanent and the source is root, keep the source.
 * </li>
 * <ul>
 * <li>*a.com -> a.com?y=1 || *a.com -> a.com/xyz/index.html</li>
 * </ul>
 * <li>If the redirect is permanent and the source is not root and the
 * destination is root, keep the destination</li>
 * <ul>
 * <li>a.com/xyz/index.html -> a.com*</li>
 * </ul>
 * <li>If the redirect is permanent and neither the source nor the
 * destination is root, then keep the destination</li>
 * <ul>
 * <li>a.com/xyz/index.html -> a.com/abc/page.html*</li>
 * </ul>
 * <li>If the redirect is temporary and source is root and destination is
 * not root, then keep the source</li>
 * <ul>
 * <li>*a.com -> a.com/xyz/index.html</li>
 * </ul>
 * <li>If the redirect is temporary and source is not root and destination
 * is root, then keep the destination</li>
 * <ul>
 * <li>a.com/xyz/index.html -> a.com*</li>
 * </ul>
 * <li>If the redirect is temporary and neither the source or the
 * destination is root, then keep the shortest url. First check for the
 * shortest host, and if both are equal then check by path. Path is first by
 * length then by the number of / path separators.</li>
 * <ul>
 * <li>a.com/xyz/index.html -> a.com/abc/page.html*</li>
 * <li>*www.a.com/xyz/index.html -> www.news.a.com/xyz/index.html</li>
 * </ul>
 * <li>If the redirect is temporary and both the source and the destination
 * are root, then keep the shortest sub-domain</li>
 * <ul>
 * <li>*www.a.com -> www.news.a.com</li>
 * </ul>
 * <br>
 * While not in this logic there is a further piece of representative url
 * logic that occurs during indexing and after scoring. During creation of
 * the basic fields before indexing, if a url has a representative url
 * stored we check both the url and its representative url (which should
 * never be the same) against their linkrank scores and the highest scoring
 * one is kept as the url and the lower scoring one is held as the orig url
 * inside of the index.
 * 
 * @param src
 *            The source url.
 * @param dst
 *            The destination url.
 * @param temp
 *            Is the redirect a temporary redirect.
 * 
 * @return String The representative url.
 */
public static String chooseRepr(String src, String dst, boolean temp) {

    // validate both are well formed urls
    URL srcUrl;
    URL dstUrl;
    try {
        srcUrl = new URL(src);
        dstUrl = new URL(dst);
    } catch (MalformedURLException e) {
        return dst;
    }

    // get the source and destination domain, host, and page
    String srcDomain = URLUtil.getDomainName(srcUrl);
    String dstDomain = URLUtil.getDomainName(dstUrl);
    String srcHost = srcUrl.getHost();
    String dstHost = dstUrl.getHost();
    String srcFile = srcUrl.getFile();
    String dstFile = dstUrl.getFile();

    // are the source and destination the root path url.com/ or url.com
    boolean srcRoot = (srcFile.equals("/") || srcFile.length() == 0);
    boolean destRoot = (dstFile.equals("/") || dstFile.length() == 0);

    // 1) different domain them keep dest, temp or perm
    // a.com -> b.com*
    //
    // 2) permanent and root, keep src
    // *a.com -> a.com?y=1 || *a.com -> a.com/xyz/index.html
    //
    // 3) permanent and not root and dest root, keep dest
    // a.com/xyz/index.html -> a.com*
    //
    // 4) permanent and neither root keep dest
    // a.com/xyz/index.html -> a.com/abc/page.html*
    //
    // 5) temp and root and dest not root keep src
    // *a.com -> a.com/xyz/index.html
    //
    // 7) temp and not root and dest root keep dest
    // a.com/xyz/index.html -> a.com*
    //
    // 8) temp and neither root, keep shortest, if hosts equal by path else
    // by
    // hosts. paths are first by length then by number of / separators
    // a.com/xyz/index.html -> a.com/abc/page.html*
    // *www.a.com/xyz/index.html -> www.news.a.com/xyz/index.html
    //
    // 9) temp and both root keep shortest sub domain
    // *www.a.com -> www.news.a.com

    // if we are dealing with a redirect from one domain to another keep the
    // destination
    if (!srcDomain.equals(dstDomain)) {
        return dst;
    }

    // if it is a permanent redirect
    if (!temp) {

        // if source is root return source, otherwise destination
        if (srcRoot) {
            return src;
        } else {
            return dst;
        }
    } else { // temporary redirect

        // source root and destination not root
        if (srcRoot && !destRoot) {
            return src;
        } else if (!srcRoot && destRoot) { // destination root and source
            // not
            return dst;
        } else if (!srcRoot && !destRoot && (srcHost.equals(dstHost))) {

            // source and destination hosts are the same, check paths, host
            // length
            int numSrcPaths = srcFile.split("/").length;
            int numDstPaths = dstFile.split("/").length;
            if (numSrcPaths != numDstPaths) {
                return (numDstPaths < numSrcPaths ? dst : src);
            } else {
                int srcPathLength = srcFile.length();
                int dstPathLength = dstFile.length();
                return (dstPathLength < srcPathLength ? dst : src);
            }
        } else {

            // different host names and both root take the shortest
            int numSrcSubs = srcHost.split("\\.").length;
            int numDstSubs = dstHost.split("\\.").length;
            return (numDstSubs < numSrcSubs ? dst : src);
        }
    }
}

From source file:dk.statsbiblioteket.util.Files.java

/**
 * Download the contents of an {@link URL} and store it on disk.
 *
 * if {@code target} argument is a directory the file will be stored
 * here with the basename as extracted from the url. If it points to
 * a non-existing file it will be written to a file with that name.
 *
 * If the {@code target} argument points to an already existing file
 * and {@code overwrite == false} a {@link FileAlreadyExistsException}
 * will be thrown. Otherwise the file will be overwritten.
 *
 * @param url       where the data should be downloaded from.
 * @param target    the place to store the downloaded data. This can be either a file or a directory.
 * @param overwrite whether or not to overwrite the target file if it already exist.
 * @return the resulting file./*from  w  w  w . j a v  a2  s  .c  om*/
 * @throws ConnectException     if there was an error opening a stream to the url.
 * @throws IOException          if there was an error downloading the file or writing it to disk.
 * @throws NullPointerException if one of the input arguments are null.
 */
public static File download(URL url, File target, boolean overwrite) throws IOException {
    log.trace("download(" + url + ", " + target + ", " + overwrite + ") called");
    if (url == null) {
        throw new NullPointerException("url is null");
    }
    if (target == null) {
        throw new NullPointerException("target is null");
    }

    File result;

    if (target.isDirectory()) {
        result = new File(target, new File(url.getFile()).getName());
    } else {
        result = target;
    }

    if (result.exists() && !overwrite) {
        throw new FileAlreadyExistsException(target);
    }

    InputStream con;
    try {
        // No BufferedInputStream as it does not support 2GB+.
        con = url.openStream();
    } catch (IOException e) {
        throw new IOException("Failed to open stream to '" + url + "'", e);
    }
    OutputStream out = new FileOutputStream(result);
    Streams.pipe(con, out);
    return result;
}

From source file:cn.org.rapid_framework.generator.util.ResourceHelper.java

/**
 * Resolve the given resource URL to a <code>java.io.File</code>,
 * i.e. to a file in the file system.//from ww w.  ja  v  a  2  s  .c  o  m
 * @param resourceUrl the resource URL to resolve
 * @param description a description of the original resource that
 * the URL was created for (for example, a class path location)
 * @return a corresponding File object
 * @throws FileNotFoundException if the URL cannot be resolved to
 * a file in the file system
 */
public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
    if (resourceUrl == null)
        throw new IllegalArgumentException("Resource URL must not be null");

    if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
        throw new FileNotFoundException(description + " cannot be resolved to absolute file path "
                + "because it does not reside in the file system: " + resourceUrl);
    }
    try {
        return new File(toURI(resourceUrl).getSchemeSpecificPart());
    } catch (URISyntaxException ex) {
        // Fallback for URLs that are not valid URIs (should hardly ever happen).
        return new File(resourceUrl.getFile());
    }
}

From source file:net.rim.ejde.internal.util.ComponentPackUtils.java

/**
 * Gets the component pack paths based on the CP extension point
 *
 * @return the component pack paths//w ww .j  a v a2 s  . c o m
 */
public static Map<String, JDEInfo> getComponentPackPaths() {
    ComponentPackUtils.log.debug("Starting Search for CPs"); //$NON-NLS-1$
    IExtension[] extensions;
    final IExtensionRegistry registry = RegistryFactory.getRegistry();
    final IExtensionPoint point = registry.getExtensionPoint(IConstants.CP_EXTENSION_POINT_ID);
    final TreeMap<String, JDEInfo> packs = new TreeMap<String, JDEInfo>(new ComponentPackComparator());

    if ((null == point) || !point.isValid()) {
        ComponentPackUtils.log.debug("Extention Point Null or Invalid"); //$NON-NLS-1$
        return packs;
    }
    extensions = point.getExtensions();

    if ((null == extensions) || (0 == extensions.length)) {
        ComponentPackUtils.log.debug("Extentions Null or Non-Existant"); //$NON-NLS-1$
        return packs;
    }

    Bundle bundle;
    URL url;

    String name, version, path;
    File file;

    for (final IExtension extension : extensions) {
        try {
            bundle = Platform.getBundle(extension.getNamespaceIdentifier());
            final int bundleState = bundle.getState();

            if ((bundleState != Bundle.UNINSTALLED) && (bundleState != Bundle.STOPPING)) {

                url = FileLocator.resolve(FileLocator.find(bundle, Path.ROOT, null));

                name = bundle.getHeaders().get(Constants.BUNDLE_NAME);
                version = bundle.getHeaders().get(Constants.BUNDLE_VERSION);

                if (StringUtils.isBlank(name) || StringUtils.isBlank(version)) {
                    break;
                }

                file = new File(url.getFile());

                if (!file.exists()) {
                    break;
                }

                path = file.getAbsolutePath() + ComponentPackUtils.SUFFIX;

                ComponentPackUtils.log.debug("CP named " + name + " was found at " + path); //$NON-NLS-1$ //$NON-NLS-2$
                packs.put(name, new JDEInfo(name, path, version));
            }
        } catch (final Throwable e) {
            ComponentPackUtils.log.error(e.getMessage(), e);
        }
    }
    return packs;
}

From source file:es.uvigo.ei.sing.jarvest.core.HTTPUtils.java

public synchronized static InputStream doPost(String urlstring, String queryString, String separator,
        Map<String, String> additionalHeaders, StringBuffer charsetb) throws HttpException, IOException {
    System.err.println("posting to: " + urlstring + ". query string: " + queryString);
    HashMap<String, String> query = parseQueryString(queryString, separator);
    HttpClient client = getClient();//from   w ww  .  j  a v  a2s  .  c  om

    URL url = new URL(urlstring);
    int port = url.getPort();
    if (port == -1) {
        if (url.getProtocol().equalsIgnoreCase("http")) {
            port = 80;
        }
        if (url.getProtocol().equalsIgnoreCase("https")) {
            port = 443;
        }
    }

    client.getHostConfiguration().setHost(url.getHost(), port, url.getProtocol());
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    final PostMethod post = new PostMethod(url.getFile());
    addHeaders(additionalHeaders, post);
    post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    post.setRequestHeader("Accept", "*/*");
    // Prepare login parameters
    NameValuePair[] valuePairs = new NameValuePair[query.size()];

    int counter = 0;

    for (String key : query.keySet()) {
        //System.out.println("Adding pair: "+key+": "+query.get(key));
        valuePairs[counter++] = new NameValuePair(key, query.get(key));

    }

    post.setRequestBody(valuePairs);
    //authpost.setRequestEntity(new StringRequestEntity(requestEntity));

    client.executeMethod(post);

    int statuscode = post.getStatusCode();
    InputStream toret = null;
    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
            || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
        Header header = post.getResponseHeader("location");
        if (header != null) {
            String newuri = header.getValue();
            if ((newuri == null) || (newuri.equals(""))) {
                newuri = "/";
            }

        } else {
            System.out.println("Invalid redirect");
            System.exit(1);
        }
    } else {
        charsetb.append(post.getResponseCharSet());
        final InputStream in = post.getResponseBodyAsStream();

        toret = new InputStream() {

            @Override
            public int read() throws IOException {
                return in.read();
            }

            @Override
            public void close() {
                post.releaseConnection();
            }

        };

    }

    return toret;
}

From source file:MainClass.java

public static void saveBinaryFile(URL u) {
    int bufferLength = 128;
    try {//from w ww. j av a 2s . c  om
        URLConnection uc = u.openConnection();
        String ct = uc.getContentType();
        int contentLength = uc.getContentLength();
        if (ct.startsWith("text/") || contentLength == -1) {
            System.err.println("This is not a binary file.");
            return;
        }

        InputStream stream = uc.getInputStream();
        byte[] buffer = new byte[contentLength];
        int bytesread = 0;
        int offset = 0;
        while (bytesread >= 0) {
            bytesread = stream.read(buffer, offset, bufferLength);
            if (bytesread == -1)
                break;
            offset += bytesread;
        }
        if (offset != contentLength) {
            System.err.println("Error: Only read " + offset + " bytes");
            System.err.println("Expected " + contentLength + " bytes");
        }

        String theFile = u.getFile();
        theFile = theFile.substring(theFile.lastIndexOf('/') + 1);
        FileOutputStream fout = new FileOutputStream(theFile);
        fout.write(buffer);
    } catch (Exception e) {
        System.err.println(e);
    }
    return;
}

From source file:com.linkedin.pinot.common.TestUtils.java

public static String getFileFromResourceUrl(URL resourceUrl) {
    System.out.println(resourceUrl);
    // Check if we need to extract the resource to a temporary directory
    String resourceUrlStr = resourceUrl.toString();
    if (resourceUrlStr.contains("jar!")) {
        try {//from  w ww. j  av a 2 s .c  o m
            String extension = resourceUrlStr.substring(resourceUrlStr.lastIndexOf('.'));
            File tempFile = File.createTempFile("pinot-test-temp", extension);
            LOGGER.info("Extractng from " + resourceUrlStr + " to " + tempFile.getAbsolutePath());
            System.out.println("Extractng from " + resourceUrlStr + " to " + tempFile.getAbsolutePath());
            FileUtils.copyURLToFile(resourceUrl, tempFile);
            return tempFile.getAbsolutePath();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        System.out.println("Not extracting plain file " + resourceUrl);
        return resourceUrl.getFile();
    }
}