@@ -43,6 +43,11 @@ public class GenerateObjectPrivateUrlApi {
43
43
*/
44
44
private long expiresDuration ;
45
45
46
+ /**
47
+ * Ufile 文件下载链接所需的Content-Disposition: attachment文件名
48
+ */
49
+ private String attachmentFileName ;
50
+
46
51
/**
47
52
* 用户可选签名参数
48
53
*/
@@ -65,6 +70,25 @@ protected GenerateObjectPrivateUrlApi(ObjectAuthorizer authorizer, String host,
65
70
this .expiresDuration = expiresDuration ;
66
71
}
67
72
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
+
68
92
69
93
/**
70
94
* 配置签名可选参数
@@ -96,10 +120,21 @@ public String createUrl() throws UfileClientException {
96
120
GetRequestBuilder builder = (GetRequestBuilder ) new GetRequestBuilder ()
97
121
.baseUrl (generateFinalHost (bucketName , keyName ));
98
122
99
- return builder .addParam (new Parameter ("UCloudPublicKey" , authorizer .getPublicKey ()))
123
+ builder .addParam (new Parameter ("UCloudPublicKey" , authorizer .getPublicKey ()))
100
124
.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 ());
103
138
}
104
139
105
140
public interface CreatePrivateUrlCallback {
@@ -122,20 +157,7 @@ public void createUrlAsync(final CreatePrivateUrlCallback callback) {
122
157
@ Override
123
158
public void run () {
124
159
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 ();
139
161
if (callback != null )
140
162
callback .onSuccess (url );
141
163
} catch (UfileClientException e ) {
@@ -154,8 +176,8 @@ private String generateFinalHost(String bucketName, String keyName) throws Ufile
154
176
return String .format ("%s/%s" , host , keyName );
155
177
156
178
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" );
159
181
return String .format ("http://%s.%s/%s" , bucketName , host , keyName );
160
182
} catch (UnsupportedEncodingException e ) {
161
183
throw new UfileClientException ("Occur error during URLEncode bucketName and keyName" );
0 commit comments