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;
    }


    }
  • 相关阅读:
    【转】Linux 用cp和rsync同步文件时跳过指定目录
    解决svn错误:postcommit hook failed (exit code 1) with output
    Linux下如何使cp命令不提示覆盖文件
    Linux tar压缩时排除某个目录或文件的参数
    ecshop中404错误页面 .
    Linux如何查看当前目录下文件的个数
    Meta Property=og标签在SEO中的应用
    mysql mysqldump只导出表结构或只导出数据的实现方法
    slaveskiperrors( 转)
    自定义404错误页面返回状态码分析
  • 原文地址:https://www.cnblogs.com/sunsing123/p/9855855.html
Copyright © 2011-2022 走看看