一、前言
在项目中使用了百度富文本,发现图片上传的路径是项目的路径,但是现在要求将图片存储在项目之外的地方,如D:/ 盘等,按照API配置貌似无法做到这个要求,那么就需要稍微修改源码了,以下是本人解决方案,仅供参考
二、解决方案
前提是已经引入百度富文本,并且能够正常使用,若还无法正常使用,请阅读API
第一步:新建ActionEnterPlugs 并继承ActionEnter(com.baidu.ueditor包内)
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 java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* 重写百度富文本类,使其具有存储非项目路径
* Created by 韩信 on 2017-12-18.
*/
public class ActionEnterPlugs 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 ActionEnterPlugs(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<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",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="com.xjkj.baidu.ueditor.plugs.ActionEnterPlugs" %>
<%@ page import="com.xjkj.common.util.PropertiesFileUtil" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%
request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html");
String rootPath = application.getRealPath( "/" );
//文件存储路径,可以读取配置文件
String saveRootPath = PropertiesFileUtil.getInstance().get("SYSTEM_UPLOAD_IMAGE");
//获取图片路径前缀,可以读取配置文件
//String getFilePath = PropertiesFileUtil.getInstance().get("SYSTEM_DOWN_IMAGE");
String action = request.getParameter("action");
//重点在这里,使用ActionEnterPlugs我们刚在新建的类
String result = new ActionEnterPlugs( request, rootPath ,saveRootPath).exec();
if( action!=null && (action.equals("listfile") || action.equals("listimage") ) ){
rootPath = saveRootPath.replace("\\", "/");
result = result.replaceAll(saveRootPath, "/");
}
//result = result.replaceAll("/file",getFilePath);
out.write( result );
%>
到此已经修改完毕,现在图片已经存储在你配置文件中配置的存储路径。