UEditor百度富文本后端上传文件接口

1 篇文章 0 订阅
1 篇文章 0 订阅

UEditor百度富文本后端上传文件接口
直接上代码
接口:

@RequestMapping("/UEditorConfig")
    public String list(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String config = environment.getProperty("ueditor.config.dir");
        
        String action = request.getParameter("action");
        String noCache = request.getParameter("noCache");
        if ("config".equals(action))
            return (new ActionEnter(request, config, "")).exec();
        else{
            String encode = request.getParameter("encode");
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            MultipartFile file = multipartRequest.getFile("upfile");

            String fileName = file.getOriginalFilename();
            String contentType = file.getContentType();
            long fileSize = file.getSize();

            String filePath = FileUploadUtils.upload(file);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("state","SUCCESS");
            jsonObject.put("original","myz.png");
            jsonObject.put("size", String.valueOf(fileSize));
            jsonObject.put("title",fileName);
            jsonObject.put("type",contentType);
            jsonObject.put("url",filePath);
            return jsonObject.toString();
        }
        
    }

其实就两种情况

一、读取配置文件

前端加载富文本时会请求一次富文本接口,而且上传文件时也是用的这个接口

if ("config".equals(action))
            return (new ActionEnter(request, config, "")).exec();

注意:ActionEnter是引入百度富文本的包
import com.baidu.ueditor.ActionEnter;

        <dependency>
            <groupId>com.gitee.qdbp.thirdparty</groupId>
            <artifactId>ueditor</artifactId>
            <version>1.4.3.3</version>
        </dependency>

如果接口返回如下内容,说明你成功了,前端应该可以上传文件了(不会报后端配置错误)
在这里插入图片描述

二、上传文件

本质上就是返回一个json,自己构建就好了

else{
            String encode = request.getParameter("encode");
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            MultipartFile file = multipartRequest.getFile("upfile");

            String fileName = file.getOriginalFilename();
            String contentType = file.getContentType();
            long fileSize = file.getSize();

            String filePath = FileUploadUtils.upload(file);
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("state","SUCCESS");
            jsonObject.put("original","myz.png");
            jsonObject.put("size", String.valueOf(fileSize));
            jsonObject.put("title",fileName);
            jsonObject.put("type",contentType);
            jsonObject.put("url",filePath);
            return jsonObject.toString();
        }

注意这里
HttpServletRequest里面的file要从 MultipartHttpServletRequest 拿出来

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("upfile");

FileUploadUtils.upload是我自己框架的上传文件方法,如果你的框架没有,可能需要自己写一个

 String filePath = FileUploadUtils.upload(file);

搞定

在这里插入图片描述
ps:其实读取配置文件的接口本质上也是返回一个json,如果你愿意,也可以自己构建这个json,不引入百度的maven包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值