Aria2RPC接⼝协议和Java的本地调⽤实现
如果你还没有启动aria2 :
⽬录
Aria2 RPC接⼝协议
本⽂中我们使⽤默认的本地调⽤,我们需要⽤POST⽅法向该地址发送Json格式的请求
localhost:6800/jsonrpc
详情请参考上⽅的官⽅⽂档,只以⼀个简单例⼦进⾏说明
{
//消息id,aria2会原样返回这个id,可以⾃动⽣成也可以⽤其他唯⼀标识
"id":"dsfasdf",
//固定值
"jsonrpc":"2.0",
//⽅法名,具体参考上⽅“⽅法列表”链接,本例中为“添加下载任务”
"method":"aria2.addUri",
//params为数组
"params":[
//第⼀个成员也为⼀个数组,它内容为需下载⽂件的url,如果有多个来源可以写多个
[
"***.***.***/*****.png"
],
//第⼆个成员为对象,成员为下载参数,详情参考上⽅“下载参数”链接
{
/
/下载根⽬录
"dir":"E:/download/",
//⽬标⽂件名
"out":"test1.png",
//referer ⽤来绕开部分防盗链机制星号表⽰使⽤url作为referer
"referer":"*"
}
]
}
返回格式
{
/
/上⽅传⼊的消息id
"id":"dsfasdf",
"jsonrpc":"2.0",
//任务的唯⼀标识
"result":"588e449a86cb56f3"
}
如果不想看全英⽂的官⽅⽂档,也可以⽤ ariaNg 插件,打开 F12 - network ,⼿动添加任务后点击对应的请求查看它的请求格式。
Java实现
依赖
<!--lombok插件不使⽤的话需要⾃⼰写getter setter⽅法--> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--发送http请求-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
<!--json解析-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
核⼼类
根据协议规定的对象结构定义对象类
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.NoArgsConstructor;
perimental.Accessors;
slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.hods.CloseableHttpResponse; import org.apache.hods.HttpPost;
import org.HttpHostConnectException;
import org.ity.ContentType;
import org.ity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Aria2请求的Json对象
*
* @author bx002
* @date 2020/11/27 17:20
*/
//lombok注解提供所有getter setter⽅法
@Data
//开启setter⽅法的链式调⽤
@Accessors(chain =true)
//⽆参构造⽅法
@NoArgsConstructor
@Slf4j
public class Aria2Json {
/**
* ⽅法名常量
*/
public final static String METHOD_TELL_ACTIVE ="llActive"; public final static String METHOD_ADD_URI ="aria2.addUri";
public final static String METHOD_GET_GLOBAL_STAT ="GlobalStat";
public final static String METHOD_TELL_STOPPED ="llStopped";
public final static String METHOD_TELL_WAITING ="llWaiting";
public final static String METHOD_REMOVE_DOWNLOAD_RESULT ="veDownloadResult"; private final static String[] PARAM_ARRAY_OF_FILED =
new String[]{"totalLength","completedLength","files","status","errorCode","gid"};
/**
* id随机⽣成,也可以⼿动设置
*/
private String id = UUID.randomUUID().toString();
private String jsonrpc ="2.0";
private String method = METHOD_TELL_ACTIVE;
private String url;
private List<Object> params =new ArrayList<>();
//暂存下载参数
/**
* 添加下载参数
* @return
*/
public Aria2Json addParam(Object obj){
params.add(obj);
return this;
}
public static String tellActive(){
Aria2Json aria2Json =new Aria2Json();
aria2Json.setMethod(METHOD_TELL_ACTIVE)
.addParam(PARAM_ARRAY_OF_FILED);
return aria2Json.send(null);
}
public static String tellStopped(){
Aria2Json aria2Json =new Aria2Json();
aria2Json.setMethod(METHOD_TELL_STOPPED)
.addParam(-1)
.addParam(1000)
.addParam(PARAM_ARRAY_OF_FILED);
return aria2Json.send(null);
}下载apache
public static String tellWaiting(){
Aria2Json aria2Json =new Aria2Json();
aria2Json.setMethod(METHOD_TELL_WAITING)
.addParam(0)
.addParam(1000)
.
addParam(PARAM_ARRAY_OF_FILED);
return aria2Json.send(null);
}
public static String removeDownloadResult(String gid){
Aria2Json aria2Json =new Aria2Json();
aria2Json.setMethod(METHOD_REMOVE_DOWNLOAD_RESULT)
.addParam(gid);
return aria2Json.send(null);
}
public Aria2Json(String id){
this.id = id;
}
public String send(String jsonRpcUrl){
//rpcurl 默认为本地默认地址
jsonRpcUrl = StringUtils.isEmpty(jsonRpcUrl)?"localhost:6800/jsonrpc": jsonRpcUrl;
//创建post请求对象
HttpPost httpPost =new HttpPost(jsonRpcUrl);
HttpPost httpPost =new HttpPost(jsonRpcUrl);
//设置content type(正⽂类型)为json格式
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.String());
//将 this 对象解析为 json字符串并⽤UTF-8编码(重要)将其设置为 entity (正⽂)
httpPost.setEntity(new JSONString(this), StandardCharsets.UTF_8));
/
/发送请求并获取返回对象
CloseableHttpResponse response;
try{
response = ateDefault().execute(httpPost);
}catch(HttpHostConnectException e){
log.debug("Aria2 ⽆法连接");
return null;
}catch(IOException e){
e.printStackTrace();
return null;
}
/
/返回的状态码
int statusCode = StatusLine().getStatusCode();
HttpEntity entity = Entity();
//请求结果字符串
String result = null;
try{
//⽤UTF-8解码返回字符串
result = String(entity, StandardCharsets.UTF_8);
//如果状态码为200表⽰请求成功,返回结果
if(statusCode == HttpStatus.SC_OK){
return result;
}
}catch(IOException | ParseException e){
e.printStackTrace();
}
//请求失败打印状态码和提⽰信息返回null
System.out.println("statusCode = "+ statusCode);
System.out.println("result = "+ result);
return null;
}
}
调⽤
由于添加下载需要⼀个对象作为下载参数,定义⼀个option类
@Data
@Accessors(chain =true)
public class Aria2Option {
String dir;
String out;
String referer;
}
调⽤时先设置option 再把option放⼊对象中
Aria2Option aria2Option =new Aria2Option();
aria2Option.setDir(dir)
.setOut(filName)
.setReferer("*")
;
String url ="***.***.***/aaa.png";
Aria2Json aria2Json =new Id()); aria2Json.setMethod(Aria2Json.METHOD_ADD_URI)
.addParam(new String[]{url})
.addParam(aria2Option);
;
String send = aria2Json.send(null);
其他静态调⽤
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论