前段时间上网找ueditor可以方便的更改文件上传路径的方法,结果找到的全都是那种写死了在配置文件里的,后来看到了一篇比较有意思的博客,才有了灵感自己写了个ueditor自定义动态更改上传路径。正确来说可以动态更改config.json里的配置项。
让我有灵感的博客地址:https://blog.csdn.net/littlebirdofjava/article/details/53945416
闲话小说
第一步:
新建类继承ueditor里的ActionEnter,具体如下:
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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 net.sf.json.JSONObject;
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;
private String configPath="/text/ueditor/jsp/config.json";//你项目里ueditor的config路径
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());
this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, configPath);//自定义
//重写configManager.getAllConfig()的配置
//System.out.println("上传路径: "+ this.rootPath + request.getRequestURI().substring( contextPath.length() ));
String json=request.getParameter("json");
if(json!=null&&json.length()>0&&this.configManager!=null){
this.rewriteConfigManager(JSONObject.fromObject(json));
}
}
public String exec () {
String callbackName = this.request.getParameter("callback");
//System.out.println("callback=="+callbackName);
if ( callbackName != null ) {
if ( !validCallbackName( callbackName ) ) {
return new BaseState( false, AppInfo.ILLEGAL ).toJSONString();
}
return callbackName+"("+this.invoke()+");";
} else {
return this.invoke();
}
}
public String invoke() {
if ( actionType == null || !ActionMap.mapping.containsKey( actionType ) ) {
return new BaseState( false, AppInfo.INVALID_ACTION ).toJSONString();
}
if ( this.configManager == null || !this.configManager.valid() ) {
return new BaseState( false, AppInfo.CONFIG_ERROR ).toJSONString();
}
State state = null;
int actionCode = ActionMap.getType( this.actionType );
Map<String, Object> conf = null;
//System.out.println("actionCode=="+actionCode);
switch ( actionCode ) {
case ActionMap.CONFIG:
return this.configManager.getAllConfig().toString();
case ActionMap.UPLOAD_IMAGE:
case ActionMap.UPLOAD_SCRAWL:
case ActionMap.UPLOAD_VIDEO:
case ActionMap.UPLOAD_FILE:
//System.out.println(1111);
conf = this.configManager.getConfig( actionCode );
state = new Uploader( request, conf ).doExec();
break;
case ActionMap.CATCH_IMAGE:
conf = configManager.getConfig( actionCode );
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 );
int start = this.getStartIndex();
state = new FileManager( conf ).listFile( start );
break;
}
return state.toJSONString();
}
/**
* 重写配置
* @param json
* @return
*/
private void rewriteConfigManager(JSONObject json){
Iterator<?> keys=json.keys();
while(keys.hasNext()){
Object key=keys.next();
//System.out.println(key.toString()+"========="+json.get(key.toString()));
this.configManager.getAllConfig().put(key.toString(), json.get(key.toString()));
}
}
}
第二步,打开ueditor/jsp路径下的controller.jsp,修改里面调用方法out.write(new ActionEnter( request, rootPath).exec()),修改为out.write(new UeditorActionEnter( request, rootPath).exec()),当然 如下:
<%-- <%@ page language="java" contentType="text/html; charset=UTF-8"
import="com.baidu.ueditor.ActionEnter"
pageEncoding="UTF-8"%> --%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="com.dc.common.ueditor.UeditorActionEnter"
pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%
request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html");
String rootPath = application.getRealPath( "/" );
//System.out.println(rootPath);
//out.write( new ActionEnter( request, rootPath ).exec() );
//JSONObject json=JSONObject.fromObject(request.getParameter("json"));
//json.put("imagePathFormat","/uploadImg/goodsDetail/{yyyy}{mm}{dd}/{time}{rand:6}");
out.write(new UeditorActionEnter( request, rootPath).exec());
%>
最后的最后,直接在你所要的页面中如下调用就行了:
//实例化编辑器
//建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例
//ue = UE.getEditor('editor');
var config={
"imagePathFormat":'uploadImage/goodsDetail/{yyyy}/{yyyy}{mm}{dd}{hh}{ii}{ss}_{rand:6}'//图片上传路径
}//重新初始化config.json里的数据
//getRootPath()是获取项目的根路径如:了http://127.0.0.1:8080/Myproject
ue = UE.getEditor('editor', {serverUrl:getRootPath()+"/ueditor/jsp/controller.jsp?json="+encodeURI(JSON.stringify(config))});
好了就这样了