/中级项目课代码/src/main/java/com/nowcoder/service/QiniuService.java
https://bitbucket.org/xiaoyang0112/sourcetree · Java · 68 lines · 49 code · 8 blank · 11 comment · 5 complexity · e0769154fc22efc679dfd9f3870ffcba MD5 · raw file
- package com.nowcoder.service;
- import com.alibaba.fastjson.JSONObject;
- import com.nowcoder.util.ToutiaoUtil;
- import com.qiniu.common.QiniuException;
- import com.qiniu.http.Response;
- import com.qiniu.storage.UploadManager;
- import com.qiniu.util.Auth;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.IOException;
- import java.util.UUID;
- /**
- * Created by nowcoder on 2016/7/7.
- */
- @Service
- public class QiniuService {
- private static final Logger logger = LoggerFactory.getLogger(QiniuService.class);
- //设置好账号的ACCESS_KEY和SECRET_KEY
- String ACCESS_KEY = "abNXnXBIlI6viRaOeRY6Hk-zc3V-NpjLcGfYz5kD";
- String SECRET_KEY = "QP7Xja3FmP1Zyl-oxwQDCb7T6wCoEFKoO-0vht_5";
- //要上传的空间
- String bucketname = "nowcoder";
- //密钥配置
- Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
- //创建上传对象
- UploadManager uploadManager = new UploadManager();
- private static String QINIU_IMAGE_DOMAIN = "http://7xsetu.com1.z0.glb.clouddn.com/";
- //简单上传,使用默认策略,只需要设置上传的空间名就可以了
- public String getUpToken() {
- return auth.uploadToken(bucketname);
- }
- public String saveImage(MultipartFile file) throws IOException {
- try {
- int dotPos = file.getOriginalFilename().lastIndexOf(".");
- if (dotPos < 0) {
- return null;
- }
- String fileExt = file.getOriginalFilename().substring(dotPos + 1).toLowerCase();
- if (!ToutiaoUtil.isFileAllowed(fileExt)) {
- return null;
- }
- String fileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + fileExt;
- //调用put方法上传
- Response res = uploadManager.put(file.getBytes(), fileName, getUpToken());
- //打印返回的信息
- if (res.isOK() && res.isJson()) {
- return QINIU_IMAGE_DOMAIN + JSONObject.parseObject(res.bodyString()).get("key");
- } else {
- logger.error("七牛异常:" + res.bodyString());
- return null;
- }
- } catch (QiniuException e) {
- // 请求失败时打印的异常的信息
- logger.error("七牛异常:" + e.getMessage());
- return null;
- }
- }
- }