首先torrent里面肯定携带的有一些信息,所以就需要我们来解析这些信息。

我们这里做多文件制作torrent,所以首先要针对每一个文件建一个实体类

  1. import java.util.List;
  2. public class Info {
  3. private String name;
  4. private byte[] pieces;
  5. private long piecesLength;
  6. private long length;
  7. private String md5sum;
  8. private List<Files> files;
  9. public Info() {
  10. }
  11. public Info(String name, byte[] pieces, long piecesLength, long length, String md5sum, List<Files> files) {
  12. super();
  13. this.name = name;
  14. this.pieces = pieces;
  15. this.piecesLength = piecesLength;
  16. this.length = length;
  17. this.md5sum = md5sum;
  18. this.files = files;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public byte[] getPieces() {
  27. return pieces;
  28. }
  29. public void setPieces(byte[] pieces) {
  30. this.pieces = pieces;
  31. }
  32. public long getPiecesLength() {
  33. return piecesLength;
  34. }
  35. public void setPiecesLength(long piecesLength) {
  36. this.piecesLength = piecesLength;
  37. }
  38. public long getLength() {
  39. return length;
  40. }
  41. public void setLength(long length) {
  42. this.length = length;
  43. }
  44. public String getMd5sum() {
  45. return md5sum;
  46. }
  47. public void setMd5sum(String md5sum) {
  48. this.md5sum = md5sum;
  49. }
  50. public List<Files> getFiles() {
  51. return files;
  52. }
  53. public void setFiles(List<Files> files) {
  54. this.files = files;
  55. }

而对于每一个File,又存在了一些信息,所以我们针对File建立一个实体类

  1. import java.util.List;
  2. public class Files {
  3. private long length;
  4. private String md5sum;
  5. private List<String> path;
  6. public Files() {
  7. }
  8. //getter and setter  and tostring
  9. public long getLength() {
  10. return length;
  11. }
  12. public Files(long length, String md5sum, List<String> path) {
  13. super();
  14. this.length = length;
  15. this.md5sum = md5sum;
  16. this.path = path;
  17. }
  18. public void setLength(long length) {
  19. this.length = length;
  20. }
  21. public String getMd5sum() {
  22. return md5sum;
  23. }
  24. public void setMd5sum(String md5sum) {
  25. this.md5sum = md5sum;
  26. }
  27. public List<String> getPath() {
  28. return path;
  29. }
  30. public void setPath(List<String> path) {
  31. this.path = path;
  32. }
  33. }

而我们在制作torrent文件时,填写了很多信息,比如要web seeds等等。所以此时也需要一个实体类

  1. import java.util.Arrays;
  2. import java.util.List;
  3. import org.jeecgframework.core.util.StringUtil;
  4. public class BitTorrentInfo {
  5. public static List<String> keyList;
  6. static{
  7. String[] keys = {"announce", "announce-list", "creation date", "comment", "created by",
  8. "info", "length", "md5sum", "name", "piece length","pieces", "files", "path"};
  9. keyList = Arrays.asList(keys);
  10. }
  11. private String announce;
  12. private List<String> announceList;
  13. private long creationDate;
  14. private String comment;
  15. private String createBy;
  16. private Info info;
  17. public BitTorrentInfo() {
  18. }
  19. //getter and setter  and tostring
  20. public BitTorrentInfo(String announce, List<String> announceList, long creationDate, String comment,
  21. String createBy, Info info) {
  22. super();
  23. this.announce = announce;
  24. this.announceList = announceList;
  25. this.creationDate = creationDate;
  26. this.comment = comment;
  27. this.createBy = createBy;
  28. this.info = info;
  29. }
  30. public static List<String> getKeyList() {
  31. return keyList;
  32. }
  33. public static void setKeyList(List<String> keyList) {
  34. BitTorrentInfo.keyList = keyList;
  35. }
  36. public String getAnnounce() {
  37. return announce;
  38. }
  39. public void setAnnounce(String announce) {
  40. this.announce = announce;
  41. }
  42. public List<String> getAnnounceList() {
  43. return announceList;
  44. }
  45. public void setAnnounceList(List<String> announceList) {
  46. this.announceList = announceList;
  47. }
  48. public long getCreationDate() {
  49. return creationDate;
  50. }
  51. public void setCreationDate(long creationDate) {
  52. this.creationDate = creationDate;
  53. }
  54. public String getComment() {
  55. return comment;
  56. }
  57. public void setComment(String comment) {
  58. this.comment = comment;
  59. }
  60. public String getCreateBy() {
  61. return createBy;
  62. }
  63. public void setCreateBy(String createBy) {
  64. this.createBy = createBy;
  65. }
  66. public Info getInfo() {
  67. return info;
  68. }
  69. public void setInfo(Info info) {
  70. this.info = info;
  71. }
  72. public void setValue(String key, Object value) throws Exception {
  73. if(!keyList.contains(key)){
  74. throw new Exception("not contains this key: " + key);
  75. }else{
  76. switch (key){
  77. case "announce":this.setAnnounce(value.toString());break;
  78. case "announce-list":this.getAnnounceList().add(value.toString());break;
  79. case "creation date":
  80. if(StringUtil.isNumeric(value.toString())){
  81. this.setCreationDate(Long.parseLong(value.toString()));
  82. }else{
  83. this.setCreationDate(0);
  84. }
  85. break;
  86. case "comment":this.setComment(value.toString());break;
  87. case "created by":this.setCreateBy(value.toString());break;
  88. case "length":
  89. List<Files> filesList1 = this.getInfo().getFiles();
  90. if(filesList1 != null){
  91. Files files = this.getInfo().getFiles().get(filesList1.size()-1);
  92. files.setLength(Long.parseLong(value.toString()));
  93. }else {
  94. this.getInfo().setLength(Long.parseLong(value.toString()));
  95. }
  96. break;
  97. case "md5sum":
  98. List<Files> filesList2 = this.getInfo().getFiles();
  99. if(filesList2 != null){
  100. Files files = this.getInfo().getFiles().get(filesList2.size()-1);
  101. files.setMd5sum(value.toString());
  102. }else {
  103. this.getInfo().setMd5sum(value.toString());
  104. }
  105. break;
  106. case "name":
  107. this.getInfo().setName(value.toString());
  108. break;
  109. case "piece length":
  110. this.getInfo().setPiecesLength(Long.parseLong(value.toString()));
  111. break;
  112. case "pieces":
  113. if(StringUtil.isNumeric(value.toString())){
  114. this.getInfo().setPieces(null);
  115. }else{
  116. this.getInfo().setPieces((byte[])value);
  117. }
  118. break;
  119. case "path":
  120. List<Files> filesList3 = this.getInfo().getFiles();
  121. Files files3 = filesList3.get(filesList3.size()-1);
  122. files3.getPath().add(value.toString());
  123. break;
  124. }
  125. }
  126. }
  127. }

解析实体类

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. public class BitTorrents {
  7. public static BitTorrentInfo parse(File btFile) throws Exception {
  8. return new BitTorrents().analyze(new FileInputStream(btFile));
  9. }
  10. public static BitTorrentInfo parse(String btFilePath) throws Exception {
  11. return new BitTorrents().analyze(new FileInputStream(btFilePath));
  12. }
  13. private BitTorrentInfo analyze(InputStream is) throws Exception {
  14. BitTorrentInfo btInfo = new BitTorrentInfo();
  15. String key = null;
  16. StringBuilder strLengthBuilder = new StringBuilder();
  17. int tempByte;
  18. while ((tempByte = is.read()) != -1) {
  19. char temp = (char) tempByte;
  20. switch (temp) {
  21. case 'i':
  22. StringBuilder itempBuilder = new StringBuilder();
  23. char iTemp;
  24. while ((iTemp = (char) is.read()) != 'e') {
  25. itempBuilder.append(iTemp);
  26. }
  27. btInfo.setValue(key, itempBuilder.toString());
  28. break;
  29. case '0': case '1': case  '2': case '3': case '4': case  '5': case '6': case '7': case '8': case '9':
  30. strLengthBuilder.append(temp);
  31. break;
  32. case ':':
  33. int strLen = Integer.parseInt(strLengthBuilder.toString());
  34. strLengthBuilder = new StringBuilder();
  35. byte[] tempBytes = new byte[strLen];
  36. is.read(tempBytes);
  37. if (key != null && key.equals("pieces")) {
  38. btInfo.setValue(key, tempBytes);
  39. } else {
  40. String tempStr = new String(tempBytes);
  41. if (BitTorrentInfo.keyList.contains(tempStr)) {
  42. key = tempStr;
  43. if (tempStr.equals("announce-list")) {
  44. btInfo.setAnnounceList(new LinkedList<String>());
  45. } else if (tempStr.equals("info")) {
  46. btInfo.setInfo(new Info());
  47. } else if (tempStr.equals("files")) {
  48. btInfo.getInfo().setFiles(new LinkedList<Files>());
  49. btInfo.getInfo().getFiles().add(new Files());
  50. } else if (tempStr.equals("length")) {
  51. List<Files> tempFiles = btInfo.getInfo().getFiles();
  52. if (tempFiles != null) {
  53. if (tempFiles.isEmpty() || tempFiles.get(tempFiles.size() - 1).getLength() != 0) {
  54. tempFiles.add(new Files());
  55. }
  56. }
  57. } else if (tempStr.equals("md5sum")) {
  58. List<Files> tempFiles = btInfo.getInfo().getFiles();
  59. if (tempFiles != null) {
  60. if (tempFiles.isEmpty() || tempFiles.get(tempFiles.size() - 1).getMd5sum() != null) {
  61. tempFiles.add(new Files());
  62. }
  63. }
  64. } else if (tempStr.equals("path")) {
  65. List<Files> tempFilesList = btInfo.getInfo().getFiles();
  66. if (tempFilesList.isEmpty()) {
  67. Files files = new Files();
  68. files.setPath(new LinkedList<String>());
  69. tempFilesList.add(files);
  70. } else {
  71. Files files = tempFilesList.get(tempFilesList.size() - 1);
  72. if (files.getPath() == null) {
  73. files.setPath(new LinkedList<String>());
  74. }
  75. }
  76. }
  77. } else {
  78. btInfo.setValue(key, tempStr);
  79. }
  80. }
  81. break;
  82. }
  83. }
  84. return btInfo;
  85. }
  86. public static void main(String[] args) throws Exception {
  87. BitTorrentInfo info=parse("E://xx/xx.torrent");
  88. System.out.println("信息:"+info.getAnnounce()+"\t"+info.getComment()+"\t"+info.getCreateBy()+"\t"+GetDate.LongConvetDateTime(info.getCreationDate()));
  89. Info it=info.getInfo();
  90. System.out.println("信息:"+it.getName()+"\t"+it.getPiecesLength()+"\t"+it.getLength()+"\t"+it.getMd5sum()+"\t"+it.getPieces());
  91. if(info.getAnnounceList().size()>0){
  92. for(String str:info.getAnnounceList()){
  93. System.out.println("信息2:"+str);
  94. }
  95. }
  96. if(it.getFiles().size()>0){
  97. for(Files file: it.getFiles()){
  98. System.out.println("信息3:"+file.getLength()+"\t"+file.getMd5sum());
  99. if(file.getPath().size()>0){
  100. for(String str:file.getPath()){
  101. System.out.println("信息4:"+str);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }

Java实现bt文件下载、制作、解析、磁力链接的更多相关文章

  1. 实战Python实现BT种子转化为磁力链接

    经常看电影的朋友肯定对BT种子并不陌生,但是BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些. 将BT种子转换为占用空间更小,分享更方便的磁 ...

  2. nodejs 实现 磁力链接资源搜索 BT磁力链接爬虫

    项目简介 前端站点 项目效果预览 http://findcl.com 使用 nodejs 实现磁力链接爬虫 磁力链接解析成 torrent种子信息,保存到数据库,利用 Elasticsearch 实现 ...

  3. 常用下载方式的区别-BT下载、磁力链接、电驴

    出处:https://www.jianshu.com/p/72b7a64e5be1 打开 115 离线下载的窗口,看到支持这么多种链接,你都清楚他们是什么原理嘛?接下来我们一个一个说. 一.HTTP( ...

  4. BT中的磁力链接(转)

    注意:磁力链接不是迅雷的,而是BT网络中的一种协议. 磁力链接与种子文件 磁力链接并不是一个新概念,早在2002年,相关的标准草稿就已经制定了.但直到2012年海盗湾为规避版权问题删除了站点上的所有T ...

  5. 将BT转为磁力链接

    实战代码 安装完成后,我们来看下代码: 系统环境:Linux Python环境:Python2.7 请注意python版本 bt2url.py 1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  6. DHT协议网络爬虫磁力链接和BT种子搜索引擎

    系统功能和用到的技术. 系统包括几个独立的部分: 使用 Python 的 Scrapy 框架开发的网络爬虫,用来爬取磁力链接和种子: 使用 PHP CI 框架开发的简易网站: 搜索引擎目前直接使用的 ...

  7. 迅雷磁力链接转BT种子工具

    种子文件目录:C:\Users\jifeng\AppData\Local\Temp\magnetex MagnetEx.exe 从迅雷5.8支持磁力链接的无视受限资源版提取 MagnetEx.exe ...

  8. 知名互联网公司校招 Java 开发岗面试知识点解析

    天之道,损有余而补不足,是故虚胜实,不足胜有余. 本文作者在一年之内参加过多场面试,应聘岗位均为 Java 开发方向.在不断的面试中,分类总结了 Java 开发岗位面试中的一些知识点. 主要包括以下几 ...

  9. Java中的static关键字解析

    Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...

随机推荐

  1. laravel 5.1 Model 属性详解

    <?php namespace Illuminate\Database\Eloquent; /** * 下面提到某些词的含义: * 1.覆盖: 在继承该类 \Illuminate\Databas ...

  2. PHP转盘抽奖算法

    流程: 1.拼装奖项数组 2.计算概率 3.返回中奖情况 代码如下: 中奖概率 ' v ' 可以在后台设置,传到此方法中,注意传整数 function get_gift(){ //拼装奖项数组 // ...

  3. 利用word的VBA,为代码统一表格宽度,底色及行号

    如果文档中的代码表格时,感觉还是很快速有用的. Sub HangHao() ' ' hanghao 宏 ' ' Dim parag As Paragraph Dim nLineNum: nLineNu ...

  4. 000 Ajax介紹

    1.介紹 2.应用 3.优点 4.缺点

  5. java数据结构之树

    树定义和基本术语定义树(Tree)是n(n≥0)个结点的有限集T,并且当n>0时满足下列条件:     (1)有且仅有一个特定的称为根(Root)的结点:     (2)当n>1时,其余结 ...

  6. 系统的Drawable(一)

    系统的Drawable(一) 学习自 <Android 开发艺术探索> <官方文档> https://www.cnblogs.com/popfisher/p/6238119.h ...

  7. PMK数据生成工具airolib-ng

    PMK数据生成工具airolib-ng   PMK(Pairwise Master Key)是根据ESSID和无线密钥生成的哈希值,用于WPA/WPA2身份认证.在WPA/WPA2暴力破解中,需要大量 ...

  8. C#开发Unity游戏教程之判断语句

    C#开发Unity游戏教程之判断语句 游戏执行路径的选择——判断 玩家在游戏时,无时无刻不在通过判断做出选择.例如,正是因为玩家做出的选择不同,才导致游戏朝着不同的剧情发展,因此一个玩家可以对一个游戏 ...

  9. Redis学习笔记(1)- CentOS 6.4 安装Redis

    Redis学习笔记(1)- CentOS 6.4 安装Redis 2013.10.13     学习环境 vm 10.1 + 默认.新装的干净 CentOS 6.4  64BIT系统     准备 1 ...

  10. JFreeChart 之折线图

    JFreeChart 之折线图 一.JFreeChart 简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets ...