get Url Directory - Java java.io

Java examples for java.io:URL

Description

get Url Directory

Demo Code


import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class Main{
    public static void main(String[] argv){
        String url = "http://java2s.com/a/v/d/e/d.htm";
        System.out.println(getUrlDirectory(url));
    }//ww w .ja  v  a 2 s . c  om
    
    public static String getUrlDirectory(String url) {
        if (StringUtils.isBlank(url))
            return url;
        int begin = url.indexOf("://");
        if (begin == -1)
            begin = 0;
        else
            begin = begin + 3;
        begin = url.indexOf("/", begin);
        // ????????
        if (begin == -1)
            return "/";
        else {
            url = url.substring(begin);
            int end = url.indexOf("?");
            if (end == -1)
                end = url.length();
            url = url.substring(0, end);
            end = url.lastIndexOf("/");
            if (end == -1)
                return "/";
            else {
                String tmp = url.substring(0, end);
                return StringUtils.isBlank(tmp) ? "/" : tmp;
            }
        }
    }
}

Related Tutorials