UEditor Java版更改上传文件路径

来源:互联网 发布:进入telnet端口命令 编辑:程序博客网 时间:2024/06/06 03:54
宽为限 紧用功 功夫到 滞塞通

玩过UEditor的大家应该都遇到过这样的问题,哈哈,终于把它放到项目上去了,编辑图片上传附件啥的挺爽的。后来突然发现显示页面不显示上传的图片了,去上传目录一看,刚刚上传的东西全没了。。。这就尴尬了

没错是重启tomcat服务的时候被tomcat给清理了,这样的话我们就不能把上传的资源放到tomcat运行的项目目录下了。问题是 config.json 文件里配的路径都是相对路径,都配置到项目上的,所以改这个是不行的了。那我们只能更改它上传文件用的 java文件也就是controller.jsp文件里的 ActionEnter,我们给他重写一下,把路径改为自己指定的路径。

重写的代码如下

package cn.lovelyc.soluser.official.util.ueditor;import com.baidu.ueditor.ActionEnter;import com.baidu.ueditor.ConfigManager;import com.baidu.ueditor.define.ActionMap;import com.baidu.ueditor.define.AppInfo;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;public class UeditorActionEnter extends ActionEnter {    private HttpServletRequest request = null;    private String rootPath = null;    private String contextPath = null;    private String actionType = null;    private ConfigManager configManager = null;    public UeditorActionEnter(HttpServletRequest request, String rootPath) {        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());    }    @Override    public String invoke() {        if (actionType == null || !ActionMap.mapping.containsKey(actionType)) {            String jsonString = new BaseState(false, AppInfo.INVALID_ACTION).toJSONString();            return jsonString;        }        if (this.configManager == null || !this.configManager.valid()) {            String jsonString = new BaseState(false, AppInfo.CONFIG_ERROR).toJSONString();            return jsonString;        }        State state = null;        int actionCode = ActionMap.getType(this.actionType);        Map<String, Object> conf = null;        switch (actionCode) {        case ActionMap.CONFIG:            String string = this.configManager.getAllConfig().toString();            return string;        case ActionMap.UPLOAD_IMAGE:        case ActionMap.UPLOAD_SCRAWL:        case ActionMap.UPLOAD_VIDEO:        case ActionMap.UPLOAD_FILE:            conf = this.configManager.getConfig(actionCode);            //注意再这里修改rootPath和savePath,上传的实际路径为rootPath+savePath            conf.put("rootPath", "/soluser/official/");            conf.put("savePath", "/upload/" + conf.get("savePath"));            state = new Uploader(request, conf).doExec();            break;        case ActionMap.CATCH_IMAGE:            conf = configManager.getConfig(actionCode);            //注意再这里修改rootPath和savePath,上传的实际路径为rootPath+savePath            conf.put("rootPath", "/soluser/official/");            conf.put("savePath", "/upload/" + conf.get("savePath"));            String[] list = this.request.getParameterValues((String) conf.get("fieldName"));            state = new ImageHunter(conf).capture(list);            break;        case ActionMap.LIST_IMAGE:        case ActionMap.LIST_FILE:            conf = configManager.getConfig(actionCode);            //注意再这里修改rootPath和savePath,上传的实际路径为rootPath+savePath            conf.put("rootPath", "/soluser/official/");            conf.put("savePath", "/upload/" + conf.get("savePath"));            conf.put("dir", conf.get("dir"));            int start = this.getStartIndex();            state = new FileManager(conf).listFile(start);            break;        }        return state.toJSONString();    }}

controller.jsp 这里也跟着改

<%@ page language="java" contentType="text/html; charset=UTF-8"    import="cn.lovelyc.soluser.official.util.ueditor.UeditorActionEnter"    pageEncoding="UTF-8"%><%@ page trimDirectiveWhitespaces="true" %><%    request.setCharacterEncoding( "utf-8" );    response.setHeader("Content-Type" , "text/html");    String rootPath = application.getRealPath( "/" );    out.write( new UeditorActionEnter( request, rootPath ).exec() );%>

config.json

"imageUrlPrefix": "/soluser/official", /* 图片访问路径前缀 */"imagePathFormat": "/describe/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */

注意:访问路径前缀一定要加哦!

上传图片试试
这里写图片描述

这里回显是报错了,404。我们看看它实际上传成功没
这里写图片描述

我们发现它是根据你重写的路径+imagePathFormat 配置路径格式是保存了的。嗯,保存到这里的话就不怕被tomcat给清理掉了,but我们怎么获取到实际磁盘路径下的文件呢?
对了,我们可以在tomcat的配置文件里加一个路径映射 \(^o^)/YES!
修改tomcat conf目录下的 server.xml文件。

look

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">    ...    <!-- 本地磁盘映射 -->    <Context path="/soluser/official" docBase="D:\soluser\official"></Context></Host>

OK!我们再重启试试
这里写图片描述

哈哈,你的小阔爱已上线!

站在巨人的肩膀上

本篇博文参考的文章
http://blog.csdn.net/littlebirdofjava/article/details/53945416
http://blog.csdn.net/xiaoyu19910321/article/details/51363679
http://blog.csdn.net/sinat_34803353/article/details/53292284?locationNum=3&fps=1
http://www.bubuko.com/infodetail-1142341.html

原创粉丝点击