springboot + ueditor + 上传的文件存放到其他磁盘目录中

在使用springboot + ueditor中的时候,我需要把图片放到非tomcat发布项目的目录中。

我自定义了一个UeditorActionEnter:



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.Base64Uploader;
import com.baidu.ueditor.upload.BinaryUploader;
import com.baidu.ueditor.upload.IStorageManager;
import com.baidu.ueditor.upload.StorageManager;

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

/**
 * 自定义ActionEnter,用来更改文件保存地址
 */
public class UeditorActionEnter extends ActionEnter {

    private HttpServletRequest request;
    private String rootPath;
    private String contextPath;
    private String actionType;
    private ConfigManager configManager;
    private IStorageManager storage;

    public UeditorActionEnter(HttpServletRequest request, String rootPath) {
        this(new StorageManager(), request, rootPath, (String)null);
    }

    public UeditorActionEnter(HttpServletRequest request, String rootPath, String configPath) {
        this(new StorageManager(), request, rootPath, configPath);
    }

    public UeditorActionEnter(IStorageManager storage, HttpServletRequest request, String rootPath) {
        this(storage, request, rootPath, (String)null);
    }

    public UeditorActionEnter(IStorageManager storage, HttpServletRequest request, String rootPath, String configPath) {
        super(storage, request, rootPath, configPath);
        this.request = null;
        this.rootPath = null;
        this.contextPath = null;
        this.actionType = null;
        this.configManager = null;
        this.storage = storage;
        this.request = request;
        this.rootPath = rootPath;
        this.actionType = request.getParameter("action");
        this.contextPath = request.getContextPath();
        if (configPath == null) {
            configPath = request.getParameter("configPath");
            if (configPath == null) {
                configPath = request.getRequestURI();
            }
        }

        this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, configPath);
    }

    public String exec() {
        String callbackName = this.request.getParameter("callback");
        if (callbackName != null) {
            return !this.validCallbackName(callbackName) ? (new BaseState(false, 401)).toJSONString() : callbackName + "(" + this.invoke() + ");";
        } else {
            return this.invoke();
        }
    }

    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<String, Object> 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","C:/image/education");
                        String filedName = (String)conf.get("fieldName");
                        if ("true".equals(conf.get("isBase64"))) {
                            state = (new Base64Uploader(this.storage)).save(this.request.getParameter(filedName), conf);
                        } else {
                            state = (new BinaryUploader(this.storage)).save(this.request, conf);

                        }
                        break;
                    case 5:
                        conf = this.configManager.getConfig(actionCode);
                        String[] list = this.request.getParameterValues((String)conf.get("fieldName"));
                        state = (new ImageHunter(this.storage, conf)).capture(list);
                        break;
                    case 6:
                    case 7:
                        conf = this.configManager.getConfig(actionCode);
                        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();
        }
    }

    public int getStartIndex() {
        String start = this.request.getParameter("start");

        try {
            return Integer.parseInt(start);
        } catch (Exception var3) {
            return 0;
        }
    }

    public boolean validCallbackName(String name) {
        return name.matches("^[a-zA-Z_]+[\\w0-9_]*$");
    }
}

就这样,在invoke的switch的case = 4中,设置了conf的rootPath的值为你想要保存的目录就可以了。

发布了145 篇原创文章 · 获赞 6 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/m0_37626203/article/details/103508340
今日推荐