Java实现bt文件下载、制作、解析、磁力链接
首先torrent里面肯定攜帶的有一些信息,所以就需要我們來解析這些信息。
我們這里做多文件制作torrent,所以首先要針對每一個文件建一個實體類
[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;
}
而對于每一個File,又存在了一些信息,所以我們針對File建立一個實體類
[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;
}
}
而我們在制作torrent文件時,填寫了很多信息,比如要web seeds等等。所以此時也需要一個實體類
[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;
}
}
}
}
解析實體類
[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);
}
}
}
}
}
}
總結
以上是生活随笔為你收集整理的Java实现bt文件下载、制作、解析、磁力链接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【STM32H7教程】第82章
- 下一篇: ios中的事件处理、响应者链条以及第一响