Spring Boot+SpringMVC使用ueditor(jsp版)

最近遇到了在Spring Boot框架下使用ueditor的情况,由于以前只在ssm框架下使用过,所以记录一下配置过程。

此方式需要下载ueditor的源码。

1、把源码中 com.baidu.ueditor 包下的内容全部复制到项目中,然后把前台静态文件复制到webapp/ueditor目录下,然后把config.json也复制到此目录下,文件结构如下图。

这里写图片描述

2、创建一个自定义的Controller,代码如下:

package cn.bhmc.ueditor.controller;

import java.io.IOException;  
import java.io.PrintWriter;  

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baidu.ueditor.ActionEnter;  

@Controller  
@RequestMapping("/ueditor") 
public class UEditorController {  

    @RequestMapping(value = "/config")  
    public void config(HttpServletRequest request, HttpServletResponse response) {  
        response.setContentType("application/json");  
        String rootPath = request.getSession().getServletContext()  
                .getRealPath("/");  
        try {  
            String exec = new ActionEnter(request, rootPath).exec();
            PrintWriter writer = response.getWriter();  
            writer.write(exec);  
            writer.flush();  
            writer.close();  
        } catch (IOException | JSONException e) {  
            e.printStackTrace();  
        }  
    }
    @RequestMapping("/main")
    public String main(){
        return "base/main/main";
    }

}  

3、修改ueditor.config.js文件中的serverUrl属性,改为 URL + “config”,拦截ueditor的请求到自定义的Controller中。

4、修改config.json中的imageUrlPrefix属性,改为你的项目地址,如:http://localhost:8080/bhmc ,imagePathFormat属性也可以修改,改成自定义的目录,要注意的是,如果imagePathFormat的前面有/的话,imageUrlPrefix的最后就不用加上/了,会重复。

5、最后,需要修改BinaryUploader中的上传源码,把save方法里的源码改为:

try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartRequest.getFile(conf.get("fieldName").toString());

            String savePath = (String) conf.get("savePath");
            String originFileName = multipartFile.getOriginalFilename();
            String suffix = FileType.getSuffixByFilename(originFileName);

            originFileName = originFileName.substring(0,originFileName.length() - suffix.length());
            savePath = savePath + suffix;

            long maxSize = ((Long) conf.get("maxSize")).longValue();

            if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
                return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
            }
            /***********/
            //自定义
            savePath = PathFormat.parse(savePath, originFileName);

            String [] savePathBySplit_temp = savePath.split("/");
            String temp = "";
            String fileName = savePathBySplit_temp[savePathBySplit_temp.length-1];
            for(int i = 1;i < savePathBySplit_temp.length-1; i++){
                if(i!=savePathBySplit_temp.length-2){
                    temp+=savePathBySplit_temp[i]+"/";
                }else{
                    temp+=savePathBySplit_temp[i];
                }
            }
            String pathTemp = request.getSession().getServletContext().getRealPath(temp); 
            System.out.println(pathTemp+","+fileName);
            System.out.println(new File(pathTemp).exists());
            File targetFile = new File(pathTemp);
            if(!targetFile.exists()){  
                targetFile.mkdirs();  
            }
            System.out.println(new File(pathTemp).exists());
            /************/
            //State storageState = StorageManager.saveFileByInputStream(multipartFile.getInputStream(),savePath, maxSize);
            State storageState = StorageManager.saveFileByInputStream(multipartFile.getInputStream(),pathTemp+"/"+fileName, maxSize);

            if (storageState.isSuccess()) {
                storageState.putInfo("url", PathFormat.format(savePath));
                storageState.putInfo("type", suffix);
                storageState.putInfo("original", originFileName + suffix);
            }

            return storageState;

        }catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return new BaseState(false, AppInfo.IO_ERROR);

然后,启动,创建一个编辑框,上传测试:

这里写图片描述

完成。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值