Java实现bt文件下载、制作、解析、磁力链接

摘要:
所以我们需要解析这些信息。因此,首先,创建一个实体类[java]viewplain copy import java。util。每个文件的列表;this.md5sum=md5sum;}对于每个文件,我们创建一个实体类[java]viewplain copy import java。util。列表

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

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

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

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

      

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

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

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

解析实体类

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

免责声明:文章转载自《Java实现bt文件下载、制作、解析、磁力链接》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇访问 IIS 元数据库失败 的解决方法内存溢出分析定位下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

进阶篇-用户界面:6.android studio使用github开源库实现下拉刷新

      说实话,这是我第一次这么正儿八经的用github开源库,之前一直在听一些大神对这个世界级的开源库赞不绝口,今天终于体会到了。由于下拉刷新的类库是在eclipse下开发完成的,而eclipse如何使用如果导入网上的教程都非常详细。昨天我试了半天发现由于自己对android studio还不是很熟悉,所以引用类库的时候发现无从下手。但是今天早晨起来...

JAVA的mock工具mockito简介

在测试过程中,难免会碰到交互的外围系统不给力的情况,这时候mock就派上用场了,前段时间跟同学聊到这块的时候,他向我推荐mockito这个mock工具,试用了一下,确实很好用,这里给大家介绍下这款工具: 1、mockito的特点 它既能mock接口也能mock实体类(咱测试框架mock工具也能做到) 简单的注解语法-@Mock 简单易懂,语法简单 支持顺...

document.cookie的使用

设置cookie 每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cookie="userId=828";如果要一次存储多个名/值对,可以使用分号加空格(; )隔开,例如: document.cookie="userId=828; userName=hulk"; 在cookie的名或值中不...

SpringBoot:Sqlite3+SpringBoot2.1.3+Mybatis-Puls整合项目

应公司要求完成sqlite3数据库的增改查小功能,特此记录一下。 1.建造项目 结构如下 因为是提供给前端调用所以做了接口。 2.Pom依赖文件 下面是这个项目所依赖的jar包。 <parent> <groupId>org.springframework.boot</groupId>...

homebrew &amp;amp; brew cask使用技巧及Mac软件安装

homebrew 安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 命令 安装软件:brew install 软件名,例:brew install wget 搜索软件:brew search 软件名...

如何控制el-image预览图片的大小

使用el-image标签,发现预览图太大,占满了整个浏览器 代码如下: <el-image :src="scope.row.carouselUrl" :preview-src-list="scope.row.photo"/> 如何才能控制预览图片的大小呢?如果我想不使用image组件又想使用预览大图的功能是否可行呢?我们可以使用image-vi...