Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.qiniu.process.other.DownloadFile Maven / Gradle / Ivy
Go to download
qiniu-suits is a efficient tools for qiniu api implemented by java8.
package com.qiniu.process.other;
import com.qiniu.process.Base;
import com.qiniu.storage.Configuration;
import com.qiniu.util.FileUtils;
import com.qiniu.util.RequestUtils;
import com.qiniu.util.StringMap;
import com.qiniu.util.URLUtils;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class DownloadFile extends Base> {
private String protocol;
private String domain;
private String urlIndex;
private StringMap headers;
private String suffixOrQuery;
private boolean preDown;
private String addPrefix;
private String rmPrefix;
private String downPath;
private Configuration configuration;
private HttpDownloader downloader;
public DownloadFile(Configuration configuration, String protocol, String domain, String urlIndex, String host,
int[] range, String suffixOrQuery, String addPrefix, String rmPrefix, String downPath, String savePath,
int saveIndex) throws IOException {
super("download", "", "", null, savePath, saveIndex);
set(configuration, protocol, domain, urlIndex, host, range, suffixOrQuery, addPrefix, rmPrefix, downPath);
downloader = new HttpDownloader(configuration);
}
public DownloadFile(Configuration configuration, String protocol, String domain, String urlIndex, String host,
int[] range, String suffixOrQuery, String addPrefix, String rmPrefix, String downPath) throws IOException {
super("download", "", "", null);
set(configuration, protocol, domain, urlIndex, host, range, suffixOrQuery, addPrefix, rmPrefix, downPath);
downloader = new HttpDownloader(configuration);
}
public DownloadFile(Configuration configuration, String protocol, String domain, String urlIndex, String host,
int[] range, String suffixOrQuery, String addPrefix, String rmPrefix, String downPath, String savePath)
throws IOException {
this(configuration, protocol, domain, urlIndex, host, range, suffixOrQuery, addPrefix, rmPrefix, downPath,
savePath, 0);
}
private void set(Configuration configuration, String protocol, String domain, String urlIndex, String host,
int[] range, String suffixOrQuery, String addPrefix, String rmPrefix, String downPath) throws IOException {
this.configuration = configuration;
if (domain == null || "".equals(domain)) {
if (urlIndex == null || "".equals(urlIndex)) {
throw new IOException("please set one of domain and url-index.");
} else {
this.urlIndex = urlIndex;
}
this.domain = null;
} else {
this.protocol = protocol == null || !protocol.matches("(http|https)") ? "http" : protocol;
RequestUtils.lookUpFirstIpFromHost(domain);
this.domain = domain;
this.urlIndex = "url";
}
if (host != null && !"".equals(host)) {
RequestUtils.lookUpFirstIpFromHost(host);
headers = new StringMap().put("Host", host);
}
if (range != null && range.length > 0) {
if (headers == null) headers = new StringMap();
headers.put("Range", new StringBuilder("bytes=").append(range[0]).append("-").append(range.length > 1 ? range[1] : ""));
}
this.suffixOrQuery = suffixOrQuery == null ? "" : suffixOrQuery;
this.addPrefix = addPrefix == null ? "" : addPrefix;
this.rmPrefix = rmPrefix;
this.downPath = downPath;
// downPath 为空时则表示预热方式下载
if (downPath == null || "".equals(downPath)) {
this.preDown = true;
} else {
File file = new File(downPath);
if (file.exists()) {
if (!file.isDirectory()) throw new IOException("please change down-path because it's existed file.");
} else if (!file.mkdirs()) {
if (!file.mkdirs()) throw new IOException(String.join("", "directory: ", downPath, " can not be created."));
}
}
}
@Override
public DownloadFile clone() throws CloneNotSupportedException {
DownloadFile downloadFile = (DownloadFile)super.clone();
downloadFile.downloader = new HttpDownloader(configuration);
return downloadFile;
}
@Override
protected String resultInfo(Map line) {
return domain == null ? line.get(urlIndex) : line.get("key");
}
@Override
protected String singleResult(Map line) throws Exception {
String url;
String key = line.get("key");
if (domain == null) {
url = line.get(urlIndex);
if (key != null) key = String.join("", addPrefix, FileUtils.rmPrefix(rmPrefix, key));
else key = String.join("", addPrefix, FileUtils.rmPrefix(rmPrefix, URLUtils.getKey(url)));
url = url + suffixOrQuery;
} else {
if (key == null || "".equals(key)) throw new IOException("key is not exists or empty in " + line);
url = String.join("", protocol, "://", domain, "/", key.replace("\\?", "%3f"), suffixOrQuery);
key = String.join("", addPrefix, FileUtils.rmPrefix(rmPrefix, key)); // 目标文件名
}
if (preDown) {
downloader.download(url, headers);
return String.join("\t", key, url);
} else {
String filename = String.join(FileUtils.pathSeparator, downPath, key);
downloader.download(url, filename, headers);
return String.join("\t", filename, url);
}
}
@Override
public void closeResource() {
super.closeResource();
protocol = null;
domain = null;
urlIndex = null;
headers = null;
suffixOrQuery = null;
addPrefix = null;
rmPrefix = null;
configuration = null;
downloader = null;
}
}