@@ -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" );
0 commit comments