diff --git a/zheng-oss/zheng-oss-sdk/src/main/java/com/zheng/oss/common/constant/OssConstant.java b/zheng-oss/zheng-oss-sdk/src/main/java/com/zheng/oss/common/constant/OssConstant.java
index 466c6fe1..f4e03e77 100644
--- a/zheng-oss/zheng-oss-sdk/src/main/java/com/zheng/oss/common/constant/OssConstant.java
+++ b/zheng-oss/zheng-oss-sdk/src/main/java/com/zheng/oss/common/constant/OssConstant.java
@@ -1,6 +1,7 @@
package com.zheng.oss.common.constant;
import com.zheng.common.base.BaseConstants;
+import com.zheng.common.util.PropertiesFileUtil;
/**
* oss系统常量类
@@ -8,4 +9,16 @@ import com.zheng.common.base.BaseConstants;
*/
public class OssConstant extends BaseConstants {
+ // endpoint
+ public static final String ALIYUN_OSS_ENDPOINT = PropertiesFileUtil.getInstance("config").get("aliyun.oss.endpoint");
+
+ // bucketName
+ public static final String ALIYUN_OSS_BUCKET_NAME = PropertiesFileUtil.getInstance("config").get("aliyun.oss.bucketName");
+
+ // 文件大小
+ public static final int ALIYUN_OSS_MAX_SIZE = PropertiesFileUtil.getInstance("config").getInt("aliyun.oss.maxSize");
+
+ // 签名有效期(单位:分钟)
+ public static final int ALIYUN_OSS_EXPIRE = PropertiesFileUtil.getInstance("config").getInt("aliyun.oss.policy.expire");
+
}
diff --git a/zheng-oss/zheng-oss-sdk/src/main/resources/applicationContext-oss.xml b/zheng-oss/zheng-oss-sdk/src/main/resources/applicationContext-oss.xml
index 7b22d3e6..0cac8848 100644
--- a/zheng-oss/zheng-oss-sdk/src/main/resources/applicationContext-oss.xml
+++ b/zheng-oss/zheng-oss-sdk/src/main/resources/applicationContext-oss.xml
@@ -6,9 +6,9 @@
zheng-oss-sdk
-
+
-
+
diff --git a/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/AliyunOssController.java b/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/AliyunOssController.java
new file mode 100644
index 00000000..3636b9df
--- /dev/null
+++ b/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/AliyunOssController.java
@@ -0,0 +1,64 @@
+package com.zheng.oss.web.controller;
+
+import com.alibaba.fastjson.JSONObject;
+import com.zheng.oss.common.constant.OssResult;
+import com.zheng.oss.common.constant.OssResultConstant;
+import com.zheng.oss.web.service.AliyunOssService;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.converter.json.MappingJacksonValue;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * Created by ZhangShuzheng on 2017/5/15.
+ */
+@Controller
+@RequestMapping("/aliyun/oss")
+public class AliyunOssController {
+
+ private static Logger _log = LoggerFactory.getLogger(AliyunOssController.class);
+
+ @Autowired
+ private AliyunOssService aliyunOssService;
+
+ /**
+ * 签名生成
+ * @param callback 跨域请求
+ * @return
+ */
+ @GetMapping("/policy")
+ @ResponseBody
+ //@CrossOrigin(origins = "*", methods = RequestMethod.GET) // 该注解不支持JDK1.7
+ public Object policy(@RequestParam(required = false) String callback) {
+ JSONObject result = aliyunOssService.policy();
+ if (StringUtils.isBlank(callback)) {
+ return result;
+ }
+ MappingJacksonValue jsonp = new MappingJacksonValue(result);
+ jsonp.setJsonpFunction(callback);
+ return jsonp;
+ }
+
+ /**
+ * 上传成功回调方法
+ * @param request
+ * @return
+ */
+ @PostMapping("callback")
+ @ResponseBody
+ public Object callback(HttpServletRequest request) {
+ JSONObject data = new JSONObject();
+ data.put("filename", request.getParameter("filename"));
+ data.put("size", request.getParameter("size"));
+ data.put("mimeType", request.getParameter("mimeType"));
+ data.put("width", request.getParameter("width"));
+ data.put("height", request.getParameter("height"));
+ return new OssResult(OssResultConstant.SUCCESS, data);
+ }
+
+}
diff --git a/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/DemoController.java b/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/DemoController.java
index e2978f6e..1b380cd6 100644
--- a/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/DemoController.java
+++ b/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/DemoController.java
@@ -2,13 +2,14 @@ package com.zheng.oss.web.controller;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
-import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectResult;
-import com.zheng.common.util.PropertiesFileUtil;
+import com.zheng.oss.common.constant.OssConstant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.ModelMap;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
import java.io.*;
@@ -20,29 +21,28 @@ import java.io.*;
@RequestMapping("/demo")
public class DemoController {
+ private static Logger _log = LoggerFactory.getLogger(DemoController.class);
+
@Autowired
private OSSClient aliyunOssClient;
- private String endPoint = PropertiesFileUtil.getInstance("zheng-oss-client").get("aliyun.oss.endpoint");
- private String bucketName = PropertiesFileUtil.getInstance("zheng-oss-client").get("aliyun.oss.bucketName");
-
@GetMapping("/aliyun/upload1")
public String upload1() {
- PutObjectResult putObjectResult = aliyunOssClient.putObject(bucketName, "text.txt", new ByteArrayInputStream("Hello OSS".getBytes()));
+ PutObjectResult putObjectResult = aliyunOssClient.putObject(OssConstant.ALIYUN_OSS_BUCKET_NAME, "text.txt", new ByteArrayInputStream("Hello OSS".getBytes()));
return "success";
}
@GetMapping("/aliyun/upload2")
public String upload2() throws FileNotFoundException {
- File file = new File("C:\\Users\\shuzheng\\Documents\\zheng.png");
- PutObjectResult putObjectResult = aliyunOssClient.putObject(bucketName, "file.png", file);
+ File file = new File("d:\\zheng.png");
+ PutObjectResult putObjectResult = aliyunOssClient.putObject(OssConstant.ALIYUN_OSS_BUCKET_NAME, "file.png", file);
return "success";
}
@GetMapping("/aliyun/download1")
public String download1() throws IOException {
StringBuffer result = new StringBuffer();
- OSSObject ossObject = aliyunOssClient.getObject(bucketName, "text.txt");
+ OSSObject ossObject = aliyunOssClient.getObject(OssConstant.ALIYUN_OSS_BUCKET_NAME, "text.txt");
InputStream content = ossObject.getObjectContent();
if (content != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
@@ -58,7 +58,7 @@ public class DemoController {
@GetMapping("/aliyun/download2")
public String download2() throws IOException {
- return "http://" + bucketName + "." + endPoint + "/file.png";
+ return "http://" + OssConstant.ALIYUN_OSS_BUCKET_NAME + "." + OssConstant.ALIYUN_OSS_ENDPOINT + "/file.png";
}
}
diff --git a/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/service/AliyunOssService.java b/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/service/AliyunOssService.java
new file mode 100644
index 00000000..aa342f59
--- /dev/null
+++ b/zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/service/AliyunOssService.java
@@ -0,0 +1,68 @@
+package com.zheng.oss.web.service;
+
+import com.alibaba.fastjson.JSONObject;
+import com.aliyun.oss.OSSClient;
+import com.aliyun.oss.common.utils.BinaryUtil;
+import com.aliyun.oss.model.MatchMode;
+import com.aliyun.oss.model.PolicyConditions;
+import com.zheng.oss.common.constant.OssConstant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * Created by ZhangShuzheng on 2017/5/15.
+ */
+@Service
+public class AliyunOssService {
+
+ private static Logger _log = LoggerFactory.getLogger(AliyunOssService.class);
+
+ @Autowired
+ private OSSClient aliyunOssClient;
+
+ /**
+ * 签名生成
+ * @return
+ */
+ public JSONObject policy() {
+ JSONObject result = new JSONObject();
+ // 存储目录
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+ String dir = sdf.format(new Date());
+ // 签名有效期
+ long expireEndTime = System.currentTimeMillis() + OssConstant.ALIYUN_OSS_EXPIRE * 1000;
+ Date expiration = new Date(expireEndTime);
+ // 文件大小
+ long maxSize = OssConstant.ALIYUN_OSS_MAX_SIZE * 1024 * 1024;
+ // 回调
+ JSONObject callback = new JSONObject();
+ callback.put("callbackUrl", "http://shuzheng.tunnel.qydev.com/aliyun/oss/callback");
+ callback.put("callbackBody", "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
+ callback.put("callbackBodyType", "application/x-www-form-urlencoded");
+ try {
+ PolicyConditions policyConds = new PolicyConditions();
+ policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, maxSize);
+ policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
+ String postPolicy = aliyunOssClient.generatePostPolicy(expiration, policyConds);
+ byte[] binaryData = postPolicy.getBytes("utf-8");
+ String policy = BinaryUtil.toBase64String(binaryData);
+ String signature = aliyunOssClient.calculatePostSignature(postPolicy);
+ String callbackData = BinaryUtil.toBase64String(callback.toString().getBytes("utf-8"));
+ // 返回结果
+ result.put("OSSAccessKeyId", aliyunOssClient.getCredentialsProvider().getCredentials().getAccessKeyId());
+ result.put("policy", policy);
+ result.put("signature", signature);
+ result.put("dir", dir);
+ result.put("callback", callbackData);
+ } catch (Exception e) {
+ _log.error("签名生成失败", e);
+ }
+ return result;
+ }
+
+}
diff --git a/zheng-oss/zheng-oss-web/src/main/resources/config.properties b/zheng-oss/zheng-oss-web/src/main/resources/config.properties
index ac1847b2..82e53bfb 100644
--- a/zheng-oss/zheng-oss-web/src/main/resources/config.properties
+++ b/zheng-oss/zheng-oss-web/src/main/resources/config.properties
@@ -1 +1,10 @@
-env=${profile.env}
\ No newline at end of file
+env=${profile.env}
+
+### aliyun oss ###
+aliyun.oss.endpoint=${aliyun.oss.endpoint}
+aliyun.oss.endpoint.internal=${aliyun.oss.endpoint.internal}
+aliyun.oss.accessKeyId=${aliyun.oss.accessKeyId}
+aliyun.oss.accessKeySecret=${aliyun.oss.accessKeySecret}
+aliyun.oss.bucketName=${aliyun.oss.bucketName}
+aliyun.oss.policy.expire=${aliyun.oss.policy.expire}
+aliyun.oss.maxSize=${aliyun.oss.maxSize}
\ No newline at end of file
diff --git a/zheng-oss/zheng-oss-web/src/main/resources/profiles/dev.properties b/zheng-oss/zheng-oss-web/src/main/resources/profiles/dev.properties
index b802e516..1f1a8299 100644
--- a/zheng-oss/zheng-oss-web/src/main/resources/profiles/dev.properties
+++ b/zheng-oss/zheng-oss-web/src/main/resources/profiles/dev.properties
@@ -1,5 +1,10 @@
profile.env=dev
-### aliyun ###
-aliyun.oss.endpoint=http://shuzheng.oss-cn-shanghai.aliyuncs.com
-aliyun.oss.endpoint.internal=http://shuzheng.oss-cn-shanghai-internal.aliyuncs.com
+### aliyun oss ###
+aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
+aliyun.oss.endpoint.internal=oss-cn-shanghai-internal.aliyuncs.com
+aliyun.oss.accessKeyId=
+aliyun.oss.accessKeySecret=
+aliyun.oss.bucketName=shuzheng
+aliyun.oss.policy.expire=30
+aliyun.oss.maxSize=10
\ No newline at end of file
diff --git a/zheng-oss/zheng-oss-web/src/main/resources/profiles/prod.properties b/zheng-oss/zheng-oss-web/src/main/resources/profiles/prod.properties
index bb7a8fdf..27e0a785 100644
--- a/zheng-oss/zheng-oss-web/src/main/resources/profiles/prod.properties
+++ b/zheng-oss/zheng-oss-web/src/main/resources/profiles/prod.properties
@@ -1 +1,10 @@
-profile.env=prod
\ No newline at end of file
+profile.env=prod
+
+### aliyun oss ###
+aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
+aliyun.oss.endpoint.internal=oss-cn-shanghai-internal.aliyuncs.com
+aliyun.oss.accessKeyId=
+aliyun.oss.accessKeySecret=
+aliyun.oss.bucketName=shuzheng_prod
+aliyun.oss.policy.expire=30
+aliyun.oss.maxSize=10
\ No newline at end of file
diff --git a/zheng-oss/zheng-oss-web/src/main/resources/profiles/test.properties b/zheng-oss/zheng-oss-web/src/main/resources/profiles/test.properties
index 4ff006f4..ff4f6ca1 100644
--- a/zheng-oss/zheng-oss-web/src/main/resources/profiles/test.properties
+++ b/zheng-oss/zheng-oss-web/src/main/resources/profiles/test.properties
@@ -1 +1,10 @@
-profile.env=test
\ No newline at end of file
+profile.env=test
+
+### aliyun oss ###
+aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
+aliyun.oss.endpoint.internal=oss-cn-shanghai-internal.aliyuncs.com
+aliyun.oss.accessKeyId=
+aliyun.oss.accessKeySecret=
+aliyun.oss.bucketName=shuzheng_test
+aliyun.oss.policy.expire=30
+aliyun.oss.maxSize=10
\ No newline at end of file
diff --git a/zheng-oss/zheng-oss-web/src/main/resources/zheng-oss-client.properties b/zheng-oss/zheng-oss-web/src/main/resources/zheng-oss-client.properties
deleted file mode 100644
index 48ec6e8d..00000000
--- a/zheng-oss/zheng-oss-web/src/main/resources/zheng-oss-client.properties
+++ /dev/null
@@ -1,5 +0,0 @@
-### aliyun oss ###
-aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
-aliyun.oss.accessKeyId=LTAIf5dDIthJN3h0
-aliyun.oss.accessKeySecret=2IZVHc1Qzxul8rC0ZQGKCEjjnpvm5d
-aliyun.oss.bucketName=shuzheng
\ No newline at end of file