Parcourir la source

增加web演示

shuzheng il y a 9 ans
Parent
commit
4e297ea584

+ 55 - 0
zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/QiniuDemo.java

@@ -0,0 +1,55 @@
+package com.zheng.oss.web;
+
+import com.qiniu.common.QiniuException;
+import com.qiniu.http.Response;
+import com.qiniu.storage.UploadManager;
+import com.qiniu.util.Auth;
+
+import java.io.IOException;
+
+/**
+ * Created by shuzheng on 2016/11/12.
+ */
+public class QiniuDemo {
+	//设置好账号的ACCESS_KEY和SECRET_KEY
+	String ACCESS_KEY = "";
+	String SECRET_KEY = "";
+	//要上传的空间
+	String bucketname = "zheng";
+	//上传到七牛后保存的文件名
+	String key = "my-java.png";
+	//上传文件的路径
+	String FilePath = "C:\\Users\\admin\\Pictures\\zsz\\20161108161228.png";
+
+	//密钥配置
+	Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
+	//创建上传对象
+	UploadManager uploadManager = new UploadManager();
+
+	//简单上传,使用默认策略,只需要设置上传的空间名就可以了
+	public String getUpToken(){
+		return auth.uploadToken(bucketname);
+	}
+
+	public void upload() throws IOException {
+		try {
+			//调用put方法上传
+			Response res = uploadManager.put(FilePath, key, getUpToken());
+			//打印返回的信息
+			System.out.println(res.bodyString());
+		} catch (QiniuException e) {
+			Response r = e.response;
+			// 请求失败时打印的异常的信息
+			System.out.println(r.toString());
+			try {
+				//响应的文本信息
+				System.out.println(r.bodyString());
+			} catch (QiniuException e1) {
+				//ignore
+			}
+		}
+	}
+	public static void main(String[] args) throws IOException {
+		new QiniuDemo().upload();
+	}
+}

+ 13 - 8
zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/AppController.java → zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/DemoController.java

@@ -1,7 +1,8 @@
-package com.zheng.oss.web.controller;
+package com.zheng.oss.web.controller;
 
 
 import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.OSSClient;
-import org.springframework.beans.factory.annotation.Value;
+import com.zheng.oss.rpc.api.OssApiService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.ModelMap;
 import org.springframework.ui.ModelMap;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -11,20 +12,23 @@ import org.springframework.web.bind.annotation.ResponseBody;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayInputStream;
 
 
 /**
 /**
- * Created by shuzheng on 2016/11/12.
+ * Created by shuzheng on 2017/4/18.
  */
  */
 @Controller
 @Controller
-@RequestMapping("/app")
-public class AppController {
+@RequestMapping("/demo")
+public class DemoController {
+
+    @Autowired
+    private OssApiService ossApiService;
 
 
     @RequestMapping(value = "/index", method = RequestMethod.GET)
     @RequestMapping(value = "/index", method = RequestMethod.GET)
     @ResponseBody
     @ResponseBody
     public String index(ModelMap modelMap) {
     public String index(ModelMap modelMap) {
         // endpoint以杭州为例,其它region请按实际情况填写
         // endpoint以杭州为例,其它region请按实际情况填写
-        String endpoint = "";
+        String endpoint = "oss-cn-shanghai.aliyuncs.com";
         // accessKey请登录https://ak-console.aliyun.com/#/查看
         // accessKey请登录https://ak-console.aliyun.com/#/查看
-        String accessKeyId = "";
-        String accessKeySecret = "";
+        String accessKeyId = "LTAIf5dDIthJN3h0";
+        String accessKeySecret = "2IZVHc1Qzxul8rC0ZQGKCEjjnpvm5d";
 
 
         // 创建OSSClient实例
         // 创建OSSClient实例
         OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
         OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
@@ -35,6 +39,7 @@ public class AppController {
 
 
         // 关闭ossClient
         // 关闭ossClient
         ossClient.shutdown();
         ossClient.shutdown();
+
         return "success";
         return "success";
     }
     }
 
 

+ 2 - 2
zheng-oss/zheng-oss-web/src/main/resources/profiles/dev.properties

@@ -1,5 +1,5 @@
 profile.env=dev
 profile.env=dev
 
 
 ### aliyun ###
 ### aliyun ###
-aliyun.oss.endpoint=
-aliyun.oss.endpoint.internal=
+aliyun.oss.endpoint=http://shuzheng.oss-cn-shanghai.aliyuncs.com
+aliyun.oss.endpoint.internal=http://shuzheng.oss-cn-shanghai-internal.aliyuncs.com