重启tomcat服务的时候被tomcat给清理了,这样的话我们就不能把上传的资源放到tomcat运行的项目目录下了。问题是 config.json 文件里配的路径都是相对路径,都配置到项目上的,所以改这个是不行的了。那我们只能更改它上传文件用的 java文件也就是controller.jsp文件里的 ActionEnter,我们给他重写一下,把路径改为自己指定的路径。
重写的代码如下
1.新建UeditorActionEnter继承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();
}
}
2.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() );
%>
3.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文件。
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
...
<!-- 本地磁盘映射 -->
<Context path="/soluser/official" docBase="D:\soluser\official"></Context>
</Host>
原文链接:https://blog.csdn.net/GMingZhou/article/details/77427793