45
45
import com .tencent .cos .xml .utils .DigestUtils ;
46
46
import com .tencent .qcloud .core .http .RequestBodySerializer ;
47
47
import com .tencent .qcloud .core .logger .QCloudLogger ;
48
+ import com .tencent .qcloud .core .util .Base64Utils ;
48
49
import com .tencent .qcloud .core .util .DomainSwitchUtils ;
50
+ import com .tencent .qcloud .core .util .OkhttpInternalUtils ;
49
51
50
52
import org .junit .Assert ;
51
53
import org .junit .Test ;
61
63
import java .net .HttpURLConnection ;
62
64
import java .net .URL ;
63
65
import java .net .URLEncoder ;
66
+ import java .security .MessageDigest ;
67
+ import java .security .NoSuchAlgorithmException ;
68
+ import java .util .ArrayList ;
64
69
65
70
import okhttp3 .MediaType ;
66
71
import okhttp3 .OkHttpClient ;
@@ -173,24 +178,68 @@ public void onFail(CosXmlClientException exception, CosXmlServiceException servi
173
178
QCloudLogger .i ("QCloudTest" , url );
174
179
Assert .assertEquals ("https://bucket.cos.ap-shanghai.myqcloud.com/objectexample" , url );
175
180
}
176
-
181
+
182
+ public String onGetMd5 (String filePath ) throws IOException {
183
+ InputStream inputStream = null ;
184
+ try {
185
+ MessageDigest messageDigest = MessageDigest .getInstance ("MD5" );
186
+ File file = new File (filePath );
187
+ inputStream = new FileInputStream (file );
188
+ byte [] buff = new byte [8 * 1024 ];
189
+ int readLen ;
190
+ long remainLength = file .length ();
191
+ while (remainLength > 0L && (readLen = inputStream .read (buff , 0 ,
192
+ (buff .length > remainLength ? (int ) remainLength : buff .length )))!= -1 ){
193
+ messageDigest .update (buff , 0 , readLen );
194
+ remainLength -= readLen ;
195
+ }
196
+ return Base64Utils .encode (messageDigest .digest ());
197
+ } catch (IOException e ) {
198
+ throw e ;
199
+ } catch (NoSuchAlgorithmException e ) {
200
+ throw new IOException ("unSupport Md5 algorithm" , e );
201
+ } finally {
202
+ if (inputStream != null ) OkhttpInternalUtils .closeQuietly (inputStream );
203
+ }
204
+ }
205
+
177
206
@ Test public void testPresignedRequest () {
207
+ String filePath = TestUtils .smallFilePath ();
178
208
PresignedUrlRequest presignedUrlRequest = new PresignedUrlRequest (TestConst .PERSIST_BUCKET , TestConst .PERSIST_BUCKET_SMALL_OBJECT_PATH ) {
179
209
@ Override
180
210
public RequestBodySerializer getRequestBody () throws CosXmlClientException {
181
- return RequestBodySerializer .file ("image/png" , new File (TestUtils . smallFilePath () ));
211
+ return RequestBodySerializer .file ("image/png" , new File (filePath ));
182
212
}
183
213
};
184
214
presignedUrlRequest .setRequestMethod ("PUT" );
185
215
presignedUrlRequest .setSignKeyTime (3600 );
186
216
presignedUrlRequest .addNoSignHeader ("Host" );
217
+ String md5 ;
218
+ // try {
219
+ // md5 = DigestUtils.getMD5(filePath);
220
+ // } catch (CosXmlClientException e) {
221
+ // throw new RuntimeException(e);
222
+ // }
223
+
224
+ try {
225
+ md5 = onGetMd5 (filePath );
226
+ } catch (IOException e ) {
227
+ throw new RuntimeException (e );
228
+ }
229
+
230
+ // try {
231
+ // presignedUrlRequest.setRequestHeaders(HttpConstants.Header.CONTENT_MD5, md5);
232
+ // } catch (CosXmlClientException e) {
233
+ // throw new RuntimeException(e);
234
+ // }
187
235
CosXmlSimpleService defaultService = ServiceFactory .INSTANCE .newDefaultService ();
188
236
try {
189
237
String signUrl = defaultService .getPresignedURL (presignedUrlRequest );
190
238
QCloudLogger .i ("QCloudTest" , signUrl );
191
239
MediaType imageType = MediaType .parse ("image/png" );
192
240
Request request = new Request .Builder ()
193
241
.url (signUrl )
242
+ // .header(HttpConstants.Header.CONTENT_MD5, md5)
194
243
.put (RequestBody .create (imageType , new File (TestUtils .smallFilePath ())))
195
244
.build ();
196
245
Response response = new OkHttpClient ().newCall (request ).execute ();
@@ -424,4 +473,35 @@ public void testIsMyqcloudUrl(){
424
473
assertFalse (DomainSwitchUtils .isMyqcloudUrl (testUrls [5 ]));
425
474
assertFalse (DomainSwitchUtils .isMyqcloudUrl (testUrls [6 ]));
426
475
}
476
+
477
+ @ Test
478
+ public void testMultiThreadedCosXmlService () {
479
+ try {
480
+ // 线程数常量
481
+ int THREAD_COUNT = 10000 ;
482
+ Thread [] threads = new Thread [THREAD_COUNT ];
483
+ ArrayList <Throwable > exceptions = new ArrayList <>();
484
+ for (int i = 0 ; i < THREAD_COUNT ; i ++) {
485
+ threads [i ] = new Thread (() -> {
486
+ CosXmlSimpleService service = ServiceFactory .INSTANCE .newDefaultService ();
487
+ service .getConfig ();
488
+ });
489
+ threads [i ].setUncaughtExceptionHandler (new Thread .UncaughtExceptionHandler () {
490
+ @ Override
491
+ public void uncaughtException (Thread t , Throwable e ) {
492
+ e .printStackTrace ();
493
+ exceptions .add (e );
494
+ }
495
+ });
496
+ threads [i ].start ();
497
+ }
498
+ for (int i = 0 ; i < THREAD_COUNT ; i ++) {
499
+ threads [i ].join ();
500
+ }
501
+ assertTrue (exceptions .isEmpty ());
502
+ } catch (Exception e ) {
503
+ e .printStackTrace ();
504
+ Assert .fail (e .getMessage ());
505
+ }
506
+ }
427
507
}
0 commit comments