Java实现迅雷地址转成普通地址实例代码
原理分析:迅雷的thunder://地址就是将普通url地址加前缀‘AA'、后缀‘ZZ',再base64编码后得到的字符串
实现:
步骤1,添加工具类Base64编码和解码:Base64.java
packageth;
importjava.io.*;
/**
*Base64编码和解码。
*
*@author宋立君
*@date2014年07月03日
*/
publicclassBase64{
publicBase64(){
}
/**
*功能:编码字符串
*
*@author宋立君
*@date2014年07月03日
*@paramdata
*源字符串
*@returnString
*/
publicstaticStringencode(Stringdata){
returnnewString(encode(data.getBytes()));
}
/**
*功能:解码字符串
*
*@author宋立君
*@date2014年07月03日
*@paramdata
*源字符串
*@returnString
*/
publicstaticStringdecode(Stringdata){
returnnewString(decode(data.toCharArray()));
}
/**
*功能:编码byte[]
*
*@author宋立君
*@date2014年07月03日
*@paramdata
*源
*@returnchar[]
*/
publicstaticchar[]encode(byte[]data){
char[]out=newchar[((data.length+2)/3)*4];
for(inti=0,index=0;i>=6;
out[index+2]=alphabet[(trip?(val&0x3F):64)];
val>>=6;
out[index+1]=alphabet[val&0x3F];
val>>=6;
out[index+0]=alphabet[val&0x3F];
}
returnout;
}
/**
*功能:解码
*
*@author宋立君
*@date2014年07月03日
*@paramdata
*编码后的字符数组
*@returnbyte[]
*/
publicstaticbyte[]decode(char[]data){
inttempLen=data.length;
for(intix=0;ix255)||codes[data[ix]]<0){
--tempLen;//ignorenon-validcharsandpadding
}
}
//calculaterequiredlength:
//--3bytesforevery4validbase64chars
//--plus2bytesifthereare3extrabase64chars,
//orplus1byteifthereare2extra.
intlen=(tempLen/4)*3;
if((tempLen%4)==3){
len+=2;
}
if((tempLen%4)==2){
len+=1;
}
byte[]out=newbyte[len];
intshift=0;//#ofexcessbitsstoredinaccum
intaccum=0;//excessbits
intindex=0;
//wenowgothroughtheentirearray(NOTusingthe'tempLen'value)
for(intix=0;ix255)?-1:codes[data[ix]];
if(value>=0){//skipovernon-code
accum<<=6;//bitsshiftupby6eachtimethru
shift+=6;//loop,withnewbitsbeingputin
accum|=value;//atthebottom.
if(shift>=8){//wheneverthereare8ormoreshiftedin,
shift-=8;//writethemout(fromthetop,leavingany
out[index++]=//excessatthebottomfornextiteration.
(byte)((accum>>shift)&0xff);
}
}
}
//ifthereisSTILLsomethingwrongwejusthavetothrowupnow!
if(index!=out.length){
thrownewError("Miscalculateddatalength(wrote"+index
+"insteadof"+out.length+")");
}
returnout;
}
/**
*功能:编码文件
*
*@author宋立君
*@date2014年07月03日
*@paramfile
*源文件
*/
publicstaticvoidencode(Filefile)throwsIOException{
if(!file.exists()){
System.exit(0);
}
else{
byte[]decoded=readBytes(file);
char[]encoded=encode(decoded);
writeChars(file,encoded);
}
file=null;
}
/**
*功能:解码文件。
*
*@author宋立君
*@date2014年07月03日
*@paramfile
*源文件
*@throwsIOException
*/
publicstaticvoiddecode(Filefile)throwsIOException{
if(!file.exists()){
System.exit(0);
}else{
char[]encoded=readChars(file);
byte[]decoded=decode(encoded);
writeBytes(file,decoded);
}
file=null;
}
//
//codecharactersforvalues0..63
//
privatestaticchar[]alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
.toCharArray();
//
//lookuptableforconvertingbase64characterstovalueinrange0..63
//
privatestaticbyte[]codes=newbyte[256];
static{
for(inti=0;i<256;i++){
codes[i]=-1;
//LoggerUtil.debug(i+"&"+codes[i]+"");
}
for(inti='A';i<='Z';i++){
codes[i]=(byte)(i-'A');
//LoggerUtil.debug(i+"&"+codes[i]+"");
}
for(inti='a';i<='z';i++){
codes[i]=(byte)(26+i-'a');
//LoggerUtil.debug(i+"&"+codes[i]+"");
}
for(inti='0';i<='9';i++){
codes[i]=(byte)(52+i-'0');
//LoggerUtil.debug(i+"&"+codes[i]+"");
}
codes['+']=62;
codes['/']=63;
}
privatestaticbyte[]readBytes(Filefile)throwsIOException{
ByteArrayOutputStreambaos=newByteArrayOutputStream();
byte[]b=null;
InputStreamfis=null;
InputStreamis=null;
try{
fis=newFileInputStream(file);
is=newBufferedInputStream(fis);
intcount=0;
byte[]buf=newbyte[16384];
while((count=is.read(buf))!=-1){
if(count>0){
baos.write(buf,0,count);
}
}
b=baos.toByteArray();
}finally{
try{
if(fis!=null)
fis.close();
if(is!=null)
is.close();
if(baos!=null)
baos.close();
}catch(Exceptione){
System.out.println(e);
}
}
returnb;
}
privatestaticchar[]readChars(Filefile)throwsIOException{
CharArrayWritercaw=newCharArrayWriter();
Readerfr=null;
Readerin=null;
try{
fr=newFileReader(file);
in=newBufferedReader(fr);
intcount=0;
char[]buf=newchar[16384];
while((count=in.read(buf))!=-1){
if(count>0){
caw.write(buf,0,count);
}
}
}finally{
try{
if(caw!=null)
caw.close();
if(in!=null)
in.close();
if(fr!=null)
fr.close();
}catch(Exceptione){
System.out.println(e);
}
}
returncaw.toCharArray();
}
privatestaticvoidwriteBytes(Filefile,byte[]data)throwsIOException{
OutputStreamfos=null;
OutputStreamos=null;
try{
fos=newFileOutputStream(file);
os=newBufferedOutputStream(fos);
os.write(data);
}finally{
try{
if(os!=null)
os.close();
if(fos!=null)
fos.close();
}catch(Exceptione){
System.out.println(e);
}
}
}
privatestaticvoidwriteChars(Filefile,char[]data)throwsIOException{
Writerfos=null;
Writeros=null;
try{
fos=newFileWriter(file);
os=newBufferedWriter(fos);
os.write(data);
}finally{
try{
if(os!=null)
os.close();
if(fos!=null)
fos.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
}
步骤2,编写迅雷地址转普通地址的类及方法:ThunderSiteConverUtil.java
packageth; /** *迅雷地址转普通地址 *Title:ThunderSiteConverUtil
*Description:
*Company:www.itcast.com
*@author入云龙 *@date2017年3月6日下午2:11:32 *@version1.0 */ publicclassThunderSiteConverUtil{ /** *迅雷thunder://地址与普通url地址转换 其实迅雷的thunder://地址就是将普通url地址加前缀‘AA'、后缀‘ZZ',再base64编码后得到的字符串 *Title:t1
*Description:
*@paramurl *@return */ publicStringconver(Stringurl){ StringnewUrl=""; //s=s.substring(intbegin,intend);截取s中从begin开始至end结束时的字符串,并将其赋值给s; //去掉迅雷地址前缀 url=url.substring(10,url.length()); //解密 newUrl=Base64.decode(url); //去掉头AA,尾ZZ newUrl=newUrl.substring(2,newUrl.length()-2); returnnewUrl; } }
步骤3,建立测试类:TestTh.java
packageth;
importorg.junit.Test;
/*
*迅雷地址转普通地址测试
*/
publicclassTestTh{
@Test
publicvoidtest1(){
Stringurl="thunder://QUFodHRwOi8vdG9vbC5sdS90ZXN0LnppcFpa";
System.out.println("迅雷地址:"+url);
url=newThunderSiteConverUtil().conver(url);
System.out.println("普通地址:"+url);
}
}
运行Junit测试test1,控制台输出:
迅雷地址:thunder://QUFodHRwOi8vdG9vbC5sdS90ZXN0LnppcFpa
普通地址:http://tool.lu/test.zip
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。