com.acgist.snail.downloader.http.HttpDownloader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snail Show documentation
Show all versions of snail Show documentation
基于Java开发的下载工具,支持下载协议:BT(BitTorrent、磁力链接、种子文件)、HLS(M3U8)、FTP、HTTP。
package com.acgist.snail.downloader.http;
import java.nio.channels.Channels;
import com.acgist.snail.context.exception.NetException;
import com.acgist.snail.downloader.SingleFileDownloader;
import com.acgist.snail.net.http.HttpClient;
import com.acgist.snail.pojo.ITaskSession;
import com.acgist.snail.utils.FileUtils;
import com.acgist.snail.utils.IoUtils;
/**
* HTTP任务下载器
*
* @author acgist
*/
public final class HttpDownloader extends SingleFileDownloader {
/**
* @param taskSession 任务信息
*/
private HttpDownloader(ITaskSession taskSession) {
super(taskSession);
}
/**
* 新建HTTP任务下载器
*
* @param taskSession 任务信息
*
* @return {@link HttpDownloader}
*/
public static final HttpDownloader newInstance(ITaskSession taskSession) {
return new HttpDownloader(taskSession);
}
@Override
public void release() {
IoUtils.close(this.input);
IoUtils.close(this.output);
super.release();
}
@Override
protected void buildInput() throws NetException {
final long downloadSize = FileUtils.fileSize(this.taskSession.getFile());
final var client = HttpClient
.newDownloader(this.taskSession.getUrl())
.range(downloadSize)
.get();
if(client.downloadable()) {
final var headers = client.responseHeader();
this.input = Channels.newChannel(client.response());
if(headers.range()) {
this.taskSession.downloadSize(downloadSize);
} else {
this.taskSession.downloadSize(0L);
}
} else if(this.taskSession.downloadSize() == this.taskSession.getSize()) {
// 优先验证下载文件大小
// 416:超出请求范围
this.completed = true;
} else {
this.fail("HTTP请求失败:" + client.code());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy