ueditor 添加字号大小_springboot集成ueditor笔记

4c282429e46bdf5f14ea0643ea499476.png

环境配置如下

IntelliJ IDEA 2018.3.5 x64
spring-boot2.0.2
apache-maven-3.5.4
ueditor1_4_3_2-utf8-jsp

新建一个springboot web项目,将下载好的资源文件放入static文件夹下的ueditor文件夹;

4077deb3c5fa7c35aaab49a1603b4f9b.png

资源文件存放目录如图(index.html被我刪除了,所以图中未显示)

70a964073c9e991f8d0830b332713f7b.png

引入资源后将jsp文件夹下lib包注入项目,(Idea直接ctrl+shift+alt+s)

8c3976022207509033ecfdbf2c38e790.png

修改01;

fc6df2eb106c7aed35bfe4d9c224f920.png

修改02

/**
 *
 */
package com.book.book.contorller;

import java.io.*;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSON;
import com.book.book.dto.UeditorConfig;

/**
 * @author Administrator
 *
 */

@Controller
public class UeditorContorller {

    private static final Logger log = LoggerFactory.getLogger(UeditorContorller.class);

    @RequestMapping("/index")
    public String getAddress() {
        System.out.println("进入页面");
        return "index";
    }

    /**
     * 根目录请求
     * @author zpY
     * @version V1.0
     */

    @ResponseBody
    @RequestMapping("ueditorConfig")
    public Object ueditorConfig(MultipartFile upfile, HttpServletRequest request, HttpServletResponse response, String action) {
        switch (action) {
            case "config":
                log.info("读取config.json");
                Object uEditorConfig = this.getUEditorConfig(response);
                return uEditorConfig;
            case "uploadimage":
                log.info("图片上传");
                String returnJson = this.uploadimage(upfile, request, response);
                return returnJson;
        }
        return "请求失败";
    }

    /**
     * 读取ueditor/config.json
     * @author zpY
     * @date 2019年5月25日 下午7:06:05
     * @version V1.0
     */
    public Object getUEditorConfig(HttpServletResponse response) {
        try {
            response.setContentType("application/json;charset=utf-8");
            Resource resource = new ClassPathResource("static/ueditor/jsp/config.json");
            BufferedReader br = new BufferedReader(new FileReader(resource.getFile()));
            StringBuilder stringBuilder = new StringBuilder();
            String configJson;
            while ((configJson = br.readLine()) != null) {
                stringBuilder.append(configJson);
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            log.error("读取ueditor/config.json异常" + e);
            return "error";
        }
    }

    /**
     * 上传图片
     * @author zpY
     * @date 2019年5月25日 下午7:16:47
     * @version V1.0
     */
    public String uploadimage(MultipartFile upfile, HttpServletRequest request, HttpServletResponse response) {
        UeditorConfig ueditorConfig = new UeditorConfig();
        try {
            try {
                String fileName = upfile.getOriginalFilename();
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                fileName = UUID.randomUUID()+suffixName;
                String visitUrl = "http://localhost:8081/boot/"+fileName;
                //自定义存放路径
                String filePath = "F:/book/book/target/classes/uploadimage/";
                try {
                    upfile.transferTo(new File(filePath+fileName));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // 返回状态码
                ueditorConfig.setState("SUCCESS");
                ueditorConfig.setUrl(visitUrl);
                ueditorConfig.setSize(upfile.getSize());
                ueditorConfig.setOriginal(fileName);
                ueditorConfig.setType(upfile.getContentType());

            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
            ueditorConfig.setState("ERROR");
        }
        String returnJson = JSON.toJSONString(ueditorConfig);
        log.info("图片上传返回字符===>>>" + returnJson);
        return returnJson;
    }


}

dto

/**
 *
 */
package com.book.book.dto;


import lombok.Data;
import org.springframework.boot.autoconfigure.domain.EntityScan;

/**
 *
 * @author Administrator
 * {state:”状态”,url:”回显路径”,size:”大小”,type:”类型”,title:”文件title”,original:”名称”}
 */

@Data
@EntityScan
public class UeditorConfig {
    private String state;
    private String url;
    private long size;
    private String type;
    private String title;
    private String original;

}

上面两步正常走完后基本能够正常上传单图和多图,只是无法正常显示上传成功的图片到编辑器;

修改03;

335890eb8ec4273c7f26f9d579ee0db6.png

代码层面就三步,这篇笔记是在网上找了各路大神参考而来,基本来自CSDN/简书;

01,正常读取config.json

ca433ef9c4c2acd2b17955a151a0c807.gif

顺嘴说一句,ueditor传文件有限制,如果没有在application.properties 加限制配置,则该版本ueditor默认上传文件大小

【The field upfile exceeds its maximum permitted size of 1048576 bytes.

为了适应实际情况,可以在application.properties 添加如下配置,大小根据实际系统条件设置

a1fd82b28232026db6fce9d79fc259d1.png

///

当然,如果正常走到这里,那么这个容器是已经可以正常使用单图/多图上传,改变样式之类的服务了,但在正常业务场景中来说,添加和修改是必定是双生子,那么就需要这个富文本把已填写的数据保持原样回显回这个容器内方便人们对不满意的地方进行修改,此处修改如下:

<body>
<!--后端数据返回待回显数据(带标签的内容) -->
<input  type="hidden" th:value="${bookDetails}" id="bookDetail"/>
    <p><span>&nbsp;&nbsp;图文详情</span><br/></p>
    <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
    <!--保存到数据库信息回显加载到富文本 -->
    <script type="text/javascript">
    var ue = UE.getEditor('editor');
    var bookDetail = document.getElementById("bookDetail").value;
    //监听编辑器是否初始化完毕,对应赋值到编辑容器
    $(document).ready(function () {
        ue.ready(function () {
            if (!!bookDetail){
                <!--回显数据填充加载到富文本框 -->
                ue.setContent(""+bookDetail+"");
                <!--用户未选择编辑事件前,该容器将关闭修改服务 -->
                ue.setDisabled();
            }
        });
    });
    </script>
    <style type="text/css">div {   width: 100%;   }</style>
    <div id="btns">
        <div>
            <button onclick="getimgContent()">提交图文信息</button>
        </div>
    </div>
</body>

这样,当需要编辑它的时候,请求携带待回显数据进入编辑页面,该容器自然会成功初始化内容;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值