Java 杂谈Spring-BootSpringFramework

基于springboot+vue.js实现的前后端分离的blog

2018-06-25  本文已影响147人  余空啊

关于

其实在很早之前我就开发过一个博客系统,不过由于当时自己的技术不够成熟,所以选择的是ssm+velocity开发。前后端没有分离,编辑器选择是百度uedit,后台使用的jQuery easyui,整个界面不是很美观,而且富文本编辑器很难用。所以一直想想给blog 升级。
现在 blog v2.0 beat即将上线。
它是基于 spring + vue实现的。

技术栈

运行截图


image.png

分隔符

image.png

主要实现代码

package com.blog.qiniu.service.impl;

import com.blog.qiniu.QiNiuProperties;
import com.blog.qiniu.service.QiNiuService;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;

/**
 * @author: yukong
 * @date: 2018/6/21 13:50
 * @description:
 */
@Service
public class QiNiuServiceImpl implements QiNiuService, InitializingBean {
    @Autowired
    private UploadManager uploadManager;
    @Autowired
    private BucketManager bucketManager;
    @Autowired
    private Auth auth;
    @Autowired
    private QiNiuProperties qiNiuProperties;
    private StringMap putPolicy;
    @Override
    public Response uploadFile(File file) throws QiniuException {
        Response response = this.uploadManager.put(file, null, getUploadToken());
        int retry = 0;
        while (response.needRetry() && retry < 3) {
            response = this.uploadManager.put(file, null, getUploadToken());
            retry++;
        }
        return response;
    }
    @Override
    public Response uploadFile(InputStream inputStream) throws QiniuException {
        Response response = this.uploadManager.put(inputStream, null, getUploadToken(), null, null);
        int retry = 0;
        while (response.needRetry() && retry < 3) {
            response = this.uploadManager.put(inputStream, null, getUploadToken(), null, null);
            retry++;
        }
        return response;
    }
    @Override
    public Response delete(String key) throws QiniuException {
        Response response = bucketManager.delete(qiNiuProperties.getBucket(), key);
        int retry = 0;
        while (response.needRetry() && retry++ < 3) {
            response = bucketManager.delete(qiNiuProperties.getBucket(), key);
        }
        return response;
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        this.putPolicy = new StringMap();
        putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
    }
    /**
     * 获取上传凭证
     *
     * @return
     */
    private String getUploadToken() {
        return this.auth.uploadToken(qiNiuProperties.getBucket(), null, 3600, putPolicy);
    }
}

总结

希望大家有兴趣一起完善的可以联系我

上一篇下一篇

猜你喜欢

热点阅读