diff --git a/src/samples/PostObjectSample.java b/src/samples/PostObjectSample.java index 6e56fa93..fd378f46 100644 --- a/src/samples/PostObjectSample.java +++ b/src/samples/PostObjectSample.java @@ -22,9 +22,7 @@ import javax.activation.MimetypesFileTypeMap; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; - import org.apache.commons.codec.binary.Base64; - import java.io.*; import java.net.HttpURLConnection; import java.net.URL; @@ -53,94 +51,98 @@ public class PostObjectSample { // The key name for the file to upload. private String key = ""; - - private void PostObject() throws Exception { + private void postObject() throws Exception { // append the 'bucketname.' prior to the domain, such as http://bucket1.oss-cn-hangzhou.aliyuncs.com. - String urlStr = endpoint.replace("http://", "http://" + bucketName+ "."); - + String urlStr = endpoint.replace("http://", "http://" + bucketName + "."); + // form fields Map formFields = new LinkedHashMap(); - + // key formFields.put("key", this.key); // Content-Disposition formFields.put("Content-Disposition", "attachment;filename=" - + localFilePath); + + localFilePath); // OSSAccessKeyId formFields.put("OSSAccessKeyId", accessKeyId); // policy - String policy = "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, 104857600]]}"; + String policy + = "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, 104857600]]}"; String encodePolicy = new String(Base64.encodeBase64(policy.getBytes())); formFields.put("policy", encodePolicy); // Signature String signaturecom = computeSignature(accessKeySecret, encodePolicy); formFields.put("Signature", signaturecom); + // Set security token. + formFields.put("x-oss-security-token", ""); String ret = formUpload(urlStr, formFields, localFilePath); - + System.out.println("Post Object [" + this.key + "] to bucket [" + bucketName + "]"); System.out.println("post reponse:" + ret); } - - private static String computeSignature(String accessKeySecret, String encodePolicy) - throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { + + private static String computeSignature(String accessKeySecret, String encodePolicy) + throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { // convert to UTF-8 byte[] key = accessKeySecret.getBytes("UTF-8"); byte[] data = encodePolicy.getBytes("UTF-8"); - + // hmac-sha1 Mac mac = Mac.getInstance("HmacSHA1"); mac.init(new SecretKeySpec(key, "HmacSHA1")); byte[] sha = mac.doFinal(data); - + // base64 return new String(Base64.encodeBase64(sha)); } private static String formUpload(String urlStr, Map formFields, String localFile) - throws Exception { + throws Exception { String res = ""; HttpURLConnection conn = null; String boundary = "9431149156168"; - + try { URL url = new URL(urlStr); - conn = (HttpURLConnection) url.openConnection(); + conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(30000); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); - conn.setRequestProperty("User-Agent", - "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"); + conn.setRequestProperty("User-Agent", + "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"); + // Set Content-MD5. The MD5 value is calculated based on the whole message body. + conn.setRequestProperty("Content-MD5", ""); conn.setRequestProperty("Content-Type", - "multipart/form-data; boundary=" + boundary); + "multipart/form-data; boundary=" + boundary); OutputStream out = new DataOutputStream(conn.getOutputStream()); - + // text if (formFields != null) { StringBuffer strBuf = new StringBuffer(); Iterator> iter = formFields.entrySet().iterator(); int i = 0; - + while (iter.hasNext()) { Entry entry = iter.next(); String inputName = entry.getKey(); String inputValue = entry.getValue(); - + if (inputValue == null) { continue; } - + if (i == 0) { strBuf.append("--").append(boundary).append("\r\n"); strBuf.append("Content-Disposition: form-data; name=\"" - + inputName + "\"\r\n\r\n"); + + inputName + "\"\r\n\r\n"); strBuf.append(inputValue); } else { strBuf.append("\r\n").append("--").append(boundary).append("\r\n"); strBuf.append("Content-Disposition: form-data; name=\"" - + inputName + "\"\r\n\r\n"); + + inputName + "\"\r\n\r\n"); strBuf.append(inputValue); } @@ -159,9 +161,9 @@ private static String formUpload(String urlStr, Map formFields, StringBuffer strBuf = new StringBuffer(); strBuf.append("\r\n").append("--").append(boundary) - .append("\r\n"); + .append("\r\n"); strBuf.append("Content-Disposition: form-data; name=\"file\"; " - + "filename=\"" + filename + "\"\r\n"); + + "filename=\"" + filename + "\"\r\n"); strBuf.append("Content-Type: " + contentType + "\r\n\r\n"); out.write(strBuf.toString().getBytes()); @@ -198,13 +200,13 @@ private static String formUpload(String urlStr, Map formFields, conn = null; } } - + return res; } public static void main(String[] args) throws Exception { PostObjectSample ossPostObject = new PostObjectSample(); - ossPostObject.PostObject(); + ossPostObject.postObject(); } }