Java实现迅雷地址转成普通地址实例代码

Java实现迅雷地址转成普通地址实例代码

迅雷下载链接其实是一种特殊的URL,称为“迅雷地址”,也就是“thunder://”开头的链接。如果要将迅雷地址转化为普通地址,则需要对该URL进行解码,才能得到真正的下载链接。

实现步骤

Java实现迅雷地址转成普通地址的过程需要以下步骤:

  1. 判断URL是否为迅雷地址:判断URL是否以“thunder://”开头,如果不是,则不需要转换,直接返回原始URL。

  2. 对迅雷地址进行解码:将“thunder://”后面的字符串部分进行Base64解码,得到真正的下载链接。

  3. 进行正则表达式替换:使用正则表达式将字符串中的“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技术站

(0)
上一篇 2023年5月20日
下一篇 2023年5月20日

相关文章

  • java实现动态编译并动态加载

    Java实现动态编译并动态加载是一种非常强大和灵活的技术。本篇文章将介绍如何实现Java的动态编译和加载,并给出两个示例说明。 动态编译的实现 Java中的动态编译是通过使用Java提供的Compiler API来实现的。在Java中,编译器可以将Java源代码编译成字节码,这些字节码可以直接在Java虚拟机上运行。下面是一些使用Java Compiler …

    Java 2023年5月26日
    00
  • Java方法的可变参数类型实例分析

    首先我们需要了解什么是Java方法的可变参数类型。 Java方法的可变参数类型 Java方法的可变参数类型指的是:在方法的参数列表中,允许最后一个参数为可变参数,即不确定数量的参数。在方法中,可变参数被声明为数组类型,在调用方法时可以传入任意数量的参数。 可变参数的语法为: 修饰符 返回值类型 方法名(参数类型… 参数名) { // 方法体 } 其中的参…

    Java 2023年5月26日
    00
  • Spring Boot中的Properties的使用详解

    SpringBoot中的Properties的使用详解 什么是Properties Properties是Java中处理属性文件的一个类。在SpringBoot中,我们可以使用application.properties或application.yml作为配置文件,来替代传统的XML配置文件,来配置服务器的相关信息。 application.properti…

    Java 2023年5月15日
    00
  • Spring Boot应用程序中如何使用Keycloak详解

    Spring Boot应用程序中如何使用Keycloak详解 Keycloak是一个强大的、开源、易于使用的认证和授权管理解决方案。Spring Boot提供了与Keycloak的集成,可以轻松地保护和管理您的应用程序。 本文将介绍如何在Spring Boot应用程序中快速集成Keycloak,以便您的Web应用程序能够以安全的方式使用它。 准备工作 在开始…

    Java 2023年5月20日
    00
  • SpringBoot 使用 Sa-Token 完成权限认证

    一、设计思路 所谓权限认证,核心逻辑就是判断一个账号是否拥有指定权限: 有,就让你通过。 没有?那么禁止访问! 深入到底层数据中,就是每个账号都会拥有一个权限码集合,框架来校验这个集合中是否包含指定的权限码。 例如:当前账号拥有权限码集合 [“user-add”, “user-delete”, “user-get”],这时候我来校验权限 “user-upda…

    Java 2023年4月25日
    00
  • Spring AOP核心功能示例代码详解

    关于《Spring AOP核心功能示例代码详解》的攻略,我会从以下三个方面详细讲解。 一、背景介绍 Spring AOP是Spring框架的一个核心组件,它提供了一种在方法调用时动态地将代码织入到原始方法体中的能力,从而可在保持应用程序开发简单性的前提下,实现横切关注点的模块化复用。 在学习Spring AOP的过程中,我们需要了解一些基本概念,例如: 连接…

    Java 2023年5月19日
    00
  • java 将字符串、list 写入到文件,并读取内容的案例

    一、将字符串写入文件并读取内容的案例 将字符串写入文件 import java.io.File; import java.io.FileWriter; import java.io.IOException; public class StringToFileExample { public static void main(String[] args) { …

    Java 2023年5月19日
    00
  • Eclipse最新版使用过程中遇到的问题总结

    Eclipse最新版使用过程中遇到的问题总结 作为一款强大的Java开发工具,Eclipse在开发中的使用率非常高。然而,在使用过程中可能会遇到一些问题,需要进行解决。本文总结了Eclipse最新版使用过程中可能遇到的问题及其解决方法,方便开发者在使用过程中进行参考。 问题一:Eclipse启动缓慢 在启动Eclipse时,会花费较长时间进行加载,影响开发效…

    Java 2023年5月19日
    00
合作推广
合作推广
分享本页
返回顶部