1. 程式人生 > >七牛雲圖片儲存---Java

七牛雲圖片儲存---Java

一、新建儲存空間

  到七牛雲官網註冊一個賬號  

 

    新建一個儲存空間

 

   到個人中心獲取祕鑰

 

 

 二、新建Java專案

1、pom.xml配置

     <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.10</
version> </dependency> <dependency> <groupId>com.qiniu</groupId> <artifactId>qiniu-java-sdk</artifactId> <version>[7.2.0, 7.2.99]</version> </dependency>

 

QiniuCloudUtil

package
org.zyu.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import com.google.gson.Gson; import com.qiniu.common.QiniuException; import com.qiniu.common.Zone; import com.qiniu.http.Response; import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet; import com.qiniu.util.Auth; import com.qiniu.util.Base64; import com.qiniu.util.StringMap; import com.qiniu.util.UrlSafeBase64; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; public class QiniuCloudUtil { // 設定需要操作的賬號的AK和SK private static final String ACCESS_KEY = ""; private static final String SECRET_KEY = ""; // 要上傳的空間 private static final String bucketname = "image"; // 金鑰 private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); // 普通上傳 public void upload(String filePath, String fileName) throws IOException { Configuration cfg = new Configuration(Zone.zone0()); // zong1() 代表華北地區 UploadManager uploadManager = new UploadManager(cfg); Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); String upToken = auth.uploadToken(bucketname); try { Response response = uploadManager.put(filePath, fileName, upToken); // 解析上傳成功的結果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); System.out.println(putRet.key); System.out.println(putRet.hash); } catch (QiniuException ex) { Response r = ex.response; System.err.println(r.toString()); try { System.err.println(r.bodyString()); } catch (QiniuException ex2) { // ignore } } } // base64方式上傳 public void put64image(String filePath, String fileName) throws Exception { FileInputStream fis = null; int l = (int) (new File(filePath).length()); byte[] src = new byte[l]; fis = new FileInputStream(new File(filePath)); fis.read(src); String file64 = Base64.encodeToString(src, 0); String url = "http://upload.qiniu.com/putb64/" + l + "/key/" + UrlSafeBase64.encodeToString(fileName); RequestBody rb = RequestBody.create(null, file64); Request request = new Request.Builder().url(url).addHeader("Content-Type", "application/octet-stream") .addHeader("Authorization", "UpToken " + getUpToken()).post(rb).build(); System.out.println(request.headers()); OkHttpClient client = new OkHttpClient(); okhttp3.Response response = client.newCall(request).execute(); System.out.println(response); } public String getUpToken() { return auth.uploadToken(bucketname, null, 3600, new StringMap().put("insertOnly", 1)); } }

 

 

 

package org.zyu.controller;


import org.zyu.utils.QiniuCloudUtil;


public class QiniuController {

    public static void main(String[] args) {

        QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();

        try {
            qiniuUtil.put64image("C:\\Users\\Administrator\\Desktop\\share_img.png", "ces5.jpg");
            System.out.println("上傳成功");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    
}