上传图片到七牛云并返回图片URL
发布日期:2021-05-14 06:37:44 浏览次数:23 分类:精选文章

本文共 7368 字,大约阅读时间需要 24 分钟。

���������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������Qiniu Cloud���������������������������������������������������������������������������������������������

���������������������������

1. ���������������������

���������������������������������https://portal.qiniu.com/signup/choice���������������������������������������������������������������������������

2. ������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������1���������

���������������������������������������������������������������������������������������������

3. ���������������������������

���������������������������������������

  • ACCESS_KEY ��� SECRET_KEY������������������������������������
  • bucketname������������������������
  • DOMAIN���������������������������������������������������
  • style���������������������������������������������������

������������������

1. ���������������������

������ Maven ������������������������������ Java SDK��������� pom.xml���

com.qiniu
qiniu-java-sdk
7.1.1

2. ������������������������������������

��������������������������� QiniuCloudUtil������������������������

  • ������������ token���
  • ��������������������� base64Chunked ���������������������
  • ������������������������������������������
  • ������������������������������������������
package com.cn.netdisk.util;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.Base64;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import com.cn.netdisk.bean.Ret;
public class QiniuCloudUtil {
private static final String ACCESS_KEY = "your ACCESS KEY";
private static final String SECRET_KEY = "your SECRET KEY";
private static final String bucketname = "your bucket name";
private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
private static final String DOMAIN = "your upload domain";
private static final String style = "custom image style";
public String getUpToken() {
return auth.uploadToken(bucketname, null, 3600, new StringMap().put("insertOnly", 1));
}
public String upload(String filePath, String fileName) throws IOException {
UploadManager uploadManager = new UploadManager();
try {
String token = auth.uploadToken(bucketname);
if (UtilValidate.isEmpty(token)) {
System.out.println("������������token���������������");
return null;
}
Response res = uploadManager.put(filePath, fileName, token);
if (res.isOK()) {
Ret ret = res.jsonToObject(Ret.class);
return DOMAIN + ret.key + "?" + style;
}
} catch (QiniuException e) {
Response r = e.response;
System.out.println(r.toString());
try {
System.out.println(r.bodyString());
} catch (QiniuException e1) {
// ignore
}
}
return null;
}
public String put64image(byte[] base64, String key) throws Exception {
String url = "http://upload.qiniu.com/putb64/" + base64.length + "/" + key;
RequestBody rb = RequestBody.create(null, Base64.encodeToString(base64, 0));
Request request = new Request.Builder()
.url(url)
.addHeader("Content-Type", "application/octet-stream")
.addHeader("Authorization", "UpToken " + getUpToken())
.post(rb)
.build();
OkHttpClient client = new OkHttpClient();
okhttp3.Response response = client.newCall(request).execute();
return DOMAIN + key + "?" + style;
}
public void delete(String key) throws IOException {
BucketManager bucketManager = new BucketManager(auth);
String shortKey = key.substring(33);
try {
bucketManager.delete(bucketname, key);
} catch (QiniuException e) {
Response r = e.response;
System.out.println(r.toString());
}
}
private static class Ret {
public long fsize;
public String key;
public String hash;
public int width;
public int height;
}
}

3. ������������������

@ResponseBody
@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
public ResultInfo uploadImg(
@RequestParam(name = "image", required = false) MultipartFile image,
HttpServletRequest request) {
ResultInfo result = new ResultInfo();
if (image.isEmpty()) {
result.setCode(400);
result.setMsg("������������������������������");
return result;
}
try {
byte[] bytes = image.getBytes();
String imageName = UUID.randomUUID().toString();
QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
try {
String url = qiniuUtil.put64image(bytes, imageName);
result.setCode(200);
result.setMsg("������������������");
result.setInfo(url);
} catch (Exception e) {
e.printStackTrace();
}
return result;
} catch (IOException e) {
result.setCode(500);
result.setMsg("���������������������������");
return result;
}
}

4. ������������������

��������������������������� Vue ������������������������������������ Quill ���������������������������������������������

uploadImg: async function(id) {
var vm = this;
var fileInput = document.getElementById("uniqueId");
var formData = new FormData();
formData.append("image", fileInput.files[0]);
try {
var response = await this.$axios({
method: "post",
url: "/api/article/uploadImg",
data: formData
});
if (response.data.code === 200) {
var url = response.data.info;
if (url != null && url.length > 0) {
var selectRange = vm.$refs.myQuillEditor.quill.getSelection();
var index = selectRange != null ? selectRange.index : 0;
vm.$refs.myQuillEditor.quill.insertEmbed(index, 'image', url);
} else {
this.$Message.error("���������������������");
}
} else {
this.$Message.error(response.data.msg);
}
} catch (err) {
console.error("���������������������", err.message);
this.$Message.error("���������������������������");
}
}

������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:Kettle7.0实现oracle到mysql数据库迁移
下一篇:springBoot集成Activiti6

发表评论

最新留言

不错!
[***.144.177.141]2025年04月10日 01时25分49秒