Java 七牛云删除文件并立刻刷新缓存

引入pom.xml

<!-- https://mvnrepository.com/artifact/com.qiniu/qiniu-java-sdk -->
        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>7.2.0</version>
        </dependency>

QiniuUtil.java


import com.education.ak1.contant.QiNiuContant;
import com.google.gson.Gson;
import com.qiniu.cdn.CdnManager;
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;

import java.util.UUID;

public class QiNiuUtil {

    //获取token
    public static String getUpToken() {
        Auth auth = Auth.create(QiNiuContant.AccessKey, QiNiuContant.SecretKey);
        String uptoken = auth.uploadToken(QiNiuContant.bucketName);
        return uptoken;
    }

    //上传
    public static String upload(byte[] uploadBytes,String fileType) throws QiniuException{

        String key = UUID.randomUUID().toString().replaceAll("-", "")+fileType;

        Configuration cfg = new Configuration(Zone.zone2());//设置华南的服务器
        UploadManager uploadManager = new UploadManager(cfg);

        String upToken = getUpToken();

        Response response = uploadManager.put(uploadBytes, key, upToken);
        //解析上传成功的结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

        String urlPath = QiNiuContant.MediaUrl + putRet.key;

        return urlPath;
    }

    //删除文件
    public static void delete(String url) throws QiniuException{
        Configuration cfg = new Configuration(Zone.zone2());//设置华南的服务器
        Auth auth = Auth.create(QiNiuContant.AccessKey, QiNiuContant.SecretKey);

        BucketManager bucketManager = new BucketManager(auth, cfg);
        bucketManager.delete(QiNiuContant.bucketName, url.replaceAll(QiNiuContant.MediaUrl,""));
    }

    //刷新文件
    public static void refresh(String url) throws QiniuException{
        String [] urls = {url};
        Auth auth = Auth.create(QiNiuContant.AccessKey, QiNiuContant.SecretKey);
        CdnManager c = new CdnManager(auth);
        Response response = c.refreshUrls(urls);
    }

//    public static void main(String[] args) throws Exception{
//        refresh("http://collegemedia.ayunyao.com/fe980ec8a930484cb1455de40d168441.png");
//    }

}

七牛云配置文件
QiniuContant.java

/**
 * 七牛的配置文件
 * */
public class QiNiuContant {

    //测试地址
    public final static String MediaUrl = "CDN地址";

    public final static String AccessKey = "你的key";
    public final static String SecretKey = "你的Secretkey";

    public final static String bucketName = "你的bucket名字";
}

QiniuController.java


import com.education.ak1.utils.QiNiuUtil;
import com.qiniu.common.QiniuException;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/upload")
public class QiniuController {

    @PostMapping("/file")
    @ApiOperation(value = "七牛云上传文件", notes = "七牛云上传文件")
    public Object upload(@RequestParam(value = "file", required = false) MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        try {
            String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            InputStream is = file.getInputStream();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = is.read(b)) != -1) {
                bos.write(b, 0, len);
            }

            byte[] uploadBytes = bos.toByteArray();

            String url = QiNiuUtil.upload(uploadBytes, fileType);
            map.put("code", 0);
            map.put("msg", "成功!");
            map.put("data", url);
        } catch (QiniuException e) {
            map.put("code", 4001);
            map.put("msg", "文件异常!");
            map.put("data", "");
        } catch (IOException e) {
            map.put("code", 4001);
            map.put("msg", "文件异常!");
            map.put("data", "");
        }
        return map;
    }

    @ApiOperation(value = "删除七牛云文件", notes = "删除七牛云文件")
    @GetMapping("/delete")
    public Object delete(String url) {
        Map<String, Object> map = new HashMap<>();
        try {
            QiNiuUtil.delete(url);//删除文件
            QiNiuUtil.refresh(url);//刷新缓存
            map.put("code", 0);
            map.put("msg", "删除成功!");
            map.put("data", "");
        } catch (QiniuException e) {
            map.put("code", 4001);
            map.put("msg", "文件异常!");
            map.put("data", "");
        } catch (IOException e) {
            map.put("code", 4001);
            map.put("msg", "文件异常!");
            map.put("data", "");
        }


        return map;
    }
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自在如风。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值