集册 Java实例教程 基于基转换为相对url

基于基转换为相对url

欢马劈雪     最近更新时间:2020-01-02 10:19:05

368
根据基准转换为相对网址
//时代Java - N o w  J a v a . c o m 提供

/*******************************************************************************

 * Copyright (c) 2006, 2010 Soyatec (http://www.soyatec.com) and others.       *

 * 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                                   *  

 * Contributors:                                                               *  

 *     Soyatec - initial API and implementation                                * 

 *******************************************************************************/

//package com.nowjava;


import java.util.StringTokenizer;


public class Main {

    public static final String FORWARD_SLASH = "/";

    public static final String RELATIVE_PATH_SIG = "../";


    /**

     * Convert to relative url based on base

     */

    public static String convertToRelative(String input, String base) {

        StringTokenizer inputTokenizer = new StringTokenizer(input,

                FORWARD_SLASH);

        StringTokenizer baseTokenizer = new StringTokenizer(base,/** from 时 代 J a v a 公 众 号 - N o w J a v  a . c o m**/

                FORWARD_SLASH);

        String token1 = "", token2 = "";

        //

        while (true) {

            if (!inputTokenizer.hasMoreTokens()

                    || !baseTokenizer.hasMoreTokens())

                break;

            token1 = baseTokenizer.nextToken();

            token2 = inputTokenizer.nextToken();

            if (!token1.equals(token2))

                break;

        }

        StringBuilder builder = new 
展开阅读全文