/* * Copyright 2017 GcsSloop * * 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. * * Last modified 2017-03-16 00:27:18 * * GitHub: https://github.com/GcsSloop * Website: http://www.gcssloop.com * Weibo: http://weibo.com/GcsSloop */ package com.gcssloop.diycode.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * url 相关工具 */ public class UrlUtil { public static boolean isUrlPrefix(String url) { return url.startsWith("http://") || url.startsWith("https://"); } /** * 判断后缀是不是图片类型的 * * @param url url */ public static boolean isImageSuffix(String url) { return url.endsWith(".png") || url.endsWith(".PNG") || url.endsWith(".jpg") || url.endsWith(".JPG") || url.endsWith(".jpeg") || url.endsWith(".JPEG"); } /** * 判断后缀是不是 GIF * * @param url url */ public static boolean isGifSuffix(String url) { return url.endsWith(".gif") || url.endsWith(".GIF"); } /** * 获取后缀名 */ public static String getSuffix(String url) { if ((url != null) && (url.length() > 0)) { int dot = url.lastIndexOf('.'); if ((dot > -1) && (dot < (url.length() - 1))) { return url.substring(dot + 1); } } return url; } /** * 获取 mimeType */ public static String getMimeType(String url) { if (url.endsWith(".png") || url.endsWith(".PNG")) { return "data:image/png;base64,"; } else if (url.endsWith(".jpg") || url.endsWith(".jpeg") || url.endsWith(".JPG") || url.endsWith(".JPEG")) { return "data:image/jpg;base64,"; } else if (url.endsWith(".gif") || url.endsWith(".GIF")) { return "data:image/gif;base64,"; } else { return ""; } } /** * 根据 url 获取 host name * http://www.gcssloop.com/ => www.gcssloop.com */ public static String getHost(String url) { if (url == null || url.trim().equals("")) { return ""; } String host = ""; Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+"); Matcher matcher = p.matcher(url); if (matcher.find()) { host = matcher.group(); } return host; } }