Java tutorial
/* * Copyright (C) 2008 feilong * * 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.feilong.core.net; import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * {@link URL} . * * <h3>URL ?</h3> * * <blockquote> * * <p> * URL W3C HTTP ?? ?; ?,??? Web ???: * </p> * * <ul> * <li><b>IE</b> ? <b>2083</b> ,? <b>2048</b> .</li> * <li><b>Firefox</b> ?? <b>65536</b> ???.</li> * <li><b>Safari</b> <b>80000</b> .</li> * <li><b>Opera</b> <b>190000</b> .</li> * </ul> * * <p> * Web ?: * </p> * * <ul> * <li><b>Apache Web</b> ? <b>4000</b> URL <b>"413 Entity Too Large"</b> .</li> * <li><b>IIS</b> URL <b>16384</b> .</li> * </ul> * </blockquote> * * @author <a href="http://feitianbenyue.iteye.com/">feilong</a> * @since 1.2.1 */ public final class URLUtil { /** The Constant log. */ private static final Logger LOGGER = LoggerFactory.getLogger(URLUtil.class); /** Don't let anyone instantiate this class. */ private URLUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } /** * ?url. * * <h3>:</h3> * <blockquote> * <ol> * <li>? check exception,? uncheck exception</li> * <li>new URL ,? {@link #toFileURL(String)}</li> * </ol> * </blockquote> * * <h3>?:</h3> * * <blockquote> * * <p> * {@link URL#URL(String)} ,, * </p> * * <pre class="code"> * * String spec = "C:/Users/feilong/feilong/train//warmReminder/20160704141057.html"; * * URL newURL = new URL(spec); * </pre> * * , * * <pre class="code"> * Caused by: java.net.MalformedURLException: <span style="color:red"><b>unknown protocol: c</b></span> * at java.net.URL.{@code <init>}(URL.java:593) * at java.net.URL.{@code <init>}(URL.java:483) * at java.net.URL.{@code <init>}(URL.java:432) * ... 24 more * </pre> * * ,?? * * <pre class="code"> * * String spec = "file://C:/Users/feilong/feilong/train//warmReminder/20160704141057.html"; * * URL newURL = new URL(spec); * </pre> * * ,???? (file URI scheme),?? url?, {@link #toFileURL(String)} * </blockquote> * * @param spec * the <code>String</code> to parse as a URL. * @return <code>spec</code> null, {@link NullPointerException}<br> * <code>spec</code> blank, {@link IllegalArgumentException}<br> * @see java.net.URL#URL(String) * @see "org.apache.cxf.common.util.StringUtils#getURL(String)" * @see "org.springframework.util.ResourceUtils#getURL(String)" * @see <a href="https://en.wikipedia.org/wiki/File_URI_scheme">File_URI_scheme</a> * @see "org.apache.xml.resolver.readers.TextCatalogReader#readCatalog(Catalog, String)" * @see <a href="https://docs.oracle.com/javase/tutorial/networking/urls/creatingUrls.html">Creating a URL</a> * @since 1.8.0 */ public static URL toURL(String spec) { Validate.notBlank(spec, "spec can't be blank!"); try { return new URL(spec); } catch (MalformedURLException e) { // no URL -> treat as file path LOGGER.info("[new URL(\"{}\")] exception,cause by :[{}],will try call [toFileURL(\"{}\")]", spec, e.getMessage(), spec); return toFileURL(spec); } } /** * <code>filePath</code> ?{@link URL}. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * * String spec = "C:\\Users\\feilong\\feilong\\train\\\\warmReminder\\20160704141057.html"; * * URL newURL = toFileURL(spec); * </pre> * * </blockquote> * * <h3>Using Java 7 ?:</h3> * * <blockquote> * Paths#get(string).toUri().toURL(); * * <p> * However, you probably want to get a URI.<br> * Eg, a URI begins with file:/// but a URL with file:/ (at least, that's what toString produces). * </p> * * ?? * <a href="http://stackoverflow.com/questions/6098472/pass-a-local-file-in-to-url-in-java#21461738">pass-a-local-file-in-to-url-in-java * </a> * </blockquote> * * @param filePath * * @return the url * @see java.io.File#toURI() * @see java.net.URI#toURL() * @see "org.apache.commons.io.FileUtils#toURLs(File[])" * @since 1.8.0 change Access Modifiers */ private static URL toFileURL(String filePath) { try { return new File(filePath).toURI().toURL();// file.toURL() ?,? URL ? } catch (MalformedURLException e) { throw new URIParseException("filePath:" + filePath, e); } } //****************************************************************************************************** /** * {@link URL}? {@link URI}. * * @param url * the url * @return <code>url</code> null, {@link NullPointerException}<br> * @see "org.springframework.util.ResourceUtils#toURI(URL)" * @see java.net.URL#toURI() * @since 1.2.2 */ public static URI toURI(URL url) { Validate.notNull(url, "url can't be null!"); try { return url.toURI(); } catch (URISyntaxException e) { throw new URIParseException(e); } } //****************************************************************************************************** /** * ???url, spec ? URL, URL URL spec ?. * * <p style="color:red"> * ?,method * </p> * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * * URL url = new URL("http://www.exiaoshuo.com/jinyiyexing/"); * URIUtil.getUnionUrl(url, "/jinyiyexing/1173348/") = http://www.exiaoshuo.com/jinyiyexing/1173348/ * </pre> * * </blockquote> * * @param context * ?? * @param spec * the <code>String</code> to parse as a URL. * @return <code>spec</code> null, {@link NullPointerException}<br> * <code>spec</code> blank, {@link IllegalArgumentException}<br> * @since 1.4.0 */ public static String getUnionUrl(URL context, String spec) { Validate.notBlank(spec, "spec can't be null!"); try { return new URL(context, spec).toString(); } catch (MalformedURLException e) { throw new URIParseException(e); } } }