|
@@ -10,6 +10,7 @@ import java.security.SignatureException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Base64;
|
|
import java.util.Base64;
|
|
import java.util.Calendar;
|
|
import java.util.Calendar;
|
|
|
|
+import java.util.Date;
|
|
import java.util.Locale;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.TimeZone;
|
|
import java.util.TimeZone;
|
|
@@ -20,6 +21,7 @@ import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.commons.lang3.time.DateUtils;
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
@@ -37,6 +39,7 @@ import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
|
+import cn.com.qmth.examcloud.commons.util.JsonUtil;
|
|
import cn.com.qmth.examcloud.commons.util.MD5;
|
|
import cn.com.qmth.examcloud.commons.util.MD5;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -71,6 +74,8 @@ public class UpYunClient {
|
|
|
|
|
|
private static final String MKDIR = "mkdir";
|
|
private static final String MKDIR = "mkdir";
|
|
|
|
|
|
|
|
+ private final String METHOD_POST = "POST";
|
|
|
|
+
|
|
private final String METHOD_PUT = "PUT";
|
|
private final String METHOD_PUT = "PUT";
|
|
|
|
|
|
private final String METHOD_DELETE = "DELETE";
|
|
private final String METHOD_DELETE = "DELETE";
|
|
@@ -117,50 +122,62 @@ public class UpYunClient {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 创建又拍云签名
|
|
|
|
|
|
+ * 创建又拍云请求
|
|
*
|
|
*
|
|
* @author WANGWEI
|
|
* @author WANGWEI
|
|
* @param filePath
|
|
* @param filePath
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public UpYunSign buildUpYunSign(String filePath) {
|
|
|
|
- return buildUpYunSign(filePath, "");
|
|
|
|
|
|
+ public UpYunHttpRequest buildUpYunHttpRequest(String filePath) {
|
|
|
|
+ return buildUpYunHttpRequest(filePath, "");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 创建又拍云签名
|
|
|
|
|
|
+ * 创建又拍云请求
|
|
*
|
|
*
|
|
* @author WANGWEI
|
|
* @author WANGWEI
|
|
* @param filePath
|
|
* @param filePath
|
|
* @param md5
|
|
* @param md5
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public UpYunSign buildUpYunSign(String filePath, String md5) {
|
|
|
|
- String path = formatPath(filePath);
|
|
|
|
- String uploadUrl = "https://" + API_DOMAIN + path;
|
|
|
|
-
|
|
|
|
- Map<String, String> headers = Maps.newHashMap();
|
|
|
|
|
|
+ public UpYunHttpRequest buildUpYunHttpRequest(String filePath, String md5) {
|
|
|
|
+ String path = filePath.trim();
|
|
|
|
+ if (!path.startsWith(SEPARATOR)) {
|
|
|
|
+ path = SEPARATOR + path;
|
|
|
|
+ }
|
|
|
|
+ String formUrl = "https://" + API_DOMAIN + "/" + this.bucketName;
|
|
String date = getDate();
|
|
String date = getDate();
|
|
|
|
+
|
|
|
|
+ Map<String, Object> policyArgs = Maps.newHashMap();
|
|
|
|
+ policyArgs.put("bucket", bucketName);
|
|
|
|
+ policyArgs.put("save-key", path);
|
|
|
|
+ Date expiration = DateUtils.addSeconds(new Date(), 120);
|
|
|
|
+ policyArgs.put("expiration", expiration.getTime() / 1000);
|
|
|
|
+ policyArgs.put("date", date);
|
|
|
|
+ policyArgs.put("content-md5", md5);
|
|
|
|
+
|
|
|
|
+ String json = JsonUtil.toJson(policyArgs);
|
|
|
|
+ String policy = Base64.getEncoder().encodeToString(json.getBytes());
|
|
|
|
+
|
|
|
|
+ Map<String, String> params = Maps.newHashMap();
|
|
|
|
+
|
|
String authorization = null;
|
|
String authorization = null;
|
|
try {
|
|
try {
|
|
md5 = null == md5 ? "" : md5;
|
|
md5 = null == md5 ? "" : md5;
|
|
- authorization = sign(userName, md5Password, METHOD_PUT, path, date, "", md5);
|
|
|
|
|
|
+ authorization = sign(userName, md5Password, METHOD_POST, path, date, policy, md5);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
throw new StatusException("100005", "[upyun]. fail to build sign", e);
|
|
throw new StatusException("100005", "[upyun]. fail to build sign", e);
|
|
}
|
|
}
|
|
- headers.put(AUTHORIZATION, authorization);
|
|
|
|
- headers.put(DATE, date);
|
|
|
|
- headers.put(MKDIR, "true");
|
|
|
|
- if (StringUtils.isNotBlank(md5)) {
|
|
|
|
- headers.put("Content-MD5", md5);
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ params.put("authorization", authorization);
|
|
|
|
+ params.put("policy", policy);
|
|
|
|
|
|
String accessUrl = this.domain + filePath;
|
|
String accessUrl = this.domain + filePath;
|
|
|
|
|
|
- UpYunSign sign = new UpYunSign();
|
|
|
|
- sign.setUploadUrl(uploadUrl);
|
|
|
|
|
|
+ UpYunHttpRequest sign = new UpYunHttpRequest();
|
|
sign.setAccessUrl(accessUrl);
|
|
sign.setAccessUrl(accessUrl);
|
|
- sign.setHeaders(headers);
|
|
|
|
|
|
+ sign.setFormParams(params);
|
|
|
|
+ sign.setFormUrl(formUrl);
|
|
|
|
|
|
return sign;
|
|
return sign;
|
|
}
|
|
}
|