Java tutorial
/** * Copyright (c) 2012 The Wiseserc. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ package com.aliyun.android.oss.task; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.ByteArrayEntity; import org.xmlpull.v1.XmlPullParserException; import android.util.Log; import com.aliyun.android.oss.OSSErrorCode; import com.aliyun.android.oss.OSSException; import com.aliyun.android.oss.http.HttpMethod; import com.aliyun.android.oss.http.OSSHttpTool; import com.aliyun.android.oss.model.OSSObject; import com.aliyun.android.oss.model.Part; import com.aliyun.android.oss.xmlparser.MultipartUploadCompleteXmlParser; import com.aliyun.android.oss.xmlserializer.PartsXmlSerializer; import com.aliyun.android.util.Helper; /** * ?MultipartUpload * ???Multipart Upload Complete Task??Multipart Upload * ????Part(part??ETAG)OSS??Part * ????PartPart??OSS?Part?? * Object * * @author Michael */ public class MultipartUploadCompleteTask extends Task { /** * Object?? */ private String objectKey; /** * uploadId */ private String uploadId; /** * ?Part */ private List<Part> partsList; /** * */ public MultipartUploadCompleteTask(String bucketName, String objectKey, String uploadId, List<Part> partsList) { super(HttpMethod.POST, bucketName); this.objectKey = objectKey; this.uploadId = uploadId; this.partsList = partsList; httpTool.setUploadId(uploadId); } /** * ??? */ @Override protected void checkArguments() { if (Helper.isEmptyString(bucketName) || Helper.isEmptyString(objectKey)) { throw new IllegalArgumentException("bucketName or objectKey not set"); } if (Helper.isEmptyString(uploadId)) { throw new IllegalArgumentException("uploadId not set"); } if (partsList == null) { throw new IllegalArgumentException("partsList cannot be null"); } } /** * ?partXML?? */ private String generateHttpEntity() { PartsXmlSerializer serializer = new PartsXmlSerializer("CompleteMultipartUpload"); return serializer.serialize(partsList); } /** * HttpPost * * @throws UnsupportedEncodingException */ protected HttpUriRequest generateHttpRequest() { String requestUri = this.getOSSEndPoint() + httpTool.generateCanonicalizedResource("/" + OSSHttpTool.encodeUri(objectKey)); HttpPost httpPost = new HttpPost(requestUri); // ?? byte[] data = generateHttpEntity().getBytes(); String resource = httpTool.generateCanonicalizedResource("/" + bucketName + "/" + objectKey); String dateStr = Helper.getGMTDate(); String authorization = OSSHttpTool.generateAuthorization(accessId, accessKey, httpMethod.toString(), "", "", dateStr, "", resource); httpPost.setHeader(AUTHORIZATION, authorization); httpPost.setHeader(DATE, dateStr); try { httpPost.setEntity(new ByteArrayEntity(data)); } catch (Exception e) { Log.e(this.getClass().getName(), e.getMessage()); } return httpPost; } /** * ????Object?bucket, objectKey?ETag * * @return * @throws OSSException */ public OSSObject getResult() throws OSSException { try { HttpResponse r = this.execute(); return new MultipartUploadCompleteXmlParser().parse(r.getEntity().getContent()); } catch (OSSException osse) { throw osse; } catch (XmlPullParserException xe) { OSSException ossException = new OSSException(xe); ossException.setErrorCode(OSSErrorCode.PARSE_XML_ERROR); throw ossException; } catch (IOException ioe) { OSSException ossException = new OSSException(ioe); ossException.setErrorCode(OSSErrorCode.GET_ENTITY_CONTENT_ERROR); throw ossException; // } catch (Exception e) { // throw new OSSException(e); } finally { this.releaseHttpClient(); } } public String getBucketName() { return bucketName; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } public String getObjectKey() { return objectKey; } public void setObjectKey(String objectKey) { this.objectKey = objectKey; } public String getUploadId() { return uploadId; } public void setUploadId(String uploadId) { this.uploadId = uploadId; } public List<Part> getPartsList() { return partsList; } public void setPartsList(List<Part> partsList) { this.partsList = partsList; } }