Java URL Connection getBaseURL(URLConnection conn)

Here you can find the source of getBaseURL(URLConnection conn)

Description

get the base URL from a connection

License

Open Source License

Declaration

static public String getBaseURL(URLConnection conn) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2011 eBay Inc.//w  w w.  j ava2s  .  c  o m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *******************************************************************************/

import java.net.URLConnection;

public class Main {
    /**
     * get the base URL from a connection
     */
    static public String getBaseURL(URLConnection conn) {
        String url = conn.getURL().toExternalForm();
        return getBaseURL(url);
    }

    /**
     * Returns the base URL with ending slash
     */
    static public String getBaseURL(String url_str) {
        if (url_str == null)
            return null;

        String s = url_str;
        int i = s.lastIndexOf(';');
        if (i > -1)
            s = s.substring(0, i);

        i = s.lastIndexOf('#');
        if (i > -1)
            s = s.substring(0, i);

        i = s.lastIndexOf('?');
        if (i > -1)
            s = s.substring(0, i);

        i = s.indexOf("://");
        if (i > 0)
            i += 4;
        int j = s.lastIndexOf('/');
        if (j > i) {
            s = s.substring(0, j);
        }
        if (!s.endsWith("/"))
            s = s + "/";
        return s;
    }
}

Related

  1. findClasses(URL resource, String packageName, boolean annotated)
  2. findClasspathUrls(ClassLoader classLoader)
  3. findResourceInJarPackage(URL url, String packageName, String packageDirName, boolean recursive, List resources)
  4. getAsStream(URL url)
  5. getBaseAuthInputStreamFromURL(String query, String basicAuthString)
  6. getBooleanFromUrl(String url)
  7. getBytes(String urlStr)
  8. getCharset(URLConnection connection)
  9. getCharsFromURL(URL url)