Kaynağa Gözat

上传的问题

MengyangDuan 5 yıl önce
ebeveyn
işleme
4fd55b6c5c

+ 2 - 2
src/main/java/edu/nju/controller/AnalyzeController.java

@@ -178,7 +178,7 @@ public class AnalyzeController {
 		}
 	}
 
-	//存储单个bug的打分等级
+
 	@RequestMapping(value = "/saveSimiliar")
 	@ResponseBody
 	public void saveSimiliarGrade(String id, String grade,String similiarBug, HttpServletResponse response) {
@@ -196,7 +196,7 @@ public class AnalyzeController {
 		}
 	}
 
-	//存储单个bug的打分等级
+
 	@RequestMapping(value = "/saveByWorker")
 	@ResponseBody
 	public void saveGradeByWorker(String id,String worker_id, String grade, HttpServletResponse response) {

+ 195 - 0
src/main/java/edu/nju/util/OssAliyun.java

@@ -0,0 +1,195 @@
+package edu.nju.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+import com.aliyun.oss.ClientException;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.OSSException;
+import com.aliyun.oss.model.Bucket;
+import com.aliyun.oss.model.CannedAccessControlList;
+import com.aliyun.oss.model.CreateBucketRequest;
+import com.aliyun.oss.model.ListBucketsRequest;
+import com.aliyun.oss.model.OSSObject;
+import com.aliyun.oss.model.OSSObjectSummary;
+import com.aliyun.oss.model.ObjectAcl;
+import com.aliyun.oss.model.ObjectListing;
+import com.aliyun.oss.model.PutObjectRequest;
+
+/**
+ * This sample demonstrates how to get started with basic requests to Aliyun OSS
+ * using the OSS SDK for Java.
+ */
+public class OssAliyun {
+
+    private static String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
+    private static String accessKeyId = "IvS323TIcWUT57MG";
+    private static String accessKeySecret = "dYml7rvT8stQkoSjMYlfRTxNj9dEsI";
+    private static String bucketName = "mooctest-site";
+
+    public static void uploadFile(String objectName,File file) throws IOException {
+        /*
+         * Constructs a client instance with your account for accessing OSS
+         */
+        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+        try {
+            /*
+             * Determine whether the bucket exists
+             */
+            if (!ossClient.doesBucketExist(bucketName)) {
+                /*
+                 * Create a new OSS bucket
+                 */
+                System.out.println("Creating bucket " + bucketName + "\n");
+                ossClient.createBucket(bucketName);
+                CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
+                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
+                ossClient.createBucket(createBucketRequest);
+            }
+
+            /*
+             * Upload an object to your bucket
+             */
+            System.out.println("Uploading a new object to OSS from a file\n");
+            ossClient.putObject(new PutObjectRequest(bucketName, objectName, file));
+
+//            /*
+//             * Determine whether an object residents in your bucket
+//             */
+//            boolean exists = ossClient.doesObjectExist(bucketName,objectName);
+//            System.out.println("Does object " + bucketName + " exist? " + exists + "\n");
+//
+//            ossClient.setObjectAcl(bucketName, objectName, CannedAccessControlList.PublicRead);
+//            ossClient.setObjectAcl(bucketName, objectName, CannedAccessControlList.Default);
+//
+//            ObjectAcl objectAcl = ossClient.getObjectAcl(bucketName, objectName);
+//            System.out.println("ACL:" + objectAcl.getPermission().toString());
+//
+//            /*
+//             * Download an object from your bucket
+//             */
+//            System.out.println("Downloading an object");
+//            OSSObject object = ossClient.getObject(bucketName,objectName);
+//            System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
+//            displayTextInputStream(object.getObjectContent());
+//
+//            /*
+//             * List objects in your bucket by prefix
+//             */
+//            System.out.println("Listing objects");
+//            ObjectListing objectListing = ossClient.listObjects(bucketName, "My");
+//            for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
+//                System.out.println(" - " + objectSummary.getKey() + "  " +
+//                        "(size = " + objectSummary.getSize() + ")");
+//            }
+//            System.out.println();
+//
+//            /*
+//             * Delete an object
+//             */
+//            System.out.println("Deleting an object\n");
+//            ossClient.deleteObject(bucketName, objectName);
+
+        } catch (OSSException oe) {
+            System.out.println("Caught an OSSException, which means your request made it to OSS, "
+                    + "but was rejected with an error response for some reason.");
+            System.out.println("Error Message: " + oe.getErrorMessage());
+            System.out.println("Error Code:       " + oe.getErrorCode());
+            System.out.println("Request ID:      " + oe.getRequestId());
+            System.out.println("Host ID:           " + oe.getHostId());
+        } catch (ClientException ce) {
+            System.out.println("Caught an ClientException, which means the client encountered "
+                    + "a serious internal problem while trying to communicate with OSS, "
+                    + "such as not being able to access the network.");
+            System.out.println("Error Message: " + ce.getMessage());
+        } finally {
+            ossClient.shutdown();
+        }
+    }
+
+    public static boolean existFile(String objectName,File file) throws IOException {
+        /*
+         * Constructs a client instance with your account for accessing OSS
+         */
+        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+        try {
+            /*
+             * Determine whether the bucket exists
+             */
+            if (!ossClient.doesBucketExist(bucketName)) {
+                /*
+                 * Create a new OSS bucket
+                 */
+                System.out.println("Creating bucket " + bucketName + "\n");
+                ossClient.createBucket(bucketName);
+                CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
+                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
+                ossClient.createBucket(createBucketRequest);
+            }
+            /*
+             * Determine whether an object residents in your bucket
+             */
+            boolean exists = ossClient.doesObjectExist(bucketName,objectName);
+            System.out.println("Does object " + bucketName + " exist? " + exists + "\n");
+            return exists;
+
+        } catch (OSSException oe) {
+            System.out.println("Caught an OSSException, which means your request made it to OSS, "
+                    + "but was rejected with an error response for some reason.");
+            System.out.println("Error Message: " + oe.getErrorMessage());
+            System.out.println("Error Code:       " + oe.getErrorCode());
+            System.out.println("Request ID:      " + oe.getRequestId());
+            System.out.println("Host ID:           " + oe.getHostId());
+        } catch (ClientException ce) {
+            System.out.println("Caught an ClientException, which means the client encountered "
+                    + "a serious internal problem while trying to communicate with OSS, "
+                    + "such as not being able to access the network.");
+            System.out.println("Error Message: " + ce.getMessage());
+        } finally {
+            ossClient.shutdown();
+        }
+        return false;
+    }
+
+
+    private static void displayTextInputStream(InputStream input) throws IOException {
+        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
+        while (true) {
+            String line = reader.readLine();
+            if (line == null) break;
+
+            System.out.println("    " + line);
+        }
+        System.out.println();
+
+        reader.close();
+    }
+
+}