Просмотр исходного кода

完成aliyunoss简单上传和下载

shuzheng 9 лет назад
Родитель
Сommit
6ba83e5e42

+ 3 - 3
zheng-oss/zheng-oss-sdk/src/main/resources/applicationContext-oss.xml

@@ -10,9 +10,9 @@
 
 	<!-- 阿里云OSS客户端 -->
 	<bean id="aliyunOssClient" class="com.aliyun.oss.OSSClient">
-		<constructor-arg value="${alipay.oss.endpoint}"/>
-		<constructor-arg value="${alipay.oss.accessKeyId}"/>
-		<constructor-arg value="${alipay.oss.accessKeySecret}"/>
+		<constructor-arg value="${aliyun.oss.endpoint}"/>
+		<constructor-arg value="${aliyun.oss.accessKeyId}"/>
+		<constructor-arg value="${aliyun.oss.accessKeySecret}"/>
 	</bean>
 
 </beans>

+ 40 - 7
zheng-oss/zheng-oss-web/src/main/java/com/zheng/oss/web/controller/DemoController.java

@@ -1,15 +1,19 @@
 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 org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.ModelMap;
 import org.springframework.web.bind.annotation.*;
 
-import java.io.ByteArrayInputStream;
+import java.io.*;
 
 /**
+ * oss测试
  * Created by shuzheng on 2017/4/18.
  */
 @RestController
@@ -19,13 +23,42 @@ public class DemoController {
     @Autowired
     private OSSClient aliyunOssClient;
 
-    @GetMapping("/aliyun/put")
-    public String index() {
-        for (int i = 0; i < 10; i++) {
-            PutObjectResult putObjectResult = aliyunOssClient.putObject("shuzheng", "test" + i, new ByteArrayInputStream(("Hello OSS" + i).getBytes()));
-            System.out.println(putObjectResult);
-        }
+    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()));
+        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);
         return "success";
     }
 
+    @GetMapping("/aliyun/download1")
+    public String download1() throws IOException {
+        StringBuffer result = new StringBuffer();
+        OSSObject ossObject = aliyunOssClient.getObject(bucketName, "text.txt");
+        InputStream content = ossObject.getObjectContent();
+        if (content != null) {
+            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
+            while (true) {
+                String line = reader.readLine();
+                if (line == null) break;
+                result.append("\n" + line);
+            }
+            content.close();
+        }
+        return result.toString();
+    }
+
+    @GetMapping("/aliyun/download2")
+    public String download2() throws IOException {
+        return "http://" + bucketName + "." + endPoint + "/file.png";
+    }
+
 }

+ 4 - 3
zheng-oss/zheng-oss-web/src/main/resources/zheng-oss-client.properties

@@ -1,4 +1,5 @@
 ### aliyun oss ###
-alipay.oss.endpoint=oss-cn-shanghai.aliyuncs.com
-alipay.oss.accessKeyId=LTAIf5dDIthJN3h0
-alipay.oss.accessKeySecret=2IZVHc1Qzxul8rC0ZQGKCEjjnpvm5d
+aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com
+aliyun.oss.accessKeyId=LTAIf5dDIthJN3h0
+aliyun.oss.accessKeySecret=2IZVHc1Qzxul8rC0ZQGKCEjjnpvm5d
+aliyun.oss.bucketName=shuzheng