Skip to content

add referer in header #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,55 @@ public void onError(Request request, ApiError error, UfileErrorBean response) {
});
}

public static void downloadFileWithReferer(String keyName, String bucketName, String localDir, String saveName) {
try {
ObjectProfile profile = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.objectProfile(keyName, bucketName)
.execute();

JLog.D(TAG, String.format("[res] = %s", (profile == null ? "null" : profile.toString())));
if (profile == null)
return;

DownloadFileBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.downloadFile(profile)
.saveAt(localDir, saveName)
/**
* 选择要下载的对象的范围,Default = [0, whole size]
*/
// .withinRange(0, 0)
/**
* 配置同时分片下载的进程数,Default = 10
*/
// .together(5)
/**
* 是否覆盖本地已有文件, Default = true;
*/
// .withCoverage(false)
/**
* 指定progress callback的间隔
*/
// .withProgressConfig(ProgressConfig.callbackWithPercent(10))
/**
* 配置读写流Buffer的大小, Default = 256 KB, MIN = 4 KB, MAX = 4 MB
*/
// .setBufferSize(4 << 20)
/**
* 配置进度监听
*/
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(long bytesWritten, long contentLength) {
JLog.D(TAG, String.format("[progress] = %d%% - [%d/%d]", (int) (bytesWritten * 1.f / contentLength * 100), bytesWritten, contentLength));
}
})
.setReferer("input your referer")
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,44 @@ public void onError(Request request, ApiError error, UfileErrorBean response) {
}
});
}

public static void getFileWithReferer(String url, String localDir, String saveName) {
try {
DownloadFileBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getFile(url)
.saveAt(localDir, saveName)
/**
* 选择要下载的对象的范围,Default = [0, whole size]
*/
// .withinRange(0, 0)
/**
* 是否覆盖本地已有文件, Default = true;
*/
// .withCoverage(false)
/**
* 指定progress callback的间隔
*/
// .withProgressConfig(ProgressConfig.callbackWithPercent(10))
/**
* 配置读写流Buffer的大小, Default = 256 KB, MIN = 4 KB, MAX = 4 MB
*/
// .setBufferSize(4 << 20)
/**
* 配置进度监听
*/
.setOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(long bytesWritten, long contentLength) {
JLog.D(TAG, String.format("[progress] = %d%% - [%d/%d]", (int) (bytesWritten * 1.f / contentLength * 100), bytesWritten, contentLength));
}
})
.setReferer("input your referer")
.execute();
JLog.D(TAG, String.format("[res] = %s", (response == null ? "null" : response.toString())));
} catch (UfileClientException e) {
e.printStackTrace();
} catch (UfileServerException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public class DownloadFileApi extends UfileObjectApi<DownloadFileBean> {
* 流读取的buffer大小,Default = 256 KB
*/
private int bufferSize = UfileConstants.DEFAULT_BUFFER_SIZE;
/**
* Optional
* referer in header
*/
private String referer;

/**
* 构造方法
Expand Down Expand Up @@ -220,6 +225,17 @@ public DownloadFileApi setBufferSize(int bufferSize) {
return this;
}

/**
* set referer in header
*
* @param referer referer
* @return {@link DownloadFileApi}
*/
public DownloadFileApi setReferer(String referer) {
this.referer = referer;
return this;
}

@Override
protected void prepareData() throws UfileClientException {
parameterValidat();
Expand Down Expand Up @@ -294,7 +310,8 @@ protected void prepareData() throws UfileClientException {
JLog.E(TAG, "[range]:" + String.format("bytes=%d-%d", start, end));
GetRequestBuilder builder = (GetRequestBuilder) new GetRequestBuilder()
.baseUrl(host)
.addHeader("Range", String.format("bytes=%d-%d", start, end));
.addHeader("Range", String.format("bytes=%d-%d", start, end))
.addHeader("Referer", referer);
builder.setConnTimeOut(connTimeOut).setReadTimeOut(readTimeOut).setWriteTimeOut(writeTimeOut);
callList.add(new DownloadCallable(builder.build(httpClient.getOkHttpClient()), i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.ucloud.ufile.auth.ObjectAuthorizer;
import cn.ucloud.ufile.bean.DownloadFileBean;
import cn.ucloud.ufile.bean.UfileErrorBean;
import cn.ucloud.ufile.exception.UfileClientException;
import cn.ucloud.ufile.exception.UfileIOException;
import cn.ucloud.ufile.exception.UfileParamException;
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
Expand Down Expand Up @@ -62,6 +63,11 @@ public class GetFileApi extends UfileObjectApi<DownloadFileBean> {
* 流读取的buffer大小,Default = 256 KB
*/
private int bufferSize = UfileConstants.DEFAULT_BUFFER_SIZE;
/**
* Optional
* referer in header
*/
private String referer;

/**
* 构造方法
Expand Down Expand Up @@ -136,6 +142,17 @@ public GetFileApi setBufferSize(int bufferSize) {
return this;
}

/**
* set referer in header
*
* @param referer referer
* @return {@link GetFileApi}
*/
public GetFileApi setReferer(String referer) {
this.referer = referer;
return this;
}

@Override
protected void prepareData() throws UfileParamException {
parameterValidat();
Expand All @@ -145,6 +162,7 @@ protected void prepareData() throws UfileParamException {
.setConnTimeOut(connTimeOut).setReadTimeOut(readTimeOut).setWriteTimeOut(writeTimeOut)
.baseUrl(host)
.addHeader("Range", String.format("bytes=%d-%s", rangeStart, rangeEnd == 0 ? "" : rangeEnd))
.addHeader("Referer", referer)
.build(httpClient.getOkHttpClient());
}

Expand Down