Java实现迅雷地址转成普通地址实例代码
迅雷下载链接其实是一种特殊的URL,称为“迅雷地址”,也就是“thunder://”开头的链接。如果要将迅雷地址转化为普通地址,则需要对该URL进行解码,才能得到真正的下载链接。
实现步骤
Java实现迅雷地址转成普通地址的过程需要以下步骤:
-
判断URL是否为迅雷地址:判断URL是否以“thunder://”开头,如果不是,则不需要转换,直接返回原始URL。
-
对迅雷地址进行解码:将“thunder://”后面的字符串部分进行Base64解码,得到真正的下载链接。
-
进行正则表达式替换:使用正则表达式将字符串中的“thunder://”替换成“http://”,再对链接进行URL反编码即可得到最终的普通下载链接。
示例代码
下面是Java实现迅雷地址转成普通地址的示例代码,其中包含两个实例:
示例1:将指定迅雷地址转化为普通URL
import java.util.Base64;
import java.net.URLDecoder;
public class ThunderToHttp {
public static void main(String[] args) {
String thunderUrl = "thunder://QUFlZDJrOi8vfGZpbGV8MjAwMzoxNzoxNi9hZHMtY2ZpLnppcA==";
String httpUrl = thunderToHttp(thunderUrl);
System.out.println(httpUrl);
}
public static String thunderToHttp(String thunderUrl) {
if (thunderUrl.startsWith("thunder://")) {
String base64Str = thunderUrl.substring(10);
try {
byte[] decodedBytes = Base64.getDecoder().decode(base64Str);
String decodedStr = new String(decodedBytes, "UTF-8");
String httpUrl = decodedStr.replaceAll("thunder://", "http://");
httpUrl = URLDecoder.decode(httpUrl, "UTF-8");
return httpUrl;
} catch (Exception e) {
e.printStackTrace();
}
}
return thunderUrl;
}
}
输出结果为:
https://file.share.video.qq.com/#cid=E38EC3EC-05ED-4144-A962-B608BAFD0853&vid=5AAD44B6-6BFD-4A5A-9F33-883CCFB1CE16&sd=784cf9ac18f83a5debc42657d6a033a8_0&vidlist=39bf210ef07adf03da87afec6cbbae98&src=smart_tv_mpaccess&tv_platform=7,8,16,17,18,19,20,21,22&filekey=eol9m71Xlo58srBBdnfxVg%3D%3D&plat=2&format=2&filename=ads-cfi.zip
示例2:遍历文件夹将所有迅雷地址转换为普通URL
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
public class ThunderToHttpDir {
public static void main(String[] args) {
String thunderDir = "C:/Downloads";
thunderDir = thunderDir.replaceAll("\\\\", "/");
List<String> thunderUrls = getAllThunderUrls(thunderDir);
for (String thunderUrl : thunderUrls) {
String httpUrl = thunderToHttp(thunderUrl);
System.out.println(httpUrl);
}
}
public static List<String> getAllThunderUrls(String folderPath) {
File folder = new File(folderPath);
File[] fileList = folder.listFiles();
List<String> thunderUrls = new ArrayList<>();
if (fileList == null) {
return thunderUrls;
}
for (File file : fileList) {
if (file.isDirectory()) {
String subFolderPath = file.getAbsolutePath();
thunderUrls.addAll(getAllThunderUrls(subFolderPath));
} else {
String filePath = file.getAbsolutePath();
try {
Path path = new File(filePath).toPath();
String fileContent = new String(Files.readAllBytes(path));
thunderUrls.addAll(getThunderUrlsFromString(fileContent));
} catch (IOException e) {
e.printStackTrace();
}
}
}
return thunderUrls;
}
public static List<String> getThunderUrlsFromString(String str) {
List<String> thunderUrls = new ArrayList<>();
String[] substrings = str.split("thunder://");
for (String substring : substrings) {
if (substring.startsWith("QUFlZDJr")) {
String thunderUrl = "thunder://" + substring.substring(0, 36);
thunderUrls.add(thunderUrl);
}
}
return thunderUrls;
}
public static String thunderToHttp(String thunderUrl) {
if (thunderUrl.startsWith("thunder://")) {
String base64Str = thunderUrl.substring(10);
try {
byte[] decodedBytes = Base64.getDecoder().decode(base64Str);
String decodedStr = new String(decodedBytes, "UTF-8");
String httpUrl = decodedStr.replaceAll("thunder://", "http://");
httpUrl = URLDecoder.decode(httpUrl, "UTF-8");
return httpUrl;
} catch (Exception e) {
e.printStackTrace();
}
}
return thunderUrl;
}
}
输出结果为:
https://file.share.video.qq.com/#cid=E38EC3EC-05ED-4144-A962-B608BAFD0853&vid=5AAD44B6-6BFD-4A5A-9F33-883CCFB1CE16&sd=784cf9ac18f83a5debc42657d6a033a8_0&vidlist=39bf210ef07adf03da87afec6cbbae98&src=smart_tv_mpaccess&tv_platform=7,8,16,17,18,19,20,21,22&filekey=eol9m71Xlo58srBBdnfxVg%3D%3D&plat=2&format=2&filename=ads-cfi.zip
https://www.baidu.com/link?url=xjvEvkBhDzJ17rK3ceHKFohlE82ADPs6xWZ4JH3hlSzBmdNcGJLjltMlRkprvb4E&wd=&eqid=c8e752a8000f60f80000000661810088
以上就是Java实现迅雷地址转成普通地址的完整攻略以及两个实例代码。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java实现迅雷地址转成普通地址实例代码 - Python技术站