七牛云上传

七牛云上传

七牛云官方SDK文档:https://developer.qiniu.com/kodo/sdk/1239/java#private-get

jfinal

config-dev.txt

#七牛配置
#AccessKey
QiNiuAccessKey=wLNOjmPiS_2Zf_dL6k7yZCj3R7dBMQDQRvc-mYiW
#SecretKey
QiNiuSecretKey=nofijnEYEcnN4WnKUy0JssYv7_1kC8g1o90oITKF
#存储空间名
QiNiuBucket=sjxt-dfnsyh
#域名
QiNiuDoMain=http://qab8553jq.bkt.clouddn.com

七牛私有空间上传返回key

/**
* 七牛私有空间上传,返回key值
*/
    public void qiNiuSetFilePrivate(){
        UploadFile file = getFile();//JFinal规定getFile()必须最先执行

        String uploadPath = file.getUploadPath();//获取文件所在路径
        String fileName = file.getFileName();//获取文件名称
        String imgUrl = uploadPath+"/"+fileName;//拼接路径
        String fileKey = QiNiuUploadUtil.getPrivateFileUploadKey(imgUrl);//文件路径上传,返回文件key(文件名)
        //查看私有文件需要key值(key就是文件名称)
        renderJson(fileKey);
    }

获取七牛私有空间外链路径

/**
* 这个没有封装为工具类
* 根据key(文件名)获取七牛外链图片路径
*/
    public void qiNiuGetFilePrivate() throws UnsupportedEncodingException { //抛出不支持编码异常

        String fileNameKey = getPara("fileNameKey");//文件的key(key就是文件名称)

        String domainOfBucket = PropKit.get("QiNiuDoMain");//空间外链域名
        //放入key,返回encodedFileName
        String encodedFileName = URLEncoder.encode(fileNameKey, "utf-8").replace("+", "%20");
        //放入domainOfBucket,encodedFileName
        String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);

        String accessKey = PropKit.get("QiNiuAccessKey");//获取accessKey
        String secretKey = PropKit.get("QiNiuSecretKey");//获取secretKey
        Auth auth = Auth.create(accessKey, secretKey);
        long expireInSeconds = 3600;//1小时,可以自定义链接过期时间
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);

        renderJson(finalUrl);
    }

上传工具类utilS

package com.ys.utils;

import com.google.gson.Gson;
import com.jfinal.kit.PropKit;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class QiNiuUploadUtil {

    /**
     * 公开空间访问返回公开路径
     * @param localFilePath
     * @return
     */
    public static String getPublicFileUpload(String localFilePath){

            String path="";

            //构造一个带指定 Region 对象的配置类
            Configuration cfg = new Configuration(Region.region2());//region2:华南
            //...其他参数参考类注释
            UploadManager uploadManager = new UploadManager(cfg);
            //...生成上传凭证,然后准备上传
            String accessKey = PropKit.get("QiNiuAccessKey");
            String secretKey = PropKit.get("QiNiuSecretKey");
            String bucket = PropKit.get("QiNiuBucket");
            //如果是Windows情况下,格式是 D:\qiniu\test.png
            //默认不指定key的情况下,以文件内容的hash值作为文件名
            String key = null;
            Auth auth = Auth.create(accessKey, secretKey);

            String upToken = auth.uploadToken(bucket);

            try {

                Response response = uploadManager.put(localFilePath, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);

                System.out.println("图片地址:"+ PropKit.get("QiNiuDoMain") +putRet.key);

                path= PropKit.get("QiNiuDoMain") +putRet.key;

            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }

            return path;
        }



    /**
     * 私有空间访问返回文件名key
     * @param localFilePath
     * @return
     */
    public static String getPrivateFileUploadKey(String localFilePath){

        String putRetKey = null;//包含key和hash
        String putRetHash = null;//包含key和hash

        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());//region2:华南
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证,然后准备上传
        String accessKey = PropKit.get("QiNiuAccessKey");
        String secretKey = PropKit.get("QiNiuSecretKey");
        String bucket = PropKit.get("QiNiuBucket");
        //如果是Windows情况下,格式是 D:\qiniu\test.png
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = null;
        Auth auth = Auth.create(accessKey, secretKey);

        String upToken = auth.uploadToken(bucket);

        DefaultPutRet putRet = null;//包含key和hash
        try {

            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            //System.out.println(putRet.key);//key,文件名
            //System.out.println(putRet.hash);//文件的hash值

            //System.out.println("图片地址:"+ PropKit.get("QiNiuDoMain") +putRet.key);//域名加key(文件名)外链地址
            putRetKey = putRet.key;

        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }

        return putRetKey;
    }

}

原文地址:https://www.cnblogs.com/yu-si/p/12918289.html