几个工具类 web

网友投稿 1026 2022-11-25

几个工具类 web

package utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** * @author gaofeng */public class CmdUtils { public static void main(String[] args) throws IOException, InterruptedException { // TODO Auto-generated method stub CmdUtils.runCMD("start } /** * 启动网页 * * @param htmlPath 网页地址(url) */ public static void startHtml(String htmlPath) { if ((htmlPath.startsWith("|| htmlPath.startsWith("w")) && (htmlPath.endsWith(".com") || htmlPath.endsWith(".com") || htmlPath.endsWith("-") || htmlPath.endsWith(".js") || htmlPath.endsWith(".jpg") || htmlPath .endsWith(".png"))) { try { runCMD("start " + htmlPath); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("htmlPath格式不对"); } } /** * @param cmd 执行命令 */ public static void runCMD(String cmd) { final String METHOD_NAME = "runCMD"; BufferedReader br = null; try { Process p = Runtime.getRuntime().exec("cmd.exe /C " + cmd);// Process p = Runtime.getRuntime().exec(cmd); // br = new BufferedReader(new InputStreamReader(p.getErrorStream())); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String readLine = br.readLine(); StringBuilder builder = new StringBuilder(); while (readLine != null) { readLine = br.readLine(); builder.append(readLine); } XLog.showLogInfo(METHOD_NAME + "#readLine: " + builder.toString()); p.waitFor(); int i = p.exitValue(); XLog.showLogInfo(METHOD_NAME + "#exitValue = " + i); } catch (Exception e) { XLog.showLogInfo(METHOD_NAME + "#ErrMsg=" + e.getMessage()); e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }}

package utils;import java.awt.Toolkit;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Random;public class DataUtiles { private static Random random = new Random(); /** * 返回手机号码 */ private static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153" .split(","); /** * 判断字符串是否有值,如果为null或者是空字符串或者只有空格或者为"null"字符串,则返回true,否则则返回false */ public static boolean isBlank(String value) { return value == null || "".equalsIgnoreCase(value.trim()) || "null" .equalsIgnoreCase(value.trim()); } /** * 将字符串复制到剪切板。 */ private static void setSysClipboardText(String writeMe) { Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard(); Transferable tText = new StringSelection(writeMe); clip.setContents(tText, null); } /** * 请求数据生成 */ public static void requestDataCreat(String methed, String arg) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append( "\n" + " user\n" + " 123\n" + " \n" + " \n" + " \n <"); stringBuffer.append(methed).append(" xmlns=\" creatArgs(arg, stringBuffer); stringBuffer.append(" \n \n") .append(""); DataUtiles.setSysClipboardText(stringBuffer.toString()); System.out.println("请求体已复制到剪切板!!!"); } private static void creatArgs(String arg, StringBuffer stringBuffer) { arg = arg.substring(1, arg.length() - 1).replace(" ", ""); String[] keyBody = arg.split(","); for (String keyAndValue : keyBody) { String key = keyAndValue.substring(0, keyAndValue.indexOf("=")); String val = keyAndValue.substring(keyAndValue.indexOf("=") + 1); stringBuffer.append(" <").append(key).append(">").append(val) .append("").append("\n"); } } public static String getRandomName() { Random random = new Random(); String[] Surname = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒", "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"}; String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 "; String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘"; int index = random.nextInt(Surname.length - 1); String name = Surname[index]; //获得一个随机的姓氏 int i = random.nextInt(3);//可以根据这个数设置产生的男女比例 if (i == 2) { int j = random.nextInt(girl.length() - 2); if (j % 2 == 0) { name = "" + name + girl.substring(j, j + 2); } else { name = "" + name + girl.substring(j, j + 1); } } else { int j = random.nextInt(girl.length() - 2); if (j % 2 == 0) { name = "" + name + boy.substring(j, j + 2); } else { name = "" + name + boy.substring(j, j + 1); } } return name; } /** * @param start 开始 * @param end 结束 * @return 获取范围内随机数字 */ public static int getRandomNum(int start, int end) { return (int) (Math.random() * (end - start + 1) + start); } /** * @return 获取随机手机号 */ public static String getRandomPhoneNumber() { int index = getRandomNum(0, telFirst.length - 1); String first = telFirst[index]; String second = String.valueOf(getRandomNum(1, 888) + 10000).substring(1); String third = String.valueOf(getRandomNum(1, 9100) + 10000).substring(1); return first + second + third; } /** * @return 获取随机姓名性别 */ public static String getNameAndSex() { Random random = new Random(); String[] Surname = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒", "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"}; String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 "; String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘"; int index = random.nextInt(Surname.length - 1); String name = Surname[index]; //获得一个随机的姓氏 int i = random.nextInt(3);//可以根据这个数设置产生的男女比例 if (i == 2) { int j = random.nextInt(girl.length() - 2); if (j % 2 == 0) { name = "女-" + name + girl.substring(j, j + 2); } else { name = "女-" + name + girl.substring(j, j + 1); } } else { int j = random.nextInt(girl.length() - 2); if (j % 2 == 0) { name = "男-" + name + boy.substring(j, j + 2); } else { name = "男-" + name + boy.substring(j, j + 1); } } return name; } /** * @return 随机性别 */ public static String getRandomSex() { return nextBoolean() ? "男" : "女"; } /** * @return 随机获取Boolean */ public static boolean nextBoolean() { return random.nextBoolean(); } /** * @return 返回true */ public static boolean reutrnTrue() { return true; } /** * @return 返回False */ public static boolean reutrnFalse() { return false; } /** * @return 获取随机ip */ public static String getRandomIp() { // ip范围 int[][] range = { {607649792, 608174079}, // 36.56.0.0-36.63.255.255 {1038614528, 1039007743}, // 61.232.0.0-61.237.255.255 {1783627776, 1784676351}, // 106.80.0.0-106.95.255.255 {2035023872, 2035154943}, // 121.76.0.0-121.77.255.255 {2078801920, 2079064063}, // 123.232.0.0-123.235.255.255 {-1950089216, -1948778497}, // 139.196.0.0-139.215.255.255 {-1425539072, -1425014785}, // 171.8.0.0-171.15.255.255 {-1236271104, -1235419137}, // 182.80.0.0-182.92.255.255 {-770113536, -768606209}, // 210.25.0.0-210.47.255.255 {-569376768, -564133889}, // 222.16.0.0-222.95.255.255 }; Random random = new Random(); int index = random.nextInt(10); String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0])); return ip; } /* * @return 将十进制转换成IP地址 */ public static String num2ip(int ip) { int[] b = new int[4]; String ipStr = ""; b[0] = (int) ((ip >> 24) & 0xff); b[1] = (int) ((ip >> 16) & 0xff); b[2] = (int) ((ip >> 8) & 0xff); b[3] = (int) (ip & 0xff); ipStr = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]); return ipStr; } /** * @return 获取随机地址 */ public static String getRandomAddress() { String[] province = {"河北省", "山西省", "辽宁省", "吉林省", "黑龙江省", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "海南省", "四川省", "贵州省", "云南省", "陕西省", "甘肃省", "青海省", "台湾省",}; String[] city = {"安康市", "安庆市", "安顺市", "安阳市", "鞍山市", "巴彦淖尔市", "巴中市", "白城市", "白山市", "白银市", "百色市", "蚌埠市", "包头市", "宝鸡市", "保定市", "保山市", "北海市", "本溪市", "滨州市", "沧州市", "昌都地区", "长春市", "长沙市", "长治市", "常德市", "常州市", "巢湖市", "朝阳市", "潮州市", "郴州市", "成都市", "承德市", "池州市", "赤峰市", "崇左市", "滁州市", "达州市", "大连市", "大庆市", "大同市", "丹东市", "德阳市", "德州市", "定西市", "东莞市", "东营市", "鄂尔多斯市", "鄂州市", "防城港市", "佛山市", "福州市", "抚顺市", "抚州市", "阜新市", "阜阳市", "甘南州", "赣州市", "固原市", "广安市", "广元市", "广州市", "贵港市", "贵阳市", "桂林市", "哈尔滨市", "哈密地区", "海北藏族自治州", "海东地区", "海口市", "邯郸市", "汉中市", "杭州市", "毫州市", "合肥市", "河池市", "河源市", "菏泽市", "贺州市", "鹤壁市", "鹤岗市", "黑河市", "衡水市", "衡阳市", "呼和浩特市", "呼伦贝尔市", "湖州市", "葫芦岛市", "怀化市", "淮安市", "淮北市", "淮南市", "黄冈市", "黄山市", "黄石市", "惠州市", "鸡西市", "吉安市", "吉林市", "济南市", "济宁市", "佳木斯市", "嘉兴市", "嘉峪关市", "江门市", "焦作市", "揭阳市", "金昌市", "金华市", "锦州市", "晋城市", "晋中市", "荆门市", "荆州市", "景德镇市", "九江市", "酒泉市", "开封市", "克拉玛依市", "昆明市", "拉萨市", "来宾市", "莱芜市", "兰州市", "廊坊市", "乐山市", "丽江市", "丽水市", "连云港市", "辽阳市", "辽源市", "聊城市", "临沧市", "临汾市", "临沂市", "柳州市", "六安市", "六盘水市", "龙岩市", "陇南市", "娄底市", "泸州市", "吕梁市", "洛阳市", "漯河市", "马鞍山市", "茂名市", "眉山市", "梅州市", "绵阳市", "牡丹江市", "内江市", "南昌市", "南充市", "南京市", "南宁市", "南平市", "南通市", "南阳市", "宁波市", "宁德市", "攀枝花市", "盘锦市", "平顶山市", "平凉市", "萍乡市", "莆田市", "濮阳市", "普洱市", "七台河市", "齐齐哈尔市", "钦州市", "秦皇岛市", "青岛市", "清远市", "庆阳市", "曲靖市", "衢州市", "泉州市", "日照市", "三门峡市", "三明市", "三亚市", "汕头市", "汕尾市", "商洛市", "商丘市", "上饶市", "韶关市", "邵阳市", "绍兴市", "深圳市", "沈阳市", "十堰市", "石家庄市", "石嘴山市", "双鸭山市", "朔州市", "四平市", "松原市", "苏州市", "宿迁市", "宿州市", "绥化市", "随州市", "遂宁市", "台州市", "太原市", "泰安市", "泰州市", "唐山市", "天水市", "铁岭市", "通化市", "通辽市", "铜川市", "铜陵市", "铜仁市", "吐鲁番地区", "威海市", "潍坊市", "渭南市", "温州市", "乌海市", "乌兰察布市", "乌鲁木齐市", "无锡市", "吴忠市", "芜湖市", "梧州市", "武汉市", "武威市", "西安市", "西宁市", "锡林郭勒盟", "厦门市", "咸宁市", "咸阳市", "湘潭市", "襄樊市", "孝感市", "忻州市", "新乡市", "新余市", "信阳市", "兴安盟", "邢台市", "徐州市", "许昌市", "宣城市", "雅安市", "烟台市", "延安市", "盐城市", "扬州市", "阳江市", "阳泉市", "伊春市", "伊犁哈萨克自治州", "宜宾市", "宜昌市", "宜春市", "益阳市", "银川市", "鹰潭市", "营口市", "永州市", "榆林市", "玉林市", "玉溪市", "岳阳市", "云浮市", "运城市", "枣庄市", "湛江市", "张家界市", "张家口市", "张掖市", "漳州市", "昭通市", "肇庆市", "镇江市", "郑州市", "中山市", "中卫市", "舟山市", "周口市", "株洲市", "珠海市", "驻马店市", "资阳市", "淄博市", "自贡市", "遵义市",}; String[] area = {"伊春区", "带岭区", "南岔区", "金山屯区", "西林区", "美溪区", "乌马河区", "翠峦区", "友好区", "新青区", "上甘岭区", "五营区", "红星区", "汤旺河区", "乌伊岭区", "榆次区"}; String[] road = {"爱国路", "安边路", "安波路", "安德路", "安汾路", "安福路", "安国路", "安化路", "安澜路", "安龙路", "安仁路", "安顺路", "安亭路", "安图路", "安业路", "安义路", "安远路", "鞍山路", "鞍山支路", "澳门路", "八一路", "巴林路", "白城路", "白城南路", "白渡路", "白渡桥", "白兰路", "白水路", "白玉路", "百安路(方泰镇)", "百官街", "百花街", "百色路", "板泉路", "半淞园路", "包头路", "包头南路", "宝安公路", "宝安路", "宝昌路", "宝联路", "宝林路", "宝祁路", "宝山路", "宝通路", "宝杨路", "宝源路", "保德路", "保定路", "保屯路", "保屯路", "北艾路",}; String[] home = {"金色家园", "耀江花园", "阳光翠竹苑", "东新大厦", "溢盈河畔别墅", "真新六街坊", "和亭佳苑", "协通公寓", "博泰新苑", "菊园五街坊", "住友嘉馨名园", "复华城市花园", "爱里舍花园"}; Random random = new Random(); int randomProvinceNum = random.nextInt(province.length); int randomCityNum = random.nextInt(city.length); int randomAreaNum = random.nextInt(area.length); int randomRoadNum = random.nextInt(road.length); int randomHomeNum = random.nextInt(home.length); int num = random.nextInt(200); return province[randomProvinceNum] + city[randomAreaNum] + area[randomAreaNum] + road[randomRoadNum] + num + "号" + home[randomHomeNum]; } // 18位身份证号码各位的含义: // 1-2位省、自治区、直辖市代码; // 3-4位地级市、盟、自治州代码; // 5-6位县、县级市、区代码; // 7-14位出生年月日,比如19670401代表1967年4月1日; // 15-17位为顺序号,其中17位(倒数第二位)男为单数,女为双数; // 18位为校验码,0-9和X。 // 作为尾号的校验码,是由把前十七位数字带入统一的公式计算出来的, // 计算的结果是0-10,如果某人的尾号是0-9,都不会出现X,但如果尾号是10,那么就得用X来代替, // 因为如果用10做尾号,那么此人的身份证就变成了19位。X是罗马数字的10,用X来代替10 public static String getRandomIdCard() { String id = ""; // 随机生成省、自治区、直辖市代码 1-2 String provinces[] = {"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82"}; String province = provinces[new Random().nextInt(provinces.length - 1)]; // 随机生成地级市、盟、自治州代码 3-4 String citys[] = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "21", "22", "23", "24", "25", "26", "27", "28"}; String city = citys[new Random().nextInt(citys.length - 1)]; // 随机生成县、县级市、区代码 5-6 String countys[] = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38"}; String county = countys[new Random().nextInt(countys.length - 1)]; // 随机生成出生年月 7-14 SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd"); Date beginDate = new Date(); Calendar date = Calendar.getInstance(); date.setTime(beginDate); date.set(Calendar.DATE, date.get(Calendar.DATE) - new Random().nextInt(365 * 100)); String birth = dft.format(date.getTime()); // 随机生成顺序号 15-17 String no = new Random().nextInt(999) + ""; // 随机生成校验码 18 String checks[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "X"}; String check = checks[new Random().nextInt(checks.length - 1)]; // 拼接身份证号码 id = province + city + county + birth + no + check; return id; } /** * 生成随机时间 * @return */ public static String getRandomDate(){ return getRandomDate("2010-09-20", "2020-09-22"); } /** * 生成随机时间 * * @param beginDate * @param endDate * @return */ public static String getRandomDate(String beginDate, String endDate) { try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date start = format.parse(beginDate);// 构造开始日期 Date end = format.parse(endDate);// 构造结束日期 // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。 if (start.getTime() >= end.getTime()) { return null; } long date = random(start.getTime(), end.getTime()); return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date); } catch (Exception e) { e.printStackTrace(); } return null; } private static long random(long begin, long end) { long rtn = begin + (long) (Math.random() * (end - begin)); // 如果返回的是开始时间和结束时间,则递归调用本函数查找随机值 if (rtn == begin || rtn == end) { return random(begin, end); } return rtn; }}

package utils;import java.io.UnsupportedEncodingException;/** * 字符串进制转换 */public class HexStringUtils { private static final char[] DIGITS_HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; protected static char[] encodeHex(byte[] data) { int l = data.length; char[] out = new char[l << 1]; for (int i = 0, j = 0; i < l; i++) { out[j++] = DIGITS_HEX[(0xF0 & data[i]) >>> 4]; out[j++] = DIGITS_HEX[0x0F & data[i]]; } return out; } protected static byte[] decodeHex(char[] data) { int len = data.length; if ((len & 0x01) != 0) { throw new RuntimeException("字符个数应该为偶数"); } byte[] out = new byte[len >> 1]; for (int i = 0, j = 0; j < len; i++) { int f = toDigit(data[j], j) << 4; j++; f |= toDigit(data[j], j); j++; out[i] = (byte) (f & 0xFF); } return out; } protected static int toDigit(char ch, int index) { int digit = Character.digit(ch, 16); if (digit == -1) { throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index); } return digit; } public static String toHex(String str) { return new String(encodeHex(str.getBytes())); } public static String fromHex(String hex) throws UnsupportedEncodingException { return new String(decodeHex(hex.toCharArray()),"gb2312"); } public static void main(String[] args) { String s = "abc你好"; String hex = toHex(s); String decode = null; try { decode = fromHex(hex); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println("原字符串:" + s); System.out.println("十六进制字符串:" + hex); System.out.println("还原:" + decode); }}

package utils;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * @author gaofeng */public class HtmlUtil { public static void main(String[] args) { String content = ""; 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() + ")(.*?)(>|>|/>)");//匹配字符串中的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; }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:AjaxResult 响应值
下一篇:判断有没有联网
相关文章

 发表评论

暂时没有评论,来抢沙发吧~