首页>代码>基于SpringBoot1.5 + MyBatis + Thymeleaf 开发的个人博客源码分享>/SpringBootBlog/src/main/java/com/wip/api/QiNiuCloudService.java
/** * Created by IntelliJ IDEA. * User: Administrator * DateTime: 2018/8/3 22:17 **/ package com.wip.api; import com.google.gson.Gson; import com.qiniu.common.QiniuException; import com.qiniu.common.Zone; import com.qiniu.http.Response; import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager; import com.qiniu.storage.model.DefaultPutRet; import com.qiniu.util.Auth; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; /** * 七牛文件上传API */ public class QiNiuCloudService { /** * 密钥凭证 */ private static final String ACCESS_KEY = "ZmI_De7H9XSG295Qerp_hck-yD3lIK9lTe1ANVnR"; private static final String SECRET_KEY = "SZnGo1PrtcNJtA1uaOAdAQgdEvy5qEgqUENMEsuX"; /** * 仓库 */ private static final String BUCKET = "imgblog"; /** * 七牛外网访问地址 */ public static final String QINIU_UPLOAD_SITE = "pr6w2yy51.bkt.clouddn.com"; /** * 上传文件到七牛公共方法 * @param file 上传的文件 * @param fileName 文件名 * @return */ public static String upload(MultipartFile file, String fileName) { // 构造一个带指定Zone对象的配置类 // 华东 Zone.zone0() // 华北 Zone.zone1() // 华南 Zone.zone2() // 北美 Zone.zoneNa0() Configuration cfg = new Configuration(Zone.zone0()); // 其它参数参考类注释 UploadManager uploadManager = new UploadManager(cfg); // 默认不指定key的情况下,以文件内容的hash值作为文件名 String key = null; Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY); String upToken = auth.uploadToken(BUCKET); try { Response response = null; response = uploadManager.put(file.getInputStream(), fileName, upToken,null,null); // 解析上传成功的结果 DefaultPutRet putRet = new Gson().fromJson(response.bodyString(),DefaultPutRet.class); System.out.println(putRet.key); System.out.println(putRet.hash); return putRet.key; } catch (QiniuException ex) { Response r = ex.response; System.out.println(r.toString()); try { System.out.println(r.bodyString()); } catch (QiniuException ex2) { } } catch (IOException e) { e.printStackTrace(); } return null; } }
最近下载更多
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 LV53
2022年5月1日
一个迷茫的打工仔 LV8
2021年12月20日
flydog3 LV2
2021年12月17日
ruishent LV6
2021年12月16日
liguoliang LV3
2021年9月15日
最近浏览更多
yangyang567 LV1
3月8日
赵明磊
1月18日
暂无贡献等级
夜起星河 LV8
2023年12月27日
微信网友_6631309708414976 LV5
2023年12月10日
zhaozhiqi LV5
2023年10月19日
漫步的海星 LV4
2023年9月26日
java小书童 LV17
2023年7月26日
hkxyyz LV6
2023年7月16日
cheyunhai
2023年6月25日
暂无贡献等级
tangyongheng08 LV6
2023年6月15日