1. 程式人生 > >上傳圖片,視訊等檔案到七牛雲物件儲存上

上傳圖片,視訊等檔案到七牛雲物件儲存上

上傳圖片:

需要用到的引數:

1、AccessKey (在“個人中心”–>“祕鑰管理”中)

2、SecretKey (在“個人中心”–>“祕鑰管理”中)

3、儲存空間名字(自己建立的)

1、建立一個Maven專案

pom.xml

 <!--七牛雲物件儲存-->
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>7.2.11</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.3.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>happy-dns-java</artifactId>
            <version>0.1.4</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

建立test111.java檔案用來測試,名字自己隨意

package com.yu.admin.controller;

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.util.Auth;

import java.io.IOException;

public class test111 {
    private static int getRandom(int count) {
        return (int) Math.round(Math.random() * (count));
    }

    private static String string = "abcdefghijklmnopqrstuvwxyz";

    private static String getRandomString(int length) {
        StringBuffer sb = new StringBuffer();
        int len = string.length();
        for (int i = 0; i < length; i++) {
            sb.append(string.charAt(getRandom(len - 1)));
        }
        return sb.toString();
    }

    //構造一個帶指定Zone物件的配置類
    Configuration cfg = new Configuration(Zone.zone0());
    // 設定好賬號的ACCESS_KEY和SECRET_KEY
    String ACCESS_KEY = "Iyzn9uW3_9gSomViUnHl9S4cQ2wr0MH"; // 這兩個登入七牛
    // 賬號裡面可以找到  進入 個人面板->個人中心->金鑰管理可找到
    String SECRET_KEY = "x7FEFFBDcSqaQLpPMq2NKVXcooY0H";//

    // 要上傳的空間
    String bucketname = "images"; // 填寫新建的那個儲存空間物件的名稱
    // 上傳到七牛後儲存的檔名
    String key = getRandomString(10) + ".jpg";//七牛雲伺服器裡用來對應唯一上傳的檔案
    // String audioUrl = "在七牛雲中找到";
    // String imagesUrl = "在七牛雲中找到";
    // 上傳檔案的路徑
    String FilePath = "d:\\zz.jpg"; // 本地要上傳檔案路徑

    // 金鑰配置
    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    // 建立上傳物件

    UploadManager uploadManager = new UploadManager(cfg,null);

    // 簡單上傳,使用預設策略,只需要設定上傳的空間名就可以了 //
    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.isOK());

            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 test111().upload();
    }
}

執行結果: 在這裡插入圖片描述

由上圖可知,上傳後的檔名為:jllkctmpmc.jpg,然後我們去七牛雲上看是否存在剛才上傳的檔案,有則成功上傳,否則失敗。 在這裡插入圖片描述