1. 先贴个本地上传到七牛云的
public static void main(String[] args) {
String accessKey = "dkoxFvCZPL9h8fNkKhPL9lUycB_ZA5oL-NXAdv";
String secretKey = "ZJ8Z1UHaPL9lUyDnUkKhbE5BtqWnHjv-OM9RqUEx";
String bucket = "takkimg";
Configuration configuration = new Configuration(Zone.zone0());
UploadManager manager = new UploadManager(configuration);
String key = "weishenmemeiyouhouzhui.png";
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
String localFilePath = "D:\\test.png";
Response response = null;
try {
response = manager.put(localFilePath, key, upToken);
DefaultPutRet set = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(set.key);
System.out.println(set.hash);
} catch (QiniuException e) {
Response r = e.response;
System.err.println(r.toString());
}
}
2. 上传图片云存储
1.后端
@PostMapping("upload1")
@ResponseBody
public String upload1(@RequestParam("file") MultipartFile file) throws IOException {
log.info("[文件类型] - [{}]", file.getContentType());
log.info("[文件名称] - [{}]", file.getOriginalFilename());
log.info("[文件大小] - [{}]", file.getSize());
String accessKey = "dkoxFvCZ8IaNh8fNaPL9lUyDFkyHocB_ZA5oL-NXAdv";
String secretKey = "ZJ8Z1UHaPL9lUyDZ8IaNh8fNE5BtqWnHjv-OM9RqUEx";
String bucket = "tangz243mg";
Configuration configuration = new Configuration(Zone.zone0());
UploadManager manager = new UploadManager(configuration);
String key = null;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
Response response = null;
DefaultPutRet set = null;
try {
response = manager.put(byteArrayInputStream, key, upToken,null,null);
set = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(set.key);
System.out.println(set.hash);
} catch (QiniuException e) {
Response r = e.response;
System.err.println(r.toString());
}
String imgUrl = "http://imgs.tangzekk.xyz/" + set.hash;
return imgUrl;
}
2. 前端
<template>
<div>
<el-upload
class="upload-demo"
drag
action="http://localhost:8080/api/upload1"
:on-success="handleAvatarSuccess"
multiple>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<div style="margin-top: 15px;">
<el-input placeholder="目前未有图片地址" v-model="imgUrl" class="input-with-select">
<el-button slot="append" icon="el-icon-search"
v-clipboard:copy="imgUrl"
v-clipboard:success="onCopy"
v-clipboard:error="onError"></el-button>
</el-input>
<img :src="imgUrl"/>
</div>
</div>
</template>
<script>
export default {
name: "Upload",
data() {
return {
imgUrl: ''
}
},
methods:{
handleAvatarSuccess(response,file,fileList) {
console.log(response)
this.imgUrl = response;
},
onCopy() {
alert("复制到剪切板")
},
onError() {
alert("复制失败")
}
}
}
</script>
<style scoped>
</style>