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

首先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()+"\t"+info.getComment()+"\t"+info.getCreateBy()+"\t"+GetDate.LongConvetDateTime(info.getCreationDate()));  
  93.         Info it=info.getInfo();  
  94.         System.out.println("信息:"+it.getName()+"\t"+it.getPiecesLength()+"\t"+it.getLength()+"\t"+it.getMd5sum()+"\t"+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()+"\t"+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. }  
posted @ 2018-05-11 13:43  快乐就好  阅读(11959)  评论(1编辑  收藏  举报