Java实现bt文件下载、制作、解析、磁力链接
首先torrent里面肯定携带的有一些信息,所以就需要我们来解析这些信息。
我们这里做多文件制作torrent,所以首先要针对每一个文件建一个实体类
- import java.util.List;
- public class Info {
- private String name;
- private byte[] pieces;
- private long piecesLength;
- private long length;
- private String md5sum;
- private List<Files> files;
- public Info() {
- }
- public Info(String name, byte[] pieces, long piecesLength, long length, String md5sum, List<Files> files) {
- super();
- this.name = name;
- this.pieces = pieces;
- this.piecesLength = piecesLength;
- this.length = length;
- this.md5sum = md5sum;
- this.files = files;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public byte[] getPieces() {
- return pieces;
- }
- public void setPieces(byte[] pieces) {
- this.pieces = pieces;
- }
- public long getPiecesLength() {
- return piecesLength;
- }
- public void setPiecesLength(long piecesLength) {
- this.piecesLength = piecesLength;
- }
- public long getLength() {
- return length;
- }
- public void setLength(long length) {
- this.length = length;
- }
- public String getMd5sum() {
- return md5sum;
- }
- public void setMd5sum(String md5sum) {
- this.md5sum = md5sum;
- }
- public List<Files> getFiles() {
- return files;
- }
- public void setFiles(List<Files> files) {
- this.files = files;
- }
而对于每一个File,又存在了一些信息,所以我们针对File建立一个实体类
- import java.util.List;
- public class Files {
- private long length;
- private String md5sum;
- private List<String> path;
- public Files() {
- }
- //getter and setter and tostring
- public long getLength() {
- return length;
- }
- public Files(long length, String md5sum, List<String> path) {
- super();
- this.length = length;
- this.md5sum = md5sum;
- this.path = path;
- }
- public void setLength(long length) {
- this.length = length;
- }
- public String getMd5sum() {
- return md5sum;
- }
- public void setMd5sum(String md5sum) {
- this.md5sum = md5sum;
- }
- public List<String> getPath() {
- return path;
- }
- public void setPath(List<String> path) {
- this.path = path;
- }
- }
而我们在制作torrent文件时,填写了很多信息,比如要web seeds等等。所以此时也需要一个实体类
- import java.util.Arrays;
- import java.util.List;
- import org.jeecgframework.core.util.StringUtil;
- public class BitTorrentInfo {
- public static List<String> keyList;
- static{
- String[] keys = {"announce", "announce-list", "creation date", "comment", "created by",
- "info", "length", "md5sum", "name", "piece length","pieces", "files", "path"};
- keyList = Arrays.asList(keys);
- }
- private String announce;
- private List<String> announceList;
- private long creationDate;
- private String comment;
- private String createBy;
- private Info info;
- public BitTorrentInfo() {
- }
- //getter and setter and tostring
- public BitTorrentInfo(String announce, List<String> announceList, long creationDate, String comment,
- String createBy, Info info) {
- super();
- this.announce = announce;
- this.announceList = announceList;
- this.creationDate = creationDate;
- this.comment = comment;
- this.createBy = createBy;
- this.info = info;
- }
- public static List<String> getKeyList() {
- return keyList;
- }
- public static void setKeyList(List<String> keyList) {
- BitTorrentInfo.keyList = keyList;
- }
- public String getAnnounce() {
- return announce;
- }
- public void setAnnounce(String announce) {
- this.announce = announce;
- }
- public List<String> getAnnounceList() {
- return announceList;
- }
- public void setAnnounceList(List<String> announceList) {
- this.announceList = announceList;
- }
- public long getCreationDate() {
- return creationDate;
- }
- public void setCreationDate(long creationDate) {
- this.creationDate = creationDate;
- }
- public String getComment() {
- return comment;
- }
- public void setComment(String comment) {
- this.comment = comment;
- }
- public String getCreateBy() {
- return createBy;
- }
- public void setCreateBy(String createBy) {
- this.createBy = createBy;
- }
- public Info getInfo() {
- return info;
- }
- public void setInfo(Info info) {
- this.info = info;
- }
- public void setValue(String key, Object value) throws Exception {
- if(!keyList.contains(key)){
- throw new Exception("not contains this key: " + key);
- }else{
- switch (key){
- case "announce":this.setAnnounce(value.toString());break;
- case "announce-list":this.getAnnounceList().add(value.toString());break;
- case "creation date":
- if(StringUtil.isNumeric(value.toString())){
- this.setCreationDate(Long.parseLong(value.toString()));
- }else{
- this.setCreationDate(0);
- }
- break;
- case "comment":this.setComment(value.toString());break;
- case "created by":this.setCreateBy(value.toString());break;
- case "length":
- List<Files> filesList1 = this.getInfo().getFiles();
- if(filesList1 != null){
- Files files = this.getInfo().getFiles().get(filesList1.size()-1);
- files.setLength(Long.parseLong(value.toString()));
- }else {
- this.getInfo().setLength(Long.parseLong(value.toString()));
- }
- break;
- case "md5sum":
- List<Files> filesList2 = this.getInfo().getFiles();
- if(filesList2 != null){
- Files files = this.getInfo().getFiles().get(filesList2.size()-1);
- files.setMd5sum(value.toString());
- }else {
- this.getInfo().setMd5sum(value.toString());
- }
- break;
- case "name":
- this.getInfo().setName(value.toString());
- break;
- case "piece length":
- this.getInfo().setPiecesLength(Long.parseLong(value.toString()));
- break;
- case "pieces":
- if(StringUtil.isNumeric(value.toString())){
- this.getInfo().setPieces(null);
- }else{
- this.getInfo().setPieces((byte[])value);
- }
- break;
- case "path":
- List<Files> filesList3 = this.getInfo().getFiles();
- Files files3 = filesList3.get(filesList3.size()-1);
- files3.getPath().add(value.toString());
- break;
- }
- }
- }
- }
解析实体类
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.util.LinkedList;
- import java.util.List;
- public class BitTorrents {
- public static BitTorrentInfo parse(File btFile) throws Exception {
- return new BitTorrents().analyze(new FileInputStream(btFile));
- }
- public static BitTorrentInfo parse(String btFilePath) throws Exception {
- return new BitTorrents().analyze(new FileInputStream(btFilePath));
- }
- private BitTorrentInfo analyze(InputStream is) throws Exception {
- BitTorrentInfo btInfo = new BitTorrentInfo();
- String key = null;
- StringBuilder strLengthBuilder = new StringBuilder();
- int tempByte;
- while ((tempByte = is.read()) != -1) {
- char temp = (char) tempByte;
- switch (temp) {
- case 'i':
- StringBuilder itempBuilder = new StringBuilder();
- char iTemp;
- while ((iTemp = (char) is.read()) != 'e') {
- itempBuilder.append(iTemp);
- }
- btInfo.setValue(key, itempBuilder.toString());
- break;
- case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
- strLengthBuilder.append(temp);
- break;
- case ':':
- int strLen = Integer.parseInt(strLengthBuilder.toString());
- strLengthBuilder = new StringBuilder();
- byte[] tempBytes = new byte[strLen];
- is.read(tempBytes);
- if (key != null && key.equals("pieces")) {
- btInfo.setValue(key, tempBytes);
- } else {
- String tempStr = new String(tempBytes);
- if (BitTorrentInfo.keyList.contains(tempStr)) {
- key = tempStr;
- if (tempStr.equals("announce-list")) {
- btInfo.setAnnounceList(new LinkedList<String>());
- } else if (tempStr.equals("info")) {
- btInfo.setInfo(new Info());
- } else if (tempStr.equals("files")) {
- btInfo.getInfo().setFiles(new LinkedList<Files>());
- btInfo.getInfo().getFiles().add(new Files());
- } else if (tempStr.equals("length")) {
- List<Files> tempFiles = btInfo.getInfo().getFiles();
- if (tempFiles != null) {
- if (tempFiles.isEmpty() || tempFiles.get(tempFiles.size() - 1).getLength() != 0) {
- tempFiles.add(new Files());
- }
- }
- } else if (tempStr.equals("md5sum")) {
- List<Files> tempFiles = btInfo.getInfo().getFiles();
- if (tempFiles != null) {
- if (tempFiles.isEmpty() || tempFiles.get(tempFiles.size() - 1).getMd5sum() != null) {
- tempFiles.add(new Files());
- }
- }
- } else if (tempStr.equals("path")) {
- List<Files> tempFilesList = btInfo.getInfo().getFiles();
- if (tempFilesList.isEmpty()) {
- Files files = new Files();
- files.setPath(new LinkedList<String>());
- tempFilesList.add(files);
- } else {
- Files files = tempFilesList.get(tempFilesList.size() - 1);
- if (files.getPath() == null) {
- files.setPath(new LinkedList<String>());
- }
- }
- }
- } else {
- btInfo.setValue(key, tempStr);
- }
- }
- break;
- }
- }
- return btInfo;
- }
- public static void main(String[] args) throws Exception {
- BitTorrentInfo info=parse("E://xx/xx.torrent");
- System.out.println("信息:"+info.getAnnounce()+"\t"+info.getComment()+"\t"+info.getCreateBy()+"\t"+GetDate.LongConvetDateTime(info.getCreationDate()));
- Info it=info.getInfo();
- System.out.println("信息:"+it.getName()+"\t"+it.getPiecesLength()+"\t"+it.getLength()+"\t"+it.getMd5sum()+"\t"+it.getPieces());
- if(info.getAnnounceList().size()>0){
- for(String str:info.getAnnounceList()){
- System.out.println("信息2:"+str);
- }
- }
- if(it.getFiles().size()>0){
- for(Files file: it.getFiles()){
- System.out.println("信息3:"+file.getLength()+"\t"+file.getMd5sum());
- if(file.getPath().size()>0){
- for(String str:file.getPath()){
- System.out.println("信息4:"+str);
- }
- }
- }
- }
- }
- }
Java实现bt文件下载、制作、解析、磁力链接的更多相关文章
- 实战Python实现BT种子转化为磁力链接
经常看电影的朋友肯定对BT种子并不陌生,但是BT种子文件相对磁力链来说存储不方便,而且在网站上存放BT文件容易引起版权纠纷,而磁力链相对来说则风险小一些. 将BT种子转换为占用空间更小,分享更方便的磁 ...
- nodejs 实现 磁力链接资源搜索 BT磁力链接爬虫
项目简介 前端站点 项目效果预览 http://findcl.com 使用 nodejs 实现磁力链接爬虫 磁力链接解析成 torrent种子信息,保存到数据库,利用 Elasticsearch 实现 ...
- 常用下载方式的区别-BT下载、磁力链接、电驴
出处:https://www.jianshu.com/p/72b7a64e5be1 打开 115 离线下载的窗口,看到支持这么多种链接,你都清楚他们是什么原理嘛?接下来我们一个一个说. 一.HTTP( ...
- BT中的磁力链接(转)
注意:磁力链接不是迅雷的,而是BT网络中的一种协议. 磁力链接与种子文件 磁力链接并不是一个新概念,早在2002年,相关的标准草稿就已经制定了.但直到2012年海盗湾为规避版权问题删除了站点上的所有T ...
- 将BT转为磁力链接
实战代码 安装完成后,我们来看下代码: 系统环境:Linux Python环境:Python2.7 请注意python版本 bt2url.py 1 2 3 4 5 6 7 8 9 10 11 12 1 ...
- DHT协议网络爬虫磁力链接和BT种子搜索引擎
系统功能和用到的技术. 系统包括几个独立的部分: 使用 Python 的 Scrapy 框架开发的网络爬虫,用来爬取磁力链接和种子: 使用 PHP CI 框架开发的简易网站: 搜索引擎目前直接使用的 ...
- 迅雷磁力链接转BT种子工具
种子文件目录:C:\Users\jifeng\AppData\Local\Temp\magnetex MagnetEx.exe 从迅雷5.8支持磁力链接的无视受限资源版提取 MagnetEx.exe ...
- 知名互联网公司校招 Java 开发岗面试知识点解析
天之道,损有余而补不足,是故虚胜实,不足胜有余. 本文作者在一年之内参加过多场面试,应聘岗位均为 Java 开发方向.在不断的面试中,分类总结了 Java 开发岗位面试中的一些知识点. 主要包括以下几 ...
- Java中的static关键字解析
Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...
随机推荐
- 20165330《网络对抗技术》Exp0 Kali安装
Kali安装 下载地址 Kali官网 VMware 安装步骤 参考在虚拟机中安装kali linux 安装Kali Linux的镜像和VMware 打开VMware,选择文件-新建虚拟机,出现对话框选 ...
- LOJ 10155 - 「一本通 5.2 例 3」数字转换
前言 从现在开始,这个博客要写一些题解了.起初,开这个博客只是好玩一样,没事就写写CSS.JS,然后把博客前端搞成了现在这个样子.以前博客只是偶尔记录一些东西,刷题也从来不记录,最近受一些学长的影响, ...
- 《精通ASP.NET MVC5》第7章 SportStore:一个真正的应用程序(1)
7.1 开始 7.1.1 解决方案 个工程. 1. 一个域模块工程. 2.一个MVC4应用. 3.一个单元测试工程. 现在我们就创建一个名为 SportsStore 的空 soluti ...
- Java编程的逻辑 (20) - 为什么要有抽象类?
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...
- Linux系统运维笔记(四),CentOS 6.4安装Nginx
Linux系统运维笔记(四),CentOS 6.4安装Nginx 1,安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool op ...
- Connect to DB2 database in eclipse via jdbc
https://stackoverflow.com/questions/23801841/connect-to-db2-database-in-eclipse-via-jdbc
- 【AtCoder】AGC022
A - Diverse Word 不到26位就加上一个最小的 到26位了就搜一下,最多回溯就一次,所以复杂度不大 #include <iostream> #include <cstd ...
- 不同浏览器的userAgent
一.IE浏览器 //IE6 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" //IE7 "Mozill ...
- win7如何不用点击用户名直接自动登录桌面
在 win7 系统中开机时必须点击相应的用户名才能登 陆系统桌面那么如何取消这一功能使当前账户自动登 录到系统桌面呢? 一. win7 如何自动登录 .在开始菜单搜索框输入 “netplwiz” 按回 ...
- BZOJ.4819.[SDOI2017]新生舞会(01分数规划 费用流SPFA)
BZOJ 洛谷 裸01分数规划.二分之后就是裸最大费用最大流了. 写的朴素SPFA费用流,洛谷跑的非常快啊,为什么有人还T成那样.. 当然用二分也很慢,用什么什么迭代会很快. [Update] 19. ...