百度Editor富文本编辑框保存图片路径到项目之外

最近项目中有使用百度Editor富文本编辑框,但是发现保存图片是保存在项目路径里面的,这样就存在一个很严重的问题,项目重新部署图片就都被删除了,研究了一下午也没结局,后来发现controller.jsp中使用了百度Editor富文本编辑框保存图片路径到项目之外_第1张图片

这个类, 发现这个类正是保存图片所使用的,也是想到了重写此类,修改保存路径

import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.ConfigManager;
import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.baidu.ueditor.hunter.FileManager;
import com.baidu.ueditor.hunter.ImageHunter;
import com.baidu.ueditor.upload.Uploader;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 
 * @Description:
 * @date Created in 2018/3/9 8:32
 */
public class SimpleActionEnter extends ActionEnter {
    private HttpServletRequest request = null;
    private String rootPath = null;
    private String contextPath = null;
    private String actionType = null;
    private ConfigManager configManager = null;

    //图片存储路径
    private String saveRootPath;
    public SimpleActionEnter(HttpServletRequest request, String rootPath, String saveRootPath) {
        super(request,rootPath);
        this.request = request;
        this.rootPath = rootPath;
        this.actionType = request.getParameter("action");
        this.contextPath = request.getContextPath();
        this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, request.getRequestURI());
        this.saveRootPath = saveRootPath;
    }

    @Override
    public String invoke() {
        if(this.actionType != null && ActionMap.mapping.containsKey(this.actionType)) {
            if(this.configManager != null && this.configManager.valid()) {
                State state = null;
                int actionCode = ActionMap.getType(this.actionType);
                Map conf = null;
                switch(actionCode) {
                    case 0:
                        return this.configManager.getAllConfig().toString();
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        conf = this.configManager.getConfig(actionCode);
                        //重点,这里覆盖了原图片的存储路径
                        conf.put("rootPath",this.saveRootPath);
                        state = (new Uploader(this.request, conf)).doExec();
                        break;
                    case 5:
                        conf = this.configManager.getConfig(actionCode);
                        //重点,这里覆盖了原图片的存储路径
                        conf.put("rootPath",this.saveRootPath);
                        String[] list = this.request.getParameterValues((String)conf.get("fieldName"));
                        state = (new ImageHunter(conf)).capture(list);
                        break;
                    case 6:
                    case 7:
                        conf = this.configManager.getConfig(actionCode);
                        //重点,这里覆盖了原图片的存储路径
                        conf.put("rootPath",this.saveRootPath);
                        int start = this.getStartIndex();
                        state = (new FileManager(conf)).listFile(start);
                }

                return state.toJSONString();
            } else {
                return (new BaseState(false, 102)).toJSONString();
            }
        } else {
            return (new BaseState(false, 101)).toJSONString();
        }
    }
}

然后再修改controller.jsp中引用的类

<%@ page language="java" contentType="text/html; charset=UTF-8"
	import="com.baidu.ueditor.ActionEnter"
    pageEncoding="UTF-8"%>
<%@ page import="cn.com.thit.tram.util.SimpleActionEnter" %>
<%@ page import="java.util.ResourceBundle" %>
<%@ page import="cn.com.thit.tram.util.CommonUtil" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%

    request.setCharacterEncoding( "utf-8" );
	response.setHeader("Content-Type" , "text/html");
	
	String rootPath = application.getRealPath( "/" );
	
	//文件存储路径,可以读取配置文件
	String saveRootPath = CommonUtil.UPLOAD_PATH;
	//获取图片路径前缀,可以读取配置文件;
	//重点在这里,使用ActionEnterPlugs我们刚在新建的类
	String result = new SimpleActionEnter( request, rootPath ,saveRootPath).exec();
	out.write( result );
//out.write( new ActionEnter( request, rootPath ).exec() );
%>

大功告成,完美解决,

发现图片前缀还是在config.json中修改, 没有找到重写的地方, 配置起来比较麻烦,不能定为常量, 有谁知道可以告诉下


你可能感兴趣的:(前端,后端)