首页>代码>基于SpringBoot1.5 + MyBatis + Thymeleaf 开发的个人博客源码分享>/SpringBootBlog/src/main/java/com/wip/api/QiNiuCloudService.java
01/**
02 * Created by IntelliJ IDEA.
03 * User: Administrator
04 * DateTime: 2018/8/3 22:17
05 **/
06package com.wip.api;
07 
08import com.google.gson.Gson;
09import com.qiniu.common.QiniuException;
10import com.qiniu.common.Zone;
11import com.qiniu.http.Response;
12import com.qiniu.storage.Configuration;
13import com.qiniu.storage.UploadManager;
14import com.qiniu.storage.model.DefaultPutRet;
15import com.qiniu.util.Auth;
16import org.springframework.web.multipart.MultipartFile;
17 
18import java.io.IOException;
19 
20/**
21 * 七牛文件上传API
22 */
23public class QiNiuCloudService {
24 
25    /**
26     * 密钥凭证
27     */
28    private static final String ACCESS_KEY = "ZmI_De7H9XSG295Qerp_hck-yD3lIK9lTe1ANVnR";
29    private static final String SECRET_KEY = "SZnGo1PrtcNJtA1uaOAdAQgdEvy5qEgqUENMEsuX";
30 
31    /**
32     * 仓库
33     */
34    private static final String BUCKET = "imgblog";
35 
36    /**
37     * 七牛外网访问地址
38     */
39    public static final String QINIU_UPLOAD_SITE = "pr6w2yy51.bkt.clouddn.com";
40 
41    /**
42     * 上传文件到七牛公共方法
43     * @param file          上传的文件
44     * @param fileName      文件名
45     * @return
46     */
47    public static String upload(MultipartFile file, String fileName) {
48 
49        // 构造一个带指定Zone对象的配置类
50        // 华东 Zone.zone0()
51        // 华北 Zone.zone1()
52        // 华南 Zone.zone2()
53        // 北美 Zone.zoneNa0()
54        Configuration cfg = new Configuration(Zone.zone0());
55        // 其它参数参考类注释
56        UploadManager uploadManager = new UploadManager(cfg);
57 
58        // 默认不指定key的情况下,以文件内容的hash值作为文件名
59        String key = null;
60        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
61        String upToken = auth.uploadToken(BUCKET);
62        try {
63            Response response = null;
64            response = uploadManager.put(file.getInputStream(), fileName, upToken,null,null);
65 
66            // 解析上传成功的结果
67            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(),DefaultPutRet.class);
68            System.out.println(putRet.key);
69            System.out.println(putRet.hash);
70            return  putRet.key;
71        } catch (QiniuException ex) {
72            Response r = ex.response;
73            System.out.println(r.toString());
74            try {
75                System.out.println(r.bodyString());
76            } catch (QiniuException ex2) {
77 
78            }
79        } catch (IOException e) {
80            e.printStackTrace();
81        }
82        return null;
83    }
84}
最近下载更多
zolscy  LV24 2024年11月29日
y1214435276  LV9 2024年9月26日
qq1466625431  LV6 2023年3月4日
chonger  LV4 2022年11月7日
dawnguarda  LV17 2022年11月2日
taijh999  LV13 2022年6月27日
LLLLLL_77  LV2 2022年6月16日
wanglinddad  LV55 2022年5月1日
一个迷茫的打工仔  LV8 2021年12月20日
flydog3  LV2 2021年12月17日
最近浏览更多
f22m1a2b2  LV17 1月9日
xinshou11111x 2024年12月12日
暂无贡献等级
zolscy  LV24 2024年11月29日
y1214435276  LV9 2024年9月26日
李朝磊  LV18 2024年7月4日
TY0165  LV20 2024年6月25日
ma406805131  LV19 2024年6月15日
lllajen 2024年6月9日
暂无贡献等级
ccccchhhh 2024年5月31日
暂无贡献等级
来一杯西瓜冰咩  LV6 2024年5月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友