集册 Java实例教程 从提供的片段构建一个完全限定的URL。

从提供的片段构建一个完全限定的URL。

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

541
根据提供的内容构建完全限定的URL。

/**

 * Copyright (c) 2000-2016 Liferay, Inc. All rights reserved.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *    http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

//package com.nowjava;/*from N o w J a v a . c o m - 时代Java*/


public class Main {

    /**

     * Build a fully qualified URL from the pieces provided. This method does not call any encoding methods, so the path

     * argument has to be properly encoded.

     *

     * @param   scheme  server protocol.

     * @param   host    server name.

     * @param   port    server port.

     * @param   path    properly encoded relative URL.

     *

     * @throws  IllegalArgumentException  if scheme, host or port is null.

     */

    public static String buildUrlAsString(String scheme, String host,

            int port, String path) {


        if ((scheme == null) || scheme.equals("") || (host == null)

                || host.equals("") || (port == 0)) {

            throw new IllegalArgumentException(

                    "Cannot build a URL using following scheme: " + scheme

                            + " host: " + host + " port: " + port

                            + " path: " + path);

        }//from n  o  w  j  a  v  a . c o m


        StringBuilder url = new StringBuilder(200);


        url.append(scheme).append("://").append(host);


        // check for protocol default port number

        if 
展开阅读全文