springboot集成ueditor,自定义后台接口,使用七牛云

ueditor为什么无法在springboot中使用

官方提供的Java后端接口是 ueditor/jsp/controller.jsp。 jsp在springboot中存在一些问题,导致ueditor不能正确使用。
1、spingboot的视图可能不使用jsp
2、spingboot项目发布为jar包时,获取不到/ueditor/jsp/config.json文件。

解决思路

这时我们就要自定义controller,来处理ueditor的请求。
ueditor的请求主要分2中,获取配置和上传。
1、当ueditor初始化时,发起请求获取配置文件。请求地址 ueditor/jsp/controller.jsp?action=config。 该接口默认返回/ueditor/jsp/config.json内容。
2、当上传文件/图片时候,会发起请求 ueditor/jsp/controller.jsp?action=uploadXXX。
action的值再/ueditor/jsp/config.json 中搜索ActionName。

解决过程

一、修改配置文件

打开 plugins/ueditor/ueditor.config.js。这里修改URL和serverUrl 2个字段
URL 是ueditor代码根目录地址
serverUrl 是 /Resources/plugins/ueditor/jsp/controller
在这里插入图片描述

自定义controller接收请求,RequestMapping要和上一步中的serverUrl 对应

@Controller
@Transactional
@RequestMapping("Resources/plugins/ueditor/jsp")
public class JSPController {

    @Autowired
    IMgrFileUploadService mgrFileUploadService;

    @RequestMapping("/controller")
    public void getConfigInfo(HttpServletRequest request, HttpServletResponse response, String action, MultipartFile upfile) throws IOException {
        if (action.equals("config")) {
            //重定向到配置文件
            response.sendRedirect("/Resources/plugins/ueditor/jsp/config.json");
            return;
        }
        if (action.contains("upload")) {
            //文件上传方法,自己实现一下
            ApiResult apiResult = mgrFileUploadService.uploadMultipartFile(upfile, "img");

            Map<String, Object> result = (Map<String, Object>) apiResult.getData();

            //ueditor要求返回格式
            Map<String, Object> map = new HashMap<>();
            //state成功必须返回SUCCESS
            map.put("state", "SUCCESS");
            //图片地址,/ueditor/jsp/config.json配置中配置的图片前缀为"",所以这里返回完成地址
            map.put("url", result.get("img_src"));
            map.put("title", result.get("original"));
            map.put("original", result.get("original"));
            map.put("type", "jpg");
            map.put("size", result.get("size"));

            //返回json
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            response.setHeader("Access-Control-Allow-Origin", "*");
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.append(JSON.toJSONString(map));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    }
}

测试

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值