Skip to content

Commit c136f3a

Browse files
author
Joshua Yin
committed
* 生成下载链接的API加入配置Content-Disposition的filename
1 parent 50542ad commit c136f3a

File tree

3 files changed

+88
-20
lines changed

3 files changed

+88
-20
lines changed

ufile-sample-java/src/main/java/cn/ucloud/ufile/sample/object/GetObjectSample.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public static void main(String[] args) {
4343
try {
4444
String url = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
4545
.getDownloadUrlFromPrivateBucket(keyName, bucketName, expiresDuration)
46+
/**
47+
* 使用Content-Disposition: attachment,并且默认文件名为KeyName
48+
*/
49+
// .withAttachment()
50+
/**
51+
* 使用Content-Disposition: attachment,并且配置文件名
52+
*/
53+
// .withAttachment("filename")
4654
.createUrl();
4755
getStream(url, localDir, saveName);
4856
} catch (UfileParamException e) {

ufile/ufile-client-java/src/main/java/cn/ucloud/ufile/api/object/GenerateObjectPrivateUrlApi.java

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public class GenerateObjectPrivateUrlApi {
4343
*/
4444
private long expiresDuration;
4545

46+
/**
47+
* Ufile 文件下载链接所需的Content-Disposition: attachment文件名
48+
*/
49+
private String attachmentFileName;
50+
4651
/**
4752
* 用户可选签名参数
4853
*/
@@ -65,6 +70,25 @@ protected GenerateObjectPrivateUrlApi(ObjectAuthorizer authorizer, String host,
6570
this.expiresDuration = expiresDuration;
6671
}
6772

73+
/**
74+
* 使用Content-Disposition: attachment,并配置attachment的文件名
75+
* @param attachmentFileName Content-Disposition: attachment的文件名
76+
* @return
77+
*/
78+
public GenerateObjectPrivateUrlApi withAttachment(String attachmentFileName) {
79+
this.attachmentFileName = attachmentFileName;
80+
return this;
81+
}
82+
83+
/**
84+
* 使用Content-Disposition: attachment,并且文件名默认为keyName
85+
* @return
86+
*/
87+
public GenerateObjectPrivateUrlApi withAttachment() {
88+
this.attachmentFileName = keyName;
89+
return this;
90+
}
91+
6892

6993
/**
7094
* 配置签名可选参数
@@ -96,10 +120,21 @@ public String createUrl() throws UfileClientException {
96120
GetRequestBuilder builder = (GetRequestBuilder) new GetRequestBuilder()
97121
.baseUrl(generateFinalHost(bucketName, keyName));
98122

99-
return builder.addParam(new Parameter("UCloudPublicKey", authorizer.getPublicKey()))
123+
builder.addParam(new Parameter("UCloudPublicKey", authorizer.getPublicKey()))
100124
.addParam(new Parameter("Signature", signature))
101-
.addParam(new Parameter("Expires", String.valueOf(expiresTime)))
102-
.generateGetUrl(builder.getBaseUrl(), builder.getParams());
125+
.addParam(new Parameter("Expires", String.valueOf(expiresTime)));
126+
127+
if (attachmentFileName != null && !attachmentFileName.isEmpty()) {
128+
try {
129+
attachmentFileName = URLEncoder.encode(attachmentFileName, "UTF-8").replace("+", "%20");
130+
builder.addParam(new Parameter("ufileattname", attachmentFileName));
131+
} catch (UnsupportedEncodingException e) {
132+
throw new UfileClientException("Occur error during URLEncode attachmentFileName", e);
133+
}
134+
}
135+
136+
137+
return builder.generateGetUrl(builder.getBaseUrl(), builder.getParams());
103138
}
104139

105140
public interface CreatePrivateUrlCallback {
@@ -122,20 +157,7 @@ public void createUrlAsync(final CreatePrivateUrlCallback callback) {
122157
@Override
123158
public void run() {
124159
try {
125-
parameterValidat();
126-
long expiresTime = System.currentTimeMillis() / 1000 + expiresDuration;
127-
128-
String signature = authorizer.authorizePrivateUrl(
129-
(ObjectDownloadAuthParam) new ObjectDownloadAuthParam(HttpMethod.GET, bucketName, keyName, expiresTime)
130-
.setOptional(authOptionalData));
131-
132-
GetRequestBuilder builder = (GetRequestBuilder) new GetRequestBuilder()
133-
.baseUrl(generateFinalHost(bucketName, keyName));
134-
135-
String url = builder.addParam(new Parameter("UCloudPublicKey", authorizer.getPublicKey()))
136-
.addParam(new Parameter("Signature", signature))
137-
.addParam(new Parameter("Expires", String.valueOf(expiresTime)))
138-
.generateGetUrl(builder.getBaseUrl(), builder.getParams());
160+
String url = createUrl();
139161
if (callback != null)
140162
callback.onSuccess(url);
141163
} catch (UfileClientException e) {
@@ -154,8 +176,8 @@ private String generateFinalHost(String bucketName, String keyName) throws Ufile
154176
return String.format("%s/%s", host, keyName);
155177

156178
try {
157-
bucketName = URLEncoder.encode(bucketName, "UTF-8").replace("+","%20");
158-
keyName = URLEncoder.encode(keyName, "UTF-8").replace("+","%20");
179+
bucketName = URLEncoder.encode(bucketName, "UTF-8").replace("+", "%20");
180+
keyName = URLEncoder.encode(keyName, "UTF-8").replace("+", "%20");
159181
return String.format("http://%s.%s/%s", bucketName, host, keyName);
160182
} catch (UnsupportedEncodingException e) {
161183
throw new UfileClientException("Occur error during URLEncode bucketName and keyName");

ufile/ufile-client-java/src/main/java/cn/ucloud/ufile/api/object/GenerateObjectPublicUrlApi.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import cn.ucloud.ufile.exception.UfileClientException;
44
import cn.ucloud.ufile.exception.UfileParamException;
55
import cn.ucloud.ufile.exception.UfileRequiredParamNotFoundException;
6+
import cn.ucloud.ufile.http.request.GetRequestBuilder;
7+
import cn.ucloud.ufile.util.Parameter;
68

79
import java.io.UnsupportedEncodingException;
810
import java.net.URLEncoder;
@@ -27,6 +29,11 @@ public class GenerateObjectPublicUrlApi {
2729
*/
2830
private String bucketName;
2931

32+
/**
33+
* Ufile 文件下载链接所需的Content-Disposition: attachment文件名
34+
*/
35+
private String attachmentFileName;
36+
3037
/**
3138
* 构造方法
3239
*
@@ -40,6 +47,25 @@ protected GenerateObjectPublicUrlApi(String host, String keyName, String bucketN
4047
this.bucketName = bucketName;
4148
}
4249

50+
/**
51+
* 使用Content-Disposition: attachment,并配置attachment的文件名
52+
* @param attachmentFileName Content-Disposition: attachment的文件名
53+
* @return
54+
*/
55+
public GenerateObjectPublicUrlApi withAttachment(String attachmentFileName) {
56+
this.attachmentFileName = attachmentFileName;
57+
return this;
58+
}
59+
60+
/**
61+
* 使用Content-Disposition: attachment,并且文件名默认为keyName
62+
* @return
63+
*/
64+
public GenerateObjectPublicUrlApi withAttachment() {
65+
this.attachmentFileName = keyName;
66+
return this;
67+
}
68+
4369
/**
4470
* 生成下载URL
4571
*
@@ -48,7 +74,19 @@ protected GenerateObjectPublicUrlApi(String host, String keyName, String bucketN
4874
*/
4975
public String createUrl() throws UfileClientException {
5076
parameterValidat();
51-
return generateFinalHost(bucketName, keyName);
77+
GetRequestBuilder builder = (GetRequestBuilder) new GetRequestBuilder()
78+
.baseUrl(generateFinalHost(bucketName, keyName));
79+
80+
if (attachmentFileName != null && !attachmentFileName.isEmpty()) {
81+
try {
82+
attachmentFileName = URLEncoder.encode(attachmentFileName, "UTF-8").replace("+", "%20");
83+
builder.addParam(new Parameter("ufileattname", attachmentFileName));
84+
} catch (UnsupportedEncodingException e) {
85+
throw new UfileClientException("Occur error during URLEncode attachmentFileName", e);
86+
}
87+
}
88+
89+
return builder.generateGetUrl(builder.getBaseUrl(), builder.getParams());
5290
}
5391

5492
private String generateFinalHost(String bucketName, String keyName) throws UfileClientException {

0 commit comments

Comments
 (0)