zoukankan      html  css  js  c++  java
  • 七牛上传图片和二进制流方法

    package com.qlyd.aspmanager.common.qiniu;

    import com.alibaba.fastjson.JSON;
    import com.google.gson.Gson;
    import com.qiniu.common.QiniuException;
    import com.qiniu.common.Zone;
    import com.qiniu.http.Response;
    import com.qiniu.storage.BucketManager;
    import com.qiniu.storage.Configuration;
    import com.qiniu.storage.UploadManager;
    import com.qiniu.storage.model.DefaultPutRet;
    import com.qiniu.storage.model.FetchRet;
    import com.qiniu.util.Auth;
    import com.qlyd.aspmanager.common.uuid.UUIDTool;

    import java.io.FileInputStream;
    import java.io.InputStream;

    /**
    * @author madebychina
    * @date 2018/9/5 10:12
    */
    public class WebFileUploadTool {
    /**
    * 七牛AK
    */
    public static final String accessKey = "bLNti1fu58oi6ZAlIS0y49RyU00CdUpuajbc8GWj";
    /**
    * 七牛SK
    */
    public static final String secretKey = "7zruUMyV_VqluxsjHQs0Ozb1GrkINmBmAV9GxlLQ";
    /**
    * 存储空间名字
    */
    public static final String bucket = "mini-app";
    /**
    * 七牛绑定的自定义域名
    */
    public static final String BUCKET_HOST_NAME = "https://miniapp.qiluyidian.mobi";

    /**
    * 上传web文件到七牛
    *
    * @param filePath 文件地址
    * @return 七牛存储地址
    */
    public static String uploadWebFile(String filePath) {
    String imgUrl = null;
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone0());
    String key = UUIDTool.getUUID();
    String extension = FileNameTool.getWebFileExtensionName(filePath);
    if (extension.length() > 0) {
    key += "." + FileNameTool.getWebFileExtensionName(filePath);
    }
    Auth auth = Auth.create(accessKey, secretKey);
    BucketManager bucketManager = new BucketManager(auth, cfg);
    try {
    FetchRet fetchRet = bucketManager.fetch(filePath, bucket, key);
    //七牛的实际存储地址
    imgUrl = BUCKET_HOST_NAME + "/" + fetchRet.key;
    } catch (QiniuException ex) {
    ex.printStackTrace();
    }
    return imgUrl;
    }

    public String uploadImg(FileInputStream file, String key) {
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone1());
    //...其他参数参考类注释
    UploadManager uploadManager = new UploadManager(cfg);
    //...生成上传凭证,然后准备上传
    // String bucket = "oy09glbzm.bkt.clouddn.com";
    //默认不指定key的情况下,以文件内容的hash值作为文件名
    try {
    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    try {
    Response response = uploadManager.put(file, key, upToken, null, null);
    //解析上传成功的结果
    DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
    String return_path = BUCKET_HOST_NAME + "/" + putRet.key;
    return return_path;
    } catch (QiniuException ex) {
    Response r = ex.response;
    System.err.println(r.toString());
    try {
    System.err.println(r.bodyString());
    } catch (QiniuException ex2) {
    //ignore
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return "";
    }

    /**
    * 通过输入流上传
    *
    * @param inputStream
    * @return
    * @throws
    */

    public static String upload(InputStream inputStream) {
    String imgUrl = "";
    Configuration cfg = new Configuration(Zone.zone0());
    //创建上传对象
    UploadManager uploadManager = new UploadManager(cfg);
    String key = UUIDTool.getUUID();

    try {
    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    try {
    Response response = uploadManager.put(inputStream, key, upToken, null, null);
    //解析上传成功的结果
    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    imgUrl = BUCKET_HOST_NAME + "/" + putRet.key;
    } catch (QiniuException ex) {
    ex.printStackTrace();
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return imgUrl;
    }


    }
  • 相关阅读:
    散列
    Studio 3T破解方式
    springboot整合elasticsearch时的版本问题:
    ElasticSearch6.4.1 【Rejecting mapping update to [posts] as the final mapping would have more than 1 type】
    IP地址查询API
    拉姆达表达式 追加 条件判断 Expression<Func<T, bool>>
    类 映射 遍历大全
    jquery load(URL,FUNCTION(){}) 异步加载页面
    LINQ to Entities 不识别方法的解决方案
    当实体类属性超多时候 映射给实体类属性赋值(拉姆达+实体类映射)
  • 原文地址:https://www.cnblogs.com/sunsing123/p/9855855.html
Copyright © 2011-2022 走看看