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.sunchenbin.store.feilong.core.net; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sunchenbin.store.feilong.core.io.IOWriteUtil; import com.sunchenbin.store.feilong.core.util.Validator; /** * The Class URLUtil. * * <h3>URL ?</h3> * * <blockquote> * * <p> * URL W3C HTTP ?? ?,,,?,??? Web ??? * </p> * * <ul> * <li>IE URL ? 2083 ,? 2048 </li> * <li>Firefox ??? 65536 ???</li> * <li>Safari ? 80000 </li> * <li>Opera ? 190000 ,</li> * </ul> * * Web ? * <ul> * <li>Apache Web ? 4000 URL 413 Entity Too Large" </li> * <li>IIS URL 16384 </li> * </ul> * </blockquote> * * @author feilong * @version 1.2.1 2015621 ?12:54:15 * @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!"); } /** * . * * <p> * ??? . * </p> * * @param urlString * ?<br> * url ?? * @param directoryName * * @throws IOException * the IO exception * @see IOWriteUtil#write(InputStream, String, String) * * @see org.apache.commons.io.FileUtils#copyURLToFile(URL, File) * @see org.apache.commons.io.FileUtils#copyURLToFile(URL, File, int, int) * */ public static void download(String urlString, String directoryName) throws IOException { if (Validator.isNullOrEmpty(urlString)) { throw new NullPointerException("urlString can't be null/empty!"); } if (Validator.isNullOrEmpty(directoryName)) { throw new NullPointerException("directoryName can't be null/empty!"); } LOGGER.info("begin download,urlString:[{}],directoryName:[{}]", urlString, directoryName); URL url = URLUtil.newURL(urlString); InputStream inputStream = url.openStream(); File file = new File(urlString); String fileName = file.getName(); IOWriteUtil.write(inputStream, directoryName, fileName); LOGGER.info("end download,url:[{}],directoryName:[{}]", urlString, directoryName); } /** * To string array. * * @param urls * the urls * @return the string[] * @since 1.2.1 */ public static String[] toStringArray(URL[] urls) { if (Validator.isNullOrEmpty(urls)) { throw new NullPointerException("urls can't be null/empty!"); } String[] stringArray = new String[urls.length]; int i = 0; for (URL url : urls) { stringArray[i] = url.toString(); i++; } return stringArray; } /** * {@link URL}? {@link URI}. * * @param url * the url * @return the uri * @see "org.springframework.util.ResourceUtils#toURI(URL)" * @since 1.2.2 */ public static URI toURI(URL url) { try { return url.toURI(); } catch (URISyntaxException e) { throw new URIParseException(e); } } /** * New url. * * @param spec * the <code>String</code> to parse as a URL. * @return the url * @since 1.3.0 */ public static URL newURL(String spec) { try { return new URL(spec); } catch (MalformedURLException e) { LOGGER.error("MalformedURLException:", e); throw new URIParseException(e); } } /** * ???url, spec? URL. URL URL spec?. * * <p style="color:red"> * ?,{@link #getUnionUrl(URL, String)} * </p> * * <p> * : URIUtil.getUnionUrl("E:\\test", "sanguo")-------------{@code >}file:/E:/test/sanguo * </p> * * @param context * ?? * @param spec * the <code>String</code> to parse as a URL. * @return ???url * @see #getFileURL(String) * @see #getUnionUrl(URL, String) * @since 1.4.0 */ public static String getUnionFileUrl(String context, String spec) { URL parentUrl = getFileURL(context); return getUnionUrl(parentUrl, spec); } /** * ???url, spec ? URL. URL URL spec ?. * * <p style="color:red"> * ?,method * </p> * * <pre> * {@code * : * * URIUtil.getUnionUrl("E:\\test", "sanguo")------------->file:/E:/test/sanguo * * URL url = new URL("http://www.exiaoshuo.com/jinyiyexing/"); * result = URIUtil.getUnionUrl(url, "/jinyiyexing/1173348/"); * http://www.exiaoshuo.com/jinyiyexing/1173348/ * } * </pre> * * @param context * ?? * @param spec * the <code>String</code> to parse as a URL. * @return ???url * @since 1.4.0 */ public static String getUnionUrl(URL context, String spec) { try { URL unionUrl = new URL(context, spec); return unionUrl.toString(); } catch (MalformedURLException e) { LOGGER.error("MalformedURLException:", e); throw new URIParseException(e); } } /** * ?url. * * @param filePathName * * @return url * @see java.io.File#toURI() * @see java.net.URI#toURL() * @since 1.4.0 */ public static URL getFileURL(String filePathName) { if (Validator.isNullOrEmpty(filePathName)) { throw new NullPointerException("filePathName can't be null/empty!"); } File file = new File(filePathName); try { // file.toURL() ?,? URL ? return file.toURI().toURL(); } catch (MalformedURLException e) { LOGGER.error("MalformedURLException:", e); throw new URIParseException(e); } } }