SpringBoot上传文件到七牛云

准备工做

maven

pom.xml添加七牛云的sdk依赖java

<dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>7.2.27</version>
        </dependency>
复制代码

配置项

七牛云上传必要的配置有:accessKey、secretKey、bucket 其中accessKey、secretKey在该网址可查看web

portal.qiniu.com/user/keyspring

bucket为你的存储空间名,以下: 微信

image.png


实现

application.yml配置

upload:
 qiniu:
 domain: 填你的域名
 access-key: 你的accesskey
 secret-key: 你的secretKey
 bucket: 你的存储空间名,我这里为colablog
复制代码

能够看到个人七牛云上传配置中有domain这项配置,这个配置是七牛云buket的存储域名,在内容管理下,主要用于上传文件成功后把文件访问路径返还回去。 app

image.png
可是这个域名是限时30天使用的,因此你最好绑定一个新的域名。

上传配置类

使用SpringBoot的@ConfigurationProperties@Component注解实现上传的配置类UploadProperties, 由于上传配置可能会有本地上传和云上传或者其余上传的,因此该配置类我留了扩展点。由于受到了rabbitmq的配置类启发,并且上传的配置不会不少,因此用内部类来分割这种配置类。上传配置类以下:dom

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/** * @author Johnson * @date 2019/12/16/ 09:35:36 */
@Component
@ConfigurationProperties(prefix = "upload")
public class UploadProperties {

    private Local local = new Local();

    public Local getLocal() {
        return local;
    }

    /** * @author: Johnson * @Date: 2019/12/16 * 本地上传配置 */
    public class Local {
        ...
    }

    private Qiniu qiniu = new Qiniu();

    public Qiniu getQiniu() {
        return qiniu;
    }

    /** * @author: Johnson * @Date: 2019/12/16 * 七牛云上传配置 */
    public class Qiniu {
        /** * 域名 */
        private String domain;

        /** * 从下面这个地址中获取accessKey和secretKey * https://portal.qiniu.com/user/key */
        private String accessKey;

        private String secretKey;

        /** * 存储空间名 */
        private String bucket;

        public String getDomain() {
            return domain;
        }

        public void setDomain(String domain) {
            this.domain = domain;
        }

        public String getAccessKey() {
            return accessKey;
        }

        public void setAccessKey(String accessKey) {
            this.accessKey = accessKey;
        }

        public String getSecretKey() {
            return secretKey;
        }

        public void setSecretKey(String secretKey) {
            this.secretKey = secretKey;
        }

        public String getBucket() {
            return bucket;
        }

        public void setBucket(String bucket) {
            this.bucket = bucket;
        }
    }
}
复制代码

七牛云上传接口和类

上传接口以下:maven

public interface UploadFile {
    String uploadFile(MultipartFile file);
}

复制代码

上传类ide

import cn.colablog.blogserver.utils.properties.UploadProperties;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/** * @author Johnson * @date 2019/12/14/ 17:20:16 * 上传文件到七牛云 */
public class UploadFileQiniu implements UploadFile {

    private UploadProperties.Qiniu properties;

    //构造一个带指定Region对象的配置类
    private Configuration cfg = new Configuration(Region.region2());

    private UploadManager uploadManager= new UploadManager(cfg);

    public UploadFileQiniu(UploadProperties.Qiniu properties) {
        this.properties = properties;
    }

    /** * @author: Johnson */
    @Override
    public String uploadFile(MultipartFile file) {
        Auth auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
        String token = auth.uploadToken(properties.getBucket());
        try {
            String originalFilename = file.getOriginalFilename();
            // 文件后缀
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileKey = UUID.randomUUID().toString() + suffix;
            Response response = uploadManager.put(file.getInputStream(), fileKey, token, null, null);
            return properties.getDomain() + fileKey;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }
}
复制代码

Region配置,这里表明空间的存取区域,由于个人存储空间的区域为华南。因此为Region.region2(),查看本身的存储区域可在空间概览的最下方查看到,这里就不截图了,图片占位太大。
Region对应的设置: this

image.png

好了,准备工做已完成,如今就到调用了,调用类以下:spa

@RestController
@RequestMapping("/upload")
public class UploadFileController {
    @Autowired
    UploadProperties uploadProperties;

    @PostMapping("/img")
    public String uploadFileYun(MultipartFile file) {
        // 上传到七牛云
        UploadFile uploadFile = new UploadFileQiniu(uploadProperties.getQiniu());
        return uploadFile.uploadFile(file);
    }
}
复制代码

是否是很简单呢?屁啊!简单个毛线,其实这个我是已经简化了,实际上在个人项目的结构是比这个复杂的。


总结

一:个人类名都是以Upload开头的,类名已经写死了只有上传功能,就限制了这个类的可扩展性了,假如添加文件删除功能,就不该该添加到这个类中。如今已经在重构文件操做(非文件上传了)的功能模块了。

二:一开始我以为文件上传功能可能使用的比较少,因此使用到的时候才去实例化文件上传类,可是这须要根据开发场景来决定,由于个人项目是一个博客后台管理系统,会常常上传图片,因此上传文件类能够注入到Spring容器中,这样也能够减小实例化的开销(虽然很小)。注入的话就是用@Component类注解。

三:配置文件我为何会想到使用内部类来分割配置项呢?其实你们在编写一些相似功能的时候,均可以去参照一下别人的源码,固然,这里指的是大神的源码。由于我在写配置项的时候就想看看大神的配置项是怎么写的,就点进了rabbitmq的配置项。因此啊,看到了大神的代码是真的有长进的。

若是你须要查看更详细的官方文档,请点击下方连接:

developer.qiniu.com/kodo/sdk/12…


最后:感谢你们的阅读,Thanks♪(・ω・)ノ

我的博客网址: colablog.cn/

若是个人文章帮助到您,能够关注个人微信公众号,第一时间分享文章给您

微信公众号
相关文章
相关标签/搜索