zoukankan      html  css  js  c++  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();
            }
    
        }
        
    }
  • 相关阅读:
    [题解]POJ_1417_(并查集背包
    [题解]POJ_2912_(带权并查集
    [题解]luogu_P1502_窗口的星星(扫描线
    [题解/模板]POJ_1733_Pairty game(带权并查集/扩展域
    [题解/模板]悬线法luogu_P1169_棋盘制作(悬线法
    [题解]luogu_P2059_卡牌游戏(状态设计/概率dp
    [题解]luogu_P2467_地精部落(思维dp
    「CJOJ2439」军队
    「LG2397」yyy loves Maths VI (mode)
    「LG2114」起床困难综合症
  • 原文地址:https://www.cnblogs.com/zyulike/p/9993761.html
Copyright © 2011-2022 走看看