集册 Java实例教程 制作URI的相对副本。

制作URI的相对副本。

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

412
制作URI的相对副本。

/* Licensed Materials - Property of IBM                              */
/** from 
n o w j a   v  a . c o m - 时  代  Java**/

//package com.nowjava;

import java.net.URI;


public class Main {

    /**

     * Make a relative copy of a URI.

     * 

     * <p>If makeRelative is false, this may return the original instance.

     * That should be OK because a URI is immutable.

     * 

     * @param original

     * @param makeRelative

     * @return

     */

    public static URI copy(URI original, boolean makeRelative) {

        URI uri = original;


        if (uri.isAbsolute() && makeRelative) {

            String rel = uri.getRawPath();

            if (uri.getQuery() != null) {

                rel += "?" + uri.getRawQuery();

            }

            uri = URI.create(rel);// 来 自 时   代    Java - nowjava.com

        }


        return uri;

    }


    /**

     * Creates a new URI from a string.

     * 

     * <p>WARNING: If the input string is not a legal URI, this method

     * will throw an unchecked exception.

     * 

     * @param str

     * @param makeRelative

     * @return

     */

    public static URI create(
展开阅读全文