wangEditor使用教程

1、官网地址

https://www.wangeditor.com/

2、页面引入js

<script th:src="@{/js/wangEditor4.7.6.js}"></script>

3、代码

<div id="inspectionContents"></div>

 4、js代码

        const E = window.wangEditor
        const editor1 = new E('#inspectionContents');
        editor1.config.showLinkImg = false
        //图片上传,后端接口
        editor1.config.uploadImgServer=prefix+'/uploadImg'
        editor1.config.debug=true;
        editor1.config.uploadFileName = 'file'
        editor1.create();

5、后端代码

    @RequestMapping("/uploadImg")
    @ResponseBody
    public String uploadImg(MultipartFile file, HttpServletRequest request)throws Exception
    {
        return dailyInspectionService.uploadImg(file, request);
    }
    @Override
    public String uploadImg(MultipartFile file, HttpServletRequest request) {
        //协议名 http // https
        String scheme = request.getScheme();
        //域名
        String serverName = request.getServerName();
        //项目名
        String contextPath = request.getContextPath();
        //路径
        String url=scheme+"://"+serverName+":"+port+contextPath;
        //文件名
        String filename = file.getOriginalFilename();
        try {
            saveImg(file.getInputStream(),filename);
        }catch (Exception e){
            throw new RuntimeException("图片上传失败");
        }
        JSONObject object=new JSONObject();
        List<ReturnData> list=new ArrayList<>();
        ReturnData data=new ReturnData();
        data.setUrl(url+"/file/"+filename);
        data.setAlt("图片");
        list.add(data);
        object.put("errno","0");
        object.put("data",list.toArray());
        return object.toJSONString();
    }
    /**
     * 图片处理
     *
     * @param stream
     * @param filename
     * @throws Exception
     */
    private void saveImg(InputStream stream, String filename)throws Exception{
        String                 path=this.getClass().getClassLoader().getResource("").getPath()+"/static/file/";
        File file=new File(path);
        String str=file.getPath()+File.separator+filename;
        FileOutputStream fs=new FileOutputStream(str);
        byte[] buffer =new byte[1024*1024];
        int bytesum = 0;
        int byteread = 0;
        while ((byteread=stream.read(buffer))!=-1) {
            bytesum+=byteread;
            fs.write(buffer,0,byteread);
            fs.flush();
        }
        fs.close();
        stream.close();
    }

6、图片上传对应的后端接口返回类型,wangEditor官网有详细的说明。以上方法是将图片上传到target/static/file/路径下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值