本来打算做一个全部都是前端完成的资源上传到七牛云的demo,但是需要获取token,经历了九九八十一难,最终还是选择放弃,token从后端获取(springboot)。如果你们有前端直接能解决的麻烦记得私我哦!!!
效果展示

先看看文档:
element ui :https://element.eleme.cn/#/zh-CN/component/upload
七牛云:https://developer.qiniu.com/kodo/1283/javascript
前端
- 首先安装七牛云的JS的sdk
复制
- 此处域名是指向你自己的七牛云域名,目的是让照片回显。如何在七牛云中找到自己的域名请看下面。

3 .七牛云中找到自己的域名
| <template> |
| <div> |
| <el-upload class="upload-demo" action="https://upload.qiniup.com" :on-remove="handleRemove" :file-list="fileList" |
| list-type="picture" :http-request="customUpload" accept="image/jpeg,image/gif,image/png"> |
| <el-button size="small" type="primary">点击上传</el-button> |
| <div slot="tip" class="el-upload__tip">只能上传jpg/png/gif文件,且不超过2M</div> |
| </el-upload> |
| </div> |
| </template> |
| |
| <script> |
| |
| import { qiniuTonken } from "@/js/qiniu_token" |
| |
| export default { |
| name: "photoList", |
| data() { |
| return { |
| fileList: [], |
| } |
| }, |
| created() { |
| this.getQiniuToken() |
| }, |
| methods: { |
| getQiniuToken() { |
| qiniuTonken().then(res => { |
| this.yourQiniuUploadToken = res.data.data |
| }) |
| }, |
| |
| handleRemove(file, fileList) { |
| console.log(file, fileList); |
| }, |
| beforeUpload(file) { |
| |
| return true; |
| }, |
| customUpload(file) { |
| const qiniu = require('qiniu-js'); |
| |
| |
| const date = new Date(); |
| const year = date.getFullYear(); |
| const month = date.getMonth() + 1; |
| const directory = 'sm-frontend/' + year + '/' + month + '/'; |
| |
| const fileName = file.file.name; |
| const key = directory + fileName; |
| |
| const observable = qiniu.upload(file.file, key, this.yourQiniuUploadToken); |
| |
| return observable.subscribe({ |
| complete(res) { |
| |
| const imageUrls = 'http://***/' + res.key |
| const file_data = { |
| name: fileName, |
| url: imageUrls, |
| } |
| _this.fileList.push(file_data) |
| |
| |
| const uploadedFileIndex = _this.fileList.findIndex(file => file.url === imageUrls); |
| |
| if (uploadedFileIndex !== -1) { |
| console.log(uploadedFileIndex) |
| this.fileList[uploadedFileIndex].status = 'success'; |
| } |
| |
| }, |
| next(res) { |
| |
| }, |
| error(err) { |
| |
| }, |
| }); |
| } |
| } |
| } |
| </script> |
复制
JS

后端
技术:springboot、maven…
- 引入maven
| |
| <dependency> |
| <groupId>com.qiniu</groupId> |
| <artifactId>qiniu-java-sdk</artifactId> |
| <version>7.2.7</version> |
| </dependency> |
复制
- 编写controller代码
| package com.admin.controller.admin.email; |
| |
| import com.common.util.QiniuUtil; |
| import com.system.constant.HTTPStatus; |
| import com.system.response.Result; |
| import org.springframework.web.bind.annotation.GetMapping; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.RequestParam; |
| import org.springframework.web.bind.annotation.RestController; |
| |
| |
| |
| |
| |
| |
| @RestController |
| @RequestMapping("/sys/qiniu") |
| public class QiniuController { |
| |
| @GetMapping("/getUpToken") |
| public Result getUpToken(@RequestParam(value = "key", required = false) String key){ |
| return new Result().setCode(HTTPStatus.SUCCESS).setData(QiniuUtil.getToken(key)); |
| } |
| } |
| |
复制
- util代码
| import com.qiniu.util.Auth; |
| import lombok.extern.slf4j.Slf4j; |
| |
| @Slf4j |
| public class QiniuUtil { |
| |
| public static String getToken(String key) { |
| Auth auth = Auth.create(***ACCESS_KEY***, ***SECRET_KEY***); |
| return auth.uploadToken(***BUCKET***, key); |
| } |
| } |
复制
- 获取相关参数
ACCESS_KEY、SECRET_KEY
BUCKET
