|
@@ -0,0 +1,88 @@
|
|
|
+package com.zhentao.pojo;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.date.ChineseDate;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.lang.Console;
|
|
|
+import cn.hutool.core.util.ReUtil;
|
|
|
+import cn.hutool.crypto.digest.DigestUtil;
|
|
|
+import cn.hutool.extra.tokenizer.Result;
|
|
|
+import cn.hutool.extra.tokenizer.TokenizerEngine;
|
|
|
+import cn.hutool.extra.tokenizer.TokenizerUtil;
|
|
|
+import cn.hutool.extra.tokenizer.Word;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class CeShi {
|
|
|
+ // 生成 MD5 签名的方法
|
|
|
+ public static String generateMD5Signature(String appId, String goodsId, String reqId, String reqTime, String appKey) {
|
|
|
+ String input = appId + goodsId + reqId + reqTime + appKey;
|
|
|
+// try {
|
|
|
+// // 获取 MD5 算法的 MessageDigest 实例
|
|
|
+// MessageDigest md = MessageDigest.getInstance("MD5");
|
|
|
+// // 将输入字符串转换为字节数组并更新到 MessageDigest 中
|
|
|
+// byte[] messageDigest = md.digest(input.getBytes());
|
|
|
+// StringBuilder hexString = new StringBuilder();
|
|
|
+// for (byte b : messageDigest) {
|
|
|
+// String hex = Integer.toHexString(0xFF & b);
|
|
|
+// System.err.println(hex);
|
|
|
+// if (hex.length() == 1) {
|
|
|
+// hexString.append('0');
|
|
|
+// }
|
|
|
+// hexString.append(hex);
|
|
|
+// }
|
|
|
+// // 返回生成的 MD5 签名
|
|
|
+// return hexString.toString();
|
|
|
+// } catch (NoSuchAlgorithmException e) {
|
|
|
+// // 若 MD5 算法不可用,抛出运行时异常
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+ return DigestUtil.md5Hex(input);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证签名的方法
|
|
|
+ public static boolean verifySignature(String appId, String goodsId, String reqId, String reqTime, String appKey, String receivedSignature) {
|
|
|
+ String generatedSignature = generateMD5Signature(appId, goodsId, reqId, reqTime, appKey);
|
|
|
+ return generatedSignature.equals(receivedSignature);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+// // 示例数据
|
|
|
+// String appId = "123";
|
|
|
+// String goodsId = "456";
|
|
|
+// String reqId = "789";
|
|
|
+// String reqTime = "20250331120000";
|
|
|
+// String appKey = "yourAppKey";
|
|
|
+//
|
|
|
+// // 生成签名
|
|
|
+// String signature = generateMD5Signature(appId, goodsId, reqId, reqTime, appKey);
|
|
|
+// System.out.println("生成的签名: " + signature);
|
|
|
+// String s = "291b0ea36b97301bd20ed80ff624f24";
|
|
|
+//
|
|
|
+// // 验证签名
|
|
|
+// boolean isVerified = verifySignature(appId, goodsId, reqId, reqTime, appKey, s);
|
|
|
+// System.out.println("签名验证结果: " + (isVerified ? "通过" : "不通过"));
|
|
|
+// ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate("2020-03-31"));
|
|
|
+// String cyclicalYMD = chineseDate.getCyclicalYMD();
|
|
|
+// System.err.println(cyclicalYMD);
|
|
|
+// TokenizerEngine engine = TokenizerUtil.createEngine();
|
|
|
+///jar包
|
|
|
+////解析文本
|
|
|
+// String text = "这两个方法的区别在于返回值";
|
|
|
+// Result result = engine.parse("我吃苹果");
|
|
|
+////输出:这 两个 方法 的 区别 在于 返回 值
|
|
|
+// String resultStr = CollUtil.join((Iterator<Word>)result, " ");
|
|
|
+ String listContent = HttpUtil.get("https://www.oschina.net/action/ajax/get_more_news_list?newsType=&p=2");
|
|
|
+//使用正则获取所有标题
|
|
|
+ List<String> titles = ReUtil.findAll("<span class=\"text-ellipsis\">(.*?)</span>", listContent, 1);
|
|
|
+// List<String> titles = ReUtil.findAll("<span class=\"bdzm\">(.*?)</span>", listContent, 1);
|
|
|
+ for (String title : titles) {
|
|
|
+ //打印标题
|
|
|
+ Console.log(title);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|