Here you can find the source of buildURL(String spec, boolean trailingSlash)
Parameter | Description |
---|---|
spec | the URL specification |
trailingSlash | flag to indicate a trailing slash on the spec |
@SuppressWarnings("deprecation") public static URL buildURL(String spec, boolean trailingSlash)
//package com.java2s; /******************************************************************************* * Copyright (c) 2006, 2010 IBM Corporation 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 * //from w w w .ja v a 2 s.com * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ import java.io.File; import java.net.MalformedURLException; import java.net.URL; public class Main { /** * Builds a URL with the given specification * @param spec the URL specification * @param trailingSlash flag to indicate a trailing slash on the spec * @return a URL */ @SuppressWarnings("deprecation") public static URL buildURL(String spec, boolean trailingSlash) { if (spec == null) return null; boolean isFile = spec.startsWith("file:"); //$NON-NLS-1$ try { if (isFile) return adjustTrailingSlash(new File(spec.substring(5)).toURL(), trailingSlash); return new URL(spec); } catch (MalformedURLException e) { // if we failed and it is a file spec, there is nothing more we can do // otherwise, try to make the spec into a file URL. if (isFile) return null; try { return adjustTrailingSlash(new File(spec).toURL(), trailingSlash); } catch (MalformedURLException e1) { return null; } } } private static URL adjustTrailingSlash(URL url, boolean trailingSlash) throws MalformedURLException { String file = url.getFile(); if (trailingSlash == (file.endsWith("/"))) //$NON-NLS-1$ return url; file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1); //$NON-NLS-1$ return new URL(url.getProtocol(), url.getHost(), file); } }