上一篇 wangEditor的配置:https://blog.csdn.net/qq_35349982/article/details/103288508
這裏主要講 java後臺
我這裏用的是七牛雲
Controller層
package com.xianbaovip.project.Controller;
import cn.hutool.core.lang.ObjectId;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xianbaovip.project.Modle.dto.ImgResultDto;
import com.xianbaovip.project.Server.UploadImg.UploadImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class UploadImgCrotroller {
@Autowired
private UploadImgService uploadImgService;
@RequestMapping("/uploadImg")
public String updata(@RequestParam(name="file") MultipartFile file) throws Exception {
//首先判斷是不是空的文件
if (!file.isEmpty()) {
// //對文文件的全名進行截取然後在後綴名進行刪選。
// int begin = file.getOriginalFilename().indexOf(".");
// int last = file.getOriginalFilename().length();
// String suffix = file.getOriginalFilename().substring(begin, last);
//
// //讀取文件的後綴名字
// String fileName = imgId+suffix;
//生成圖片的唯一id
String imgId = ObjectId.next();
ImgResultDto imgResultDto = uploadImgService.uploadImage(imgId, file);
String jsonStr= JSONUtil.toJsonStr(imgResultDto);
return jsonStr;
}else{
JSONObject json1 = JSONUtil.createObj();
json1.put("code", 300);
json1.put("msg", "圖片上傳錯誤");
String jsonStr= JSONUtil.toJsonStr(json1);
return jsonStr;
}
}
}
Server層
package com.xianbaovip.project.Server.UploadImg;
import cn.hutool.core.util.StrUtil;
import com.xianbaovip.project.Common.constant;
import com.xianbaovip.project.Modle.dto.ImgResultDto;
import com.xianbaovip.project.Utils.UpdataImgUtils;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
@PropertySource("classpath:common.properties")
public class UploadImgService {
@Value("${imgServer}")
private String imgServer;
public ImgResultDto uploadImage(String imgId,MultipartFile file) throws IOException {
String putRetKey = new UpdataImgUtils().UpdataImg(imgId, file.getBytes());
String[] urlData = new String[1];
ImgResultDto imgResultDto = new ImgResultDto();
if(!StrUtil.hasBlank(putRetKey)){
//設置返回值的類
urlData[0] =imgServer+imgId;
imgResultDto.setErrno(constant.imgErrorStart);//設置狀態碼
imgResultDto.setData(urlData);
}else{
imgResultDto.setErrno(constant.imgErrorStart1);//設置狀態碼
}
return imgResultDto;
}
}
工具類
package com.xianbaovip.project.Utils;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
public class UpdataImgUtils {
private static final String img_Ak = "自行填寫";
private static final String img_Sk = "自行填寫";
private static final String img_Bucket = "自行填寫";
private static final String img_Url = "自行填寫";
public String UpdataImg(String imgName, byte[] uploadBytes ){
String putRetKey = "";
//構造一個帶指定Region對象的配置類
Configuration cfg = new Configuration(Region.region1());
//...其他參數參考類註釋
UploadManager uploadManager = new UploadManager(cfg);
//...生成上傳憑證,然後準備上傳
String accessKey = img_Ak;
String secretKey = img_Sk;
String bucket = img_Bucket;
//如果是Windows情況下,格式是 E:\ss.jpg
//String localFilePath = "E:\\ss.jpg";
//默認不指定key的情況下,以文件內容的hash值作爲文件名
//String key = null;
//byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket,imgName);
try {
Response response = uploadManager.put(byteInputStream,imgName,upToken,null, null);
//解析上傳成功的結果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
putRetKey = putRet.key;
System.out.println(putRet);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
return putRetKey;
}
}