上一篇 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 {@Autowiredprivate 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;//生成图片的唯一idString 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;}
}

wangEditor Java富文本的图片上传相关推荐

  1. 富文本编辑器图片上传失败的BUG解决:IndexError:list index out of range

    富文本编辑器图片上传失败的BUG解决 问题原因 我们将通过Django上传的图片保存到了FastDFS中,而保存在FastDFS中的文件名没有后缀名(.png/.jpg/.jif),而ckeditor ...

  2. layui富文本编辑器上传图片java_解决layui富文本编辑器图片上传无法回显的问题...

    layui富文本编辑器用起来挺方便的,但是不足的是不提供图片上传的接口,需要自己写上传接口,而且返回的数据类型要符合layui富文本编辑器图片上传插件的要求,否则图片可以上传成功,但是无法回显,这个问 ...

  3. layui富文本编辑器图片上传无法回显问题解决

    layui富文本编辑器用起来挺方便的,但是不足的是不提供图片上传的接口,需要自己写上传接口,而且返回的数据类型要符合layui富文本编辑器图片上传插件的要求,否则图片可以上传成功,但是无法回显,这个问 ...

  4. WangEditor富文本编辑器图片上传踩坑之路

    最近由于业务需求,需要用到富文本编辑器,找寻了好久,起初想使用百度的ueditor,但在使用的过程中实在是遇到了太多的坑,于是另外锁定了一款富文本编辑器--wangEditor.这是一款轻量级的富文本 ...

  5. springboot整合H+,基于summernote的富文本编辑器图片上传

    我在做这块任务时遇到了好几个问题,也查了一些资料,自己总结的一些东西,希望可以帮到一些有需要的朋友,废话不多说开始看代码. 首先看前台页面: 需要注意的是summernote的默认图片上传是base6 ...

  6. bootstrap summernote富文本编辑器图片上传干货分享

    个人技术网站 欢迎关注 今天做后台的时候需要一个富文本编辑器组件,由于项目使用的是bootstrap,所以毫不犹豫的用上了summernote富文本编辑器.文档各大大牛已经整理出来了  但是图片上传到 ...

  7. Layui富文本编辑器图片上传接口(.NET C#)

    本来想偷懒找个现成的接口,搜了一下发现没有现成的,那我在这写一个并分享给大家吧. demo打包好了在我的csdn下载中心:http://download.csdn.net/download/xiang ...

  8. Tinymce富文本编辑器图片上传即编辑配置详解

    Tinymce富文本编辑器 本站使用的也是Tinymce富文本编辑器,非常的好用,就是图片上传和编辑配置上有点麻烦,当时没有看完Tinymce官网的资料,导致浪费了点时间,下面就把图片上传的配置分享给 ...

  9. vue项目 vue-quill-editor富文本编辑器+图片上传

    目录 基础使用: 进阶版: 使用 el-upload 图片上传: 进阶版2.0: 可拖动调整图片大小: 基础使用: 富文本编辑器: 此方法得到的图片为base64编码,图片上传在下面. npm ins ...

最新文章

  1. 配置bind主域名服务器
  2. Profiler 使用说明
  3. Graph Embedding方案之DeepWalk
  4. MAP(Mean Average Precision):
  5. 在2008 server上部署域
  6. 数据交换平台_从零开始理解大数据架构之数据交换平台
  7. ASP.NET发送电子邮件
  8. 信息学奥赛一本通 1324:【例6.6】整数区间
  9. 模块化日常:开源库与私有库重名
  10. javascript 高级程序设计_所以 JavaScript 到底是什么?我居然懵了????
  11. 用脆弱性评估流程击败黑客
  12. eclipse JSP页面中的JS代码为黑色,JS单独文件是OK的
  13. android怎么实现记住密码功能,Android实现用户登录记住密码功能
  14. java工程师读音_JAVA怎么念
  15. 计算机技术在中医领域的应用,计算机技术在中医药领域的应用概况.doc
  16. 诺基亚如何利用计算机上网,诺基亚E63的WIFI上网功能全教程
  17. EOS多节点环境部署
  18. 周怎么换算成月_时间序列数据,只找到了周数据,怎么把它转化成月数据?
  19. 进阶无人驾驶—百度Apollo高精度地图
  20. tensorflow object detection api关于csv转换tfrecord格式

热门文章

  1. 随机发牌_用Python制作4人扑克牌发牌游戏
  2. Vite与webpack优势
  3. Java黑皮书课后题第5章:5.13(求满足n的三次方<12000的n的最大值)使用while循环找出满足n的三次方<12000的n的最大值
  4. 后缀数组求最长重复子串
  5. easyUI下datagrid嵌套显示
  6. 蓝桥杯——寻找数组中的最大值
  7. JavaScript原生对象属性和方法详解——String对象
  8. ubuntu安装oracle数据库乱码问题解决方案(超级简单)
  9. 从生活角度学习应用程序、虚拟目录、应用程序池(解惑篇)
  10. boost------signals2的使用1(Boost程序库完全开发指南)读书笔记