Skip to content

Commit 196cf71

Browse files
authored
Merge pull request #148 from MariaLiuSM/master
Update PostObjectSample.java
2 parents 69da853 + 928d9eb commit 196cf71

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/samples/PostObjectSample.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import javax.activation.MimetypesFileTypeMap;
2323
import javax.crypto.Mac;
2424
import javax.crypto.spec.SecretKeySpec;
25-
2625
import org.apache.commons.codec.binary.Base64;
27-
2826
import java.io.*;
2927
import java.net.HttpURLConnection;
3028
import java.net.URL;
@@ -53,94 +51,98 @@ public class PostObjectSample {
5351
// The key name for the file to upload.
5452
private String key = "<key>";
5553

56-
57-
private void PostObject() throws Exception {
54+
private void postObject() throws Exception {
5855
// append the 'bucketname.' prior to the domain, such as http://bucket1.oss-cn-hangzhou.aliyuncs.com.
59-
String urlStr = endpoint.replace("http://", "http://" + bucketName+ ".");
60-
56+
String urlStr = endpoint.replace("http://", "http://" + bucketName + ".");
57+
6158
// form fields
6259
Map<String, String> formFields = new LinkedHashMap<String, String>();
63-
60+
6461
// key
6562
formFields.put("key", this.key);
6663
// Content-Disposition
6764
formFields.put("Content-Disposition", "attachment;filename="
68-
+ localFilePath);
65+
+ localFilePath);
6966
// OSSAccessKeyId
7067
formFields.put("OSSAccessKeyId", accessKeyId);
7168
// policy
72-
String policy = "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, 104857600]]}";
69+
String policy
70+
= "{\"expiration\": \"2120-01-01T12:00:00.000Z\",\"conditions\": [[\"content-length-range\", 0, 104857600]]}";
7371
String encodePolicy = new String(Base64.encodeBase64(policy.getBytes()));
7472
formFields.put("policy", encodePolicy);
7573
// Signature
7674
String signaturecom = computeSignature(accessKeySecret, encodePolicy);
7775
formFields.put("Signature", signaturecom);
76+
// Set security token.
77+
formFields.put("x-oss-security-token", "<yourSecurityToken>");
7878

7979
String ret = formUpload(urlStr, formFields, localFilePath);
80-
80+
8181
System.out.println("Post Object [" + this.key + "] to bucket [" + bucketName + "]");
8282
System.out.println("post reponse:" + ret);
8383
}
84-
85-
private static String computeSignature(String accessKeySecret, String encodePolicy)
86-
throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
84+
85+
private static String computeSignature(String accessKeySecret, String encodePolicy)
86+
throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
8787
// convert to UTF-8
8888
byte[] key = accessKeySecret.getBytes("UTF-8");
8989
byte[] data = encodePolicy.getBytes("UTF-8");
90-
90+
9191
// hmac-sha1
9292
Mac mac = Mac.getInstance("HmacSHA1");
9393
mac.init(new SecretKeySpec(key, "HmacSHA1"));
9494
byte[] sha = mac.doFinal(data);
95-
95+
9696
// base64
9797
return new String(Base64.encodeBase64(sha));
9898
}
9999

100100
private static String formUpload(String urlStr, Map<String, String> formFields, String localFile)
101-
throws Exception {
101+
throws Exception {
102102
String res = "";
103103
HttpURLConnection conn = null;
104104
String boundary = "9431149156168";
105-
105+
106106
try {
107107
URL url = new URL(urlStr);
108-
conn = (HttpURLConnection) url.openConnection();
108+
conn = (HttpURLConnection)url.openConnection();
109109
conn.setConnectTimeout(5000);
110110
conn.setReadTimeout(30000);
111111
conn.setDoOutput(true);
112112
conn.setDoInput(true);
113113
conn.setRequestMethod("POST");
114-
conn.setRequestProperty("User-Agent",
115-
"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
114+
conn.setRequestProperty("User-Agent",
115+
"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
116+
// Set Content-MD5. The MD5 value is calculated based on the whole message body.
117+
conn.setRequestProperty("Content-MD5", "<yourContentMD5>");
116118
conn.setRequestProperty("Content-Type",
117-
"multipart/form-data; boundary=" + boundary);
119+
"multipart/form-data; boundary=" + boundary);
118120
OutputStream out = new DataOutputStream(conn.getOutputStream());
119-
121+
120122
// text
121123
if (formFields != null) {
122124
StringBuffer strBuf = new StringBuffer();
123125
Iterator<Entry<String, String>> iter = formFields.entrySet().iterator();
124126
int i = 0;
125-
127+
126128
while (iter.hasNext()) {
127129
Entry<String, String> entry = iter.next();
128130
String inputName = entry.getKey();
129131
String inputValue = entry.getValue();
130-
132+
131133
if (inputValue == null) {
132134
continue;
133135
}
134-
136+
135137
if (i == 0) {
136138
strBuf.append("--").append(boundary).append("\r\n");
137139
strBuf.append("Content-Disposition: form-data; name=\""
138-
+ inputName + "\"\r\n\r\n");
140+
+ inputName + "\"\r\n\r\n");
139141
strBuf.append(inputValue);
140142
} else {
141143
strBuf.append("\r\n").append("--").append(boundary).append("\r\n");
142144
strBuf.append("Content-Disposition: form-data; name=\""
143-
+ inputName + "\"\r\n\r\n");
145+
+ inputName + "\"\r\n\r\n");
144146
strBuf.append(inputValue);
145147
}
146148

@@ -159,9 +161,9 @@ private static String formUpload(String urlStr, Map<String, String> formFields,
159161

160162
StringBuffer strBuf = new StringBuffer();
161163
strBuf.append("\r\n").append("--").append(boundary)
162-
.append("\r\n");
164+
.append("\r\n");
163165
strBuf.append("Content-Disposition: form-data; name=\"file\"; "
164-
+ "filename=\"" + filename + "\"\r\n");
166+
+ "filename=\"" + filename + "\"\r\n");
165167
strBuf.append("Content-Type: " + contentType + "\r\n\r\n");
166168

167169
out.write(strBuf.toString().getBytes());
@@ -198,13 +200,13 @@ private static String formUpload(String urlStr, Map<String, String> formFields,
198200
conn = null;
199201
}
200202
}
201-
203+
202204
return res;
203205
}
204206

205207
public static void main(String[] args) throws Exception {
206208
PostObjectSample ossPostObject = new PostObjectSample();
207-
ossPostObject.PostObject();
209+
ossPostObject.postObject();
208210
}
209211

210212
}

0 commit comments

Comments
 (0)