日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

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

發(fā)布時(shí)間:2023/12/13 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 Java实现bt文件下载、制作、解析、磁力链接 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

首先torrent里面肯定攜帶的有一些信息,所以就需要我們來(lái)解析這些信息。

我們這里做多文件制作torrent,所以首先要針對(duì)每一個(gè)文件建一個(gè)實(shí)體類(lèi)

[java]view plaincopy

importjava.util.List;

publicclassInfo{
privateStringname;
privatebyte[]pieces;
privatelongpiecesLength;
privatelonglength;
privateStringmd5sum;
privateList<Files>files;

publicInfo(){
}

publicInfo(Stringname,byte[]pieces,longpiecesLength,longlength,Stringmd5sum,List<Files>files){
super();
this.name=name;
this.pieces=pieces;
this.piecesLength=piecesLength;
this.length=length;
this.md5sum=md5sum;
this.files=files;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicbyte[]getPieces(){
returnpieces;
}

publicvoidsetPieces(byte[]pieces){
this.pieces=pieces;
}

publiclonggetPiecesLength(){
returnpiecesLength;
}

publicvoidsetPiecesLength(longpiecesLength){
this.piecesLength=piecesLength;
}

publiclonggetLength(){
returnlength;
}

publicvoidsetLength(longlength){
this.length=length;
}

publicStringgetMd5sum(){
returnmd5sum;
}

publicvoidsetMd5sum(Stringmd5sum){
this.md5sum=md5sum;
}

publicList<Files>getFiles(){
returnfiles;
}

publicvoidsetFiles(List<Files>files){
this.files=files;
}

而對(duì)于每一個(gè)File,又存在了一些信息,所以我們針對(duì)File建立一個(gè)實(shí)體類(lèi)

[java]view plaincopy

importjava.util.List;

publicclassFiles{
privatelonglength;
privateStringmd5sum;
privateList<String>path;

publicFiles(){
}
//getterandsetterandtostring

publiclonggetLength(){
returnlength;
}

publicFiles(longlength,Stringmd5sum,List<String>path){
super();
this.length=length;
this.md5sum=md5sum;
this.path=path;
}

publicvoidsetLength(longlength){
this.length=length;
}

publicStringgetMd5sum(){
returnmd5sum;
}

publicvoidsetMd5sum(Stringmd5sum){
this.md5sum=md5sum;
}

publicList<String>getPath(){
returnpath;
}

publicvoidsetPath(List<String>path){
this.path=path;
}
}

而我們?cè)谥谱鱰orrent文件時(shí),填寫(xiě)了很多信息,比如要web seeds等等。所以此時(shí)也需要一個(gè)實(shí)體類(lèi)

[java]view plaincopy

importjava.util.Arrays;
importjava.util.List;

importorg.jeecgframework.core.util.StringUtil;

publicclassBitTorrentInfo{
publicstaticList<String>keyList;
static{
String[]keys={"announce","announce-list","creationdate","comment","createdby",
"info","length","md5sum","name","piecelength","pieces","files","path"};
keyList=Arrays.asList(keys);
}

privateStringannounce;
privateList<String>announceList;
privatelongcreationDate;
privateStringcomment;
privateStringcreateBy;
privateInfoinfo;

publicBitTorrentInfo(){
}

//getterandsetterandtostring

publicBitTorrentInfo(Stringannounce,List<String>announceList,longcreationDate,Stringcomment,
StringcreateBy,Infoinfo){
super();
this.announce=announce;
this.announceList=announceList;
this.creationDate=creationDate;
this.comment=comment;
this.createBy=createBy;
this.info=info;
}

publicstaticList<String>getKeyList(){
returnkeyList;
}

publicstaticvoidsetKeyList(List<String>keyList){
BitTorrentInfo.keyList=keyList;
}

publicStringgetAnnounce(){
returnannounce;
}

publicvoidsetAnnounce(Stringannounce){
this.announce=announce;
}

publicList<String>getAnnounceList(){
returnannounceList;
}

publicvoidsetAnnounceList(List<String>announceList){
this.announceList=announceList;
}

publiclonggetCreationDate(){
returncreationDate;
}

publicvoidsetCreationDate(longcreationDate){
this.creationDate=creationDate;
}

publicStringgetComment(){
returncomment;
}

publicvoidsetComment(Stringcomment){
this.comment=comment;
}

publicStringgetCreateBy(){
returncreateBy;
}

publicvoidsetCreateBy(StringcreateBy){
this.createBy=createBy;
}

publicInfogetInfo(){
returninfo;
}

publicvoidsetInfo(Infoinfo){
this.info=info;
}

publicvoidsetValue(Stringkey,Objectvalue)throwsException{
if(!keyList.contains(key)){
thrownewException("notcontainsthiskey:"+key);
}else{
switch(key){
case"announce":this.setAnnounce(value.toString());break;
case"announce-list":this.getAnnounceList().add(value.toString());break;
case"creationdate":
if(StringUtil.isNumeric(value.toString())){
this.setCreationDate(Long.parseLong(value.toString()));
}else{
this.setCreationDate(0);
}
break;
case"comment":this.setComment(value.toString());break;
case"createdby":this.setCreateBy(value.toString());break;
case"length":
List<Files>filesList1=this.getInfo().getFiles();
if(filesList1!=null){
Filesfiles=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){
Filesfiles=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"piecelength":
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();
Filesfiles3=filesList3.get(filesList3.size()-1);
files3.getPath().add(value.toString());
break;
}
}
}
}

解析實(shí)體類(lèi)

[java]view plaincopy

importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStream;
importjava.util.LinkedList;
importjava.util.List;

publicclassBitTorrents{
publicstaticBitTorrentInfoparse(FilebtFile)throwsException{
returnnewBitTorrents().analyze(newFileInputStream(btFile));
}

publicstaticBitTorrentInfoparse(StringbtFilePath)throwsException{
returnnewBitTorrents().analyze(newFileInputStream(btFilePath));
}

privateBitTorrentInfoanalyze(InputStreamis)throwsException{
BitTorrentInfobtInfo=newBitTorrentInfo();
Stringkey=null;
StringBuilderstrLengthBuilder=newStringBuilder();
inttempByte;
while((tempByte=is.read())!=-1){
chartemp=(char)tempByte;
switch(temp){
case'i':
StringBuilderitempBuilder=newStringBuilder();
chariTemp;
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':':
intstrLen=Integer.parseInt(strLengthBuilder.toString());
strLengthBuilder=newStringBuilder();
byte[]tempBytes=newbyte[strLen];
is.read(tempBytes);
if(key!=null&&key.equals("pieces")){
btInfo.setValue(key,tempBytes);
}else{
StringtempStr=newString(tempBytes);
if(BitTorrentInfo.keyList.contains(tempStr)){
key=tempStr;
if(tempStr.equals("announce-list")){
btInfo.setAnnounceList(newLinkedList<String>());
}elseif(tempStr.equals("info")){
btInfo.setInfo(newInfo());
}elseif(tempStr.equals("files")){
btInfo.getInfo().setFiles(newLinkedList<Files>());
btInfo.getInfo().getFiles().add(newFiles());
}elseif(tempStr.equals("length")){
List<Files>tempFiles=btInfo.getInfo().getFiles();
if(tempFiles!=null){
if(tempFiles.isEmpty()||tempFiles.get(tempFiles.size()-1).getLength()!=0){
tempFiles.add(newFiles());
}
}
}elseif(tempStr.equals("md5sum")){
List<Files>tempFiles=btInfo.getInfo().getFiles();
if(tempFiles!=null){
if(tempFiles.isEmpty()||tempFiles.get(tempFiles.size()-1).getMd5sum()!=null){
tempFiles.add(newFiles());
}
}
}elseif(tempStr.equals("path")){
List<Files>tempFilesList=btInfo.getInfo().getFiles();
if(tempFilesList.isEmpty()){
Filesfiles=newFiles();
files.setPath(newLinkedList<String>());
tempFilesList.add(files);
}else{
Filesfiles=tempFilesList.get(tempFilesList.size()-1);
if(files.getPath()==null){
files.setPath(newLinkedList<String>());
}
}
}
}else{
btInfo.setValue(key,tempStr);
}
}
break;
}
}
returnbtInfo;
}

publicstaticvoidmain(String[]args)throwsException{
BitTorrentInfoinfo=parse("E://xx/xx.torrent");
System.out.println("信息:"+info.getAnnounce()+" "+info.getComment()+" "+info.getCreateBy()+" "+GetDate.LongConvetDateTime(info.getCreationDate()));
Infoit=info.getInfo();
System.out.println("信息:"+it.getName()+" "+it.getPiecesLength()+" "+it.getLength()+" "+it.getMd5sum()+" "+it.getPieces());
if(info.getAnnounceList().size()>0){
for(Stringstr:info.getAnnounceList()){
System.out.println("信息2:"+str);
}
}
if(it.getFiles().size()>0){
for(Filesfile:it.getFiles()){
System.out.println("信息3:"+file.getLength()+" "+file.getMd5sum());
if(file.getPath().size()>0){
for(Stringstr:file.getPath()){
System.out.println("信息4:"+str);
}
}
}
}
}
}

總結(jié)

以上是生活随笔為你收集整理的Java实现bt文件下载、制作、解析、磁力链接的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。