• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > Java实现迅雷地址转成普通地址实例代码

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

作者:十年放牛 字体:[增加 减小] 来源:互联网 时间:2017-05-28

十年放牛 通过本文主要向大家介绍了java项目实例,java继承实例,java编程实例,让java实例自动结束,java开发实例100等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

原理分析:迅雷的thunder://地址就是将普通url地址加前缀‘AA'、后缀‘ZZ',再base64编码后得到的字符串

实现:

步骤1,添加工具类Base64 编码和解码:Base64.java

package th;
import java.io.*; 
/** 

 * Base64 编码和解码。 

 * 

 * @author 宋立君 

 * @date 2014年07月03日 

 */ 

public class Base64 { 
  public Base64() { 

  } 
  /** 

   * 功能:编码字符串 

   * 

   * @author 宋立君 

   * @date 2014年07月03日 

   * @param data 

   *   源字符串 

   * @return String 

   */ 

  public static String encode(String data) { 

    return new String(encode(data.getBytes())); 

  } 

  

  /** 

   * 功能:解码字符串 

   * 

   * @author 宋立君 

   * @date 2014年07月03日 

   * @param data 

   *   源字符串 

   * @return String 

   */ 

  public static String decode(String data) { 

    return new String(decode(data.toCharArray())); 

  } 

  

  /** 

   * 功能:编码byte[] 

   * 

   * @author 宋立君 

   * @date 2014年07月03日 

   * @param data 

   *   源 

   * @return char[] 

   */ 

  public static char[] encode(byte[] data) { 

    char[] out = new char[((data.length + 2) / 3) * 4]; 

    for (int i = 0, index = 0; i < data.length; i += 3, index += 4) { 

      boolean quad = false; 

      boolean trip = false; 

  

      int val = (0xFF & (int) data[i]); 

      val <<= 8; 

      if ((i + 1) < data.length) { 

        val |= (0xFF & (int) data[i + 1]); 

        trip = true; 

      } 

      val <<= 8; 

      if ((i + 2) < data.length) { 

        val |= (0xFF & (int) data[i + 2]); 

        quad = true; 

      } 

      out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)]; 

      val >>= 6; 

      out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)]; 

      val >>= 6; 

      out[index + 1] = alphabet[val & 0x3F]; 

      val >>= 6; 

      out[index + 0] = alphabet[val & 0x3F]; 

    } 

    return out; 

  } 

  

  /** 

   * 功能:解码 

   * 

   * @author 宋立君 

   * @date 2014年07月03日 

   * @param data 

   *   编码后的字符数组 

   * @return byte[] 

   */ 

  public static byte[] decode(char[] data) { 

  

    int tempLen = data.length; 

    for (int ix = 0; ix < data.length; ix++) { 

      if ((data[ix] > 255) || codes[data[ix]] < 0) { 

        --tempLen; // ignore non-valid chars and padding 

      } 

    } 

    // calculate required length: 

    // -- 3 bytes for every 4 valid base64 chars 

    // -- plus 2 bytes if there are 3 extra base64 chars, 

    // or plus 1 byte if there are 2 extra. 

  

    int len = (tempLen / 4) * 3; 

    if ((tempLen % 4) == 3) { 

      len += 2; 

    } 

    if ((tempLen % 4) == 2) { 

      len += 1; 

  

    } 

    byte[] out = new byte[len]; 

  

    int shift = 0; // # of excess bits stored in accum 

    int accum = 0; // excess bits 

    int index = 0; 

  

    // we now go through the entire array (NOT using the 'tempLen' value) 

    for (int ix = 0; ix < data.length; ix++) { 

      int value = (data[ix] > 255) ? -1 : codes[data[ix]]; 

  

      if (value >= 0) { // skip over non-code 

        accum <<= 6; // bits shift up by 6 each time thru 

        shift += 6; // loop, with new bits being put in 

        accum |= value; // at the bottom. 

        if (shift >= 8) { // whenever there are 8 or more shifted in, 

          shift -= 8; // write them out (from the top, leaving any 

          out[index++] = // excess at the bottom for next iteration. 

          (byte) ((accum >> shift) & 0xff); 

        } 

      } 

    } 

  

    // if there is STILL something wrong we just have to throw up now! 

    if (index != out.length) { 

      throw new Error("Miscalculated data length (wrote " + index 

          + " instead of " + out.length + ")"); 

    } 

  

    return out; 

  } 

  

  /** 

   * 功能:编码文件 

   * 

   * @author 宋立君 

   * @date 2014年07月03日 

   * @param file 

   *   源文件 

   */ 

  public static void encode(File file) throws IOException { 

    if (!file.exists()) { 

      System.exit(0); 

    } 

  

    else { 

      byte[] decoded = readBytes(file); 

      char[] encoded = encode(decoded); 

      writeChars(file, encoded); 

    } 

    file = null; 

  } 

  

  /** 

   * 功能:解码文件。 

   * 

   * @author 宋立君 

   * @date 2014年07月03日 

   * @param file 

   *   源文件 

   * @throws IOException 

   */ 

  public static void decode(File file) throws IOException { 

    if (!file.exists()) { 

      System.exit(0); 

    } else { 

      char[] encoded = readChars(file); 

      byte[] decoded = decode(encoded); 

      writeBytes(file, decoded); 

    } 

    file = null; 

  } 

  

  // 

  // code characters for values 0..63 

  // 

  private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" 

      .toCharArray(); 

  

  // 

  // lookup table for converting base64 characters to value in range 0..63 

  // 

  private static byte[] codes = new byte[256]; 

  static { 

    for (int i = 0; i < 256; i++) { 

      codes[i] = -1; 

      // LoggerUtil.debug(i + "&" + codes[i] + " "); 

    } 

    for (int i = 'A'; i <= 'Z'; i++) { 

      codes[i] = (byte) (i - 'A'); 

      // LoggerUtil.debug(i + "&" + codes[i] + " "); 

    } 

  

    for (int i = 'a'; i <= 'z'; i++) { 

      codes[i] = (byte) (26 + i - 'a'); 

      // LoggerUtil.debug(i + "&" + codes[i] + " "); 

    } 

    for (int i = '0'; i <= '9'; i++) { 

      codes[i] = (byte) (52 + i - '0'); 

      // LoggerUtil.debug(i + "&" + codes[i] + " "); 

    } 

    codes['+'] = 62; 

    codes['/'] = 63; 

  } 

  

  private static byte[] readBytes(File file) throws IOException { 

    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

    byte[] b = null; 

    InputStream fis = null; 

    InputStream is = null; 

    try { 

      fis = new FileInputStream(file); 

      is = new BufferedInputStream(fis); 

      int count = 0; 

      byte[] buf = new byte[16384]; 

      while ((count = is.read(buf)) != -1) { 

        if (count > 0) { 

          baos.write(buf, 0, count); 

        } 

      } 

      b = baos.toByteArray(); 

  

    } finally { 

      try { 

        if (fis != null) 

          fis.close(); 

        if (is != null) 

          is.close(); 

        if (baos != null) 

          baos.close(); 

      } catch (Exception e) { 

        System.out.println(e); 

      } 

    } 

  

    return b; 

  } 

  

  private static char[] readChars(File file) throws IOException { 

    CharArrayWriter caw = new CharArrayWriter(); 

    Reader fr = null; 

    Reader in = null; 

    try { 

      fr = new FileReader(file); 

      in = new BufferedReader(fr); 

      int count = 0; 

      char[] buf = new char[16384]; 

      while ((count = in.read(buf)) != -1) { 

        if (count > 0) { 

          caw.write(buf, 0, count); 

        } 

      } 

  

    } finally { 

      try { 

        if (caw != null) 

          caw.close(); 

        if (in != null) 

          in.close(); 

        if (fr != null) 

          fr.close(); 

      } catch (Exception e) { 

        System.out.println(e); 

      } 

    } 

  

    return caw.toCharArray(); 

  } 

  

  private static void writeBytes(File file, byte[] data) throws IOException { 





 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • java根据模板动态生成PDF实例
  • java 文件大数据Excel下载实例代码
  • JAVA读取PDF、WORD文档实例代码
  • Java追加文件内容的三种方法实例代码
  • Java从网络读取图片并保存至本地实例
  • java模拟微信抢红包的实例代码
  • Java实现迅雷地址转成普通地址实例代码
  • Java正则匹配中文的方法实例分析
  • java根据模板动态生成PDF实例
  • java 文件大数据Excel下载实例代码

相关文章

  • 2017-05-28基于Spring开发之自定义标签及其解析
  • 2017-05-28Java 中的CharArrayReader 介绍_动力节点Java学院整理
  • 2017-05-28深入理解Java嵌套类和内部类
  • 2017-05-28java 基础之JavaBean属性命名规范问题
  • 2017-05-28Java Set简介_动力节点Java学院整理
  • 2017-05-28Java Timezone类常见问题_动力节点Java学院整理
  • 2017-05-28Java Map 在put值时value值不被覆盖的解决办法
  • 2017-05-28根据list中对象的属性去重和排序小结(必看篇)
  • 2017-05-28Java 逻辑运算符中&&与&,||与|的区别
  • 2017-05-28详解Spring Boot中Controller用法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • window下安装和配置maven环境
    • SpringMVC实现文件的上传和下载实例代码
    • Java实现迅雷地址转成普通地址实例代码
    • Java基本数据类型与对应的包装类(动力节点java学院整理)
    • Java集合之HashMap用法详解
    • Java微信公众平台开发(9) 关键字回复以及客服接口实现
    • java反射遍历实体类属性和类型,并赋值和获取值的简单方法
    • 认证流程源码级详解
    • Spring boot 整合CXF开发web service示例
    • 详解利用Spring加载Properties配置文件

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有