七牛云上传文件

更改授权码和钥匙,外链域名即可使用

package co.yixiang.modules.wechat.rest.utils;

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


/**
 * 七牛云工具类
 * Zone.zone0:华东
 * Zone.zone1:华北
 * Zone.zone2:华南
 * Zone.zoneNa0:北美
 * ———http上传,自动识别上传区域——
 * Zone.httpAutoZone
 * ———https上传,自动识别上传区域—— //Zone.httpsAutoZone
 * Configuration cfg = new Configuration(Zone.zone2());其中Configuration中的Zone.zone2()可以改为Zone.zone0()
 */
public class QiniuUtils {
	//访问授权码
    public  static String accessKey = "I2B9zvxKNWXxO8cp-jCDqXFXVum2UbKHW5zY4w3q";
    //秘密钥匙
    public  static String secretKey = "WoHDOcE0N7-ZYXM1MlSBF6dEK0XC_7dmX15P0nQ5";
    //空间名称
    public  static String bucket = "haoyong-yuanyuzhou";
    //外链域名
    public static String domain = "http://r673s884h.hn-bkt.clouddn.com/";
	
	//上传方式一:外链+文件名 获取其他图片链接进行上传
    public static void upload2Qiniu(String filePath,String fileName){
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(filePath, fileName, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        } catch (QiniuException ex) {
            Response r = ex.response;
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
    }

    //上传方式二:文件上传 通过上传文件的方式上传到存储空间
    public static String upload2Qiniu(byte[] bytes, String fileName){
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);

        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(bytes, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            //System.out.println(putRet.key);
           // System.out.println(putRet.hash);
            //返回文件完整路径
            return domain+putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            //System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
                return "";
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return "";
    }

    //删除文件 参数:存储的图片文件名
    public static void deleteFileFromQiniu(String fileName){
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException ex) {
            //如果遇到异常,说明删除失败
            //System.err.println(ex.code());
           // System.err.println(ex.response.toString());
        }
    }
}
@RequestMapping("/upload")
@ResponseBody
public HashMap<String, Object> upload(@RequestParam("file") MultipartFile imgFile){
    HashMap<String,Object> result = new HashMap<String,Object>();
    String originalFilename = imgFile.getOriginalFilename();//获取图片原始文件名
    int index = originalFilename.lastIndexOf(".");
    String extention = originalFilename.substring(index);//获得图片后缀名  .jpg
    String fileName = UUID.randomUUID().toString() + extention; //进行拼接
    fileName = fileName.replace("-",""); //将文件路径中的-替换掉
    try {
        //使用工具类将文件上传到七牛云服务器
        String filePath = QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
        result.put("filePath",filePath);
    } catch (IOException e) {
        throw new BusinessException("上传图片失败!",e.getMessage());
    }
    return result;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值