"; extractImgUrl(content); } /** * 提取图片链接地址 * * @param content 内容 */ public static List extractImgUrl(String content) { List srcList = new ArrayList(); //用来存储获取到的图片地址 String type[] = {"img", "video"};//这里因为只需要从网页获取视频url和图片url,所以只用匹配img和video标签,注意为了较小改动原博主的代码,这里一定要写小写 for (String string : type) { //以下主要参照csdn博主‘为你征伐’的方法,这里只是把正则规则变得灵活的去匹配type变量中的标签,已达到拿到图片和视频url的目的 Pattern p = Pattern.compile( "<(" + string + "|" + string.toUpperCase() + ")(.*?)(>|>" + string + ">|/>)");//匹配字符串中的img标签 Matcher matcher = p.matcher(content); boolean hasPic = matcher.find(); if (hasPic) {//判断是否含有图片 while (hasPic) {//如果含有图片,那么持续进行查找,直到匹配不到 String group = matcher.group(2);//获取第二个分组的内容,也就是 (.*?)匹配到的 Pattern srcText = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");//匹配图片的地址 Matcher matcher2 = srcText.matcher(group); if (matcher2.find()) { srcList.add(matcher2.group(3));//把获取到的图片地址添加到列表中 } hasPic = matcher.find();//判断是否还有img标签 } } } for (String itemUrl : srcList) { System.out.println("匹配到的内容:" + itemUrl); } return srcList; }}
package utils;import java.util.Random;/** * @author gaofeng */public class RandomUtils { /** * 生成范围内随机数(包含最大值,最小值) * * @param min 最小值 * @param max 最大值 * @return 随机数 */ public static int randInt(int min, int max) { // NOTE: Usually this should be a field rather than a method // variable so that it is not re-seeded every call. Random rand = new Random(); // nextInt is normally exclusive of the top value, // so add 1 to make it inclusive int randomNum = rand.nextInt((max - min) + 1) + min; return randomNum; }}
package utils;import java.io.UnsupportedEncodingException;public class StringUtil { /* * 字符转换为字节 */ private static byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } /* * 16进制字符串转字节数组 */ public static byte[] hexString2Bytes(String hex) { if ((hex == null) || (hex.equals(""))) { return null; } else if (hex.length() % 2 != 0) { return null; } else { hex = hex.toUpperCase(); int len = hex.length() / 2; byte[] b = new byte[len]; char[] hc = hex.toCharArray(); for (int i = 0; i < len; i++) { int p = 2 * i; b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p + 1])); } return b; } } /* * 字节数组转字符串 */ public static String bytes2String(byte[] b) throws Exception { String r = new String(b, "gb2312"); return r; } /** * 16进制直接转换成为字符串(无需Unicode解码) */ public static String hexStr2Str(String hexStr) throws UnsupportedEncodingException { String str = "0123456789ABCDEF"; char[] hexs = hexStr.toCharArray(); byte[] bytes = new byte[hexStr.length() / 2]; int n; for (int i = 0; i < bytes.length; i++) { n = str.indexOf(hexs[2 * i]) * 16; n += str.indexOf(hexs[2 * i + 1]); bytes[i] = (byte) (n & 0xff); } return new String(bytes, "gb2312"); } /** * 提取指定索引位置的单词 * * @param str 源字符串 * @param index 索引位置(提取第几个单词) * @return 指定索引位置的单词 */ public static String liftWord(String str, int index) { str = str.replaceAll(" +", " "); String[] strArr = str.split(" "); if (strArr.length <= index) { return "提取不到,超出索引界限"; } return strArr[index]; }}
package utils;import java.text.DecimalFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.Locale;import java.util.Map;public class DateUtil { /** yyyy-MM-dd */ public static final String REGEX_DATE = "yyyy-MM-dd"; /** kk:mm */ public static final String REGEX_TIME = "kk:mm"; /** yyyy-MM-dd HH:mm:ss */ public static final String REGEX_DATE_TIME = "yyyy-MM-dd kk:mm:ss"; /** yyyy年MM月dd日 */ public static final String REGEX_DATE_CHINESE = "yyyy年MM月dd日"; /** yyyy年MM月dd日 kk:mm */ public static final String REGEX_DATE_TIME_CHINESE = "yyyy年MM月dd日 kk:mm"; private static Map formatterMap; /** 获取当前日期或时间的字符串 */ public static String formatDate(String regex) { return getFormatter(regex).format(new Date()); } /** 获取指定日期或时间的字符串 */ public static String formatDate(String regex, Date date) { return getFormatter(regex).format(date); } /** 获取指定日期或时间的字符串 */ public static String formatDate(String regex, int year, int month, int day) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day); return getFormatter(regex).format(calendar.getTime()); } /** 获取指定日期或时间的字符串 */ public static String formatDate(String tagRegex, String srcRegex, String dateStr) { try { Date date = getFormatter(srcRegex).parse(dateStr); return getFormatter(tagRegex).format(date); } catch (ParseException e) { e.printStackTrace(); } return ""; } /** 获取指定日期或时间的Calendar对象 */ public static Calendar getCalendar(String regex, String date) { Calendar calendar = Calendar.getInstance(); try { calendar.setTime(getFormatter(regex).parse(date)); } catch (ParseException e) { e.printStackTrace(); } return calendar; } public static String formatTime(long time) { time = time / 1000; //如果时间小于60秒,则返回秒数 if (time / 60 == 0) { return time + "秒"; } //如果时间小于1小时,则返回分钟数 if (time / (60 * 60) == 0) { return (time / 60) + "分" + (time % 60) + "秒"; } //如果时间小于1天,则返回小时数 if (time / (60 * 60 * 24) == 0) { long hour = time / (60 * 60); time = time % (60 * 60); return hour + "小时" + (time / 60) + "分" + (time % 60) + "秒"; } long day = time / (60 * 60 * 24); time = time % (60 * 60 * 24); long hour = time / (60 * 60); time = time % (60 * 60); return day + "天" + hour + "小时" + (time / 60) + "分"; } public static String formatSendDate(String regex, String date) { //首先获取到传入的日期和当前日期 Calendar calendar = getCalendar(regex, date); Calendar current = Calendar.getInstance(); //首先判断是否为当天或者3天以内的邮件 int currentDay = current.get(Calendar.DAY_OF_YEAR); int calendarDay = calendar.get(Calendar.DAY_OF_YEAR); //如果是当天的邮件,则返回15:30这样格式的日期 if (currentDay == calendarDay) { return "今天 " + getFormatter(REGEX_TIME).format(calendar.getTime()); } //如果是昨天的邮件,则返回昨天 15:30这样格式的日期 if (currentDay - calendarDay == 1) { return "昨天 " + getFormatter(REGEX_TIME).format(calendar.getTime()); } //如果是前天的邮件,则返回前天 15:30这样格式的日期 if (currentDay - calendarDay == 2) { return "前天 " + getFormatter(REGEX_TIME).format(calendar.getTime()); } //然后判断是否为同一年 int currentYear = current.get(Calendar.YEAR); int calendarYear = calendar.get(Calendar.YEAR); //如果不是同一年,则返回年-月-日 时:分:秒这样格式的日期 if (currentYear != calendarYear) { return getFormatter("yyyy-MM-dd kk:mm").format(calendar.getTime()); } //否则返回1月1日 15:02这样格式的日期 return getFormatter("MM月dd日 kk:mm").format(calendar.getTime()); } private static SimpleDateFormat getFormatter(String regex) { if (formatterMap == null) { formatterMap = new HashMap<>(); } SimpleDateFormat formatter = formatterMap.get(regex); if (formatter == null) { formatter = new SimpleDateFormat(regex, Locale.getDefault()); formatterMap.put(regex, formatter); } return formatter; } public static String getHourStr(String timeStr) { String[] times = timeStr.split(":"); int hours = Integer.parseInt(times[0]) * 3600; int minutes = Integer.parseInt(times[1]) * 60; int seconds = Integer.parseInt(times[2]); double time = (hours + minutes + seconds) / 3600.0; DecimalFormat df = new DecimalFormat("#.#"); return df.format(time); } public static String getWeekStr(int year, int month, int day) { String[] arr = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day); return arr[calendar.get(Calendar.DAY_OF_WEEK) - 1]; } public static String formatCurrentDate(String regex) { return getFormatter(regex).format(new Date(System.currentTimeMillis())); } /** * @param regex 格式 * @param beginStr 开始时间 * @param endStr 结束时间 * @return 返回两时间的时间间隔(以天计算) */ public static long getDateDiffByDay(String regex, String beginStr, String endStr) { long day = 1; try { SimpleDateFormat format = getFormatter(regex); Date begin = format.parse(beginStr); Date end = format.parse(endStr); return (begin.getTime() - end.getTime()) / (1000 * 3600 * 24); } catch (ParseException e) { e.printStackTrace(); } return day; }}