您的位置:首页 > 产品设计 > UI/UE

UEditor扩展上传

2015-07-27 08:30 609 查看
1.修改配置文件ueditor.config.js,重写后台请求方法,serverUrl

if(!window.UEDITOR_HOME_URL){
window.UEDITOR_HOME_URL = "/lw-component/component/module/ueditor/";
}

var URL = window.UEDITOR_HOME_URL || getUEBasePath();

/**
* 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
*/
window.UEDITOR_CONFIG = {

//为编辑器实例添加一个路径,这个不能被注释
UEDITOR_HOME_URL: URL

// 服务器统一请求接口路径
, serverUrl: "/xxx-component/component/ueditor/exec"


2.对post/get传递的进行重写

@Controller
@RequestMapping("/component/ueditor")
public class UEditorController extends SpringController {

@RequestMapping(value = "/exec", method = RequestMethod.GET)
@ResponseBody
public void exec(){
String jsonFilePath=getRequest().getServletContext().getRealPath(UEditorConstant.CONFIG_FILEPATH);
String actionType = getRequest().getParameter("action");
if(actionType.equals(UEditorConstant.CONFIG)){
this.print(UEditorUtil.getAllConfig(jsonFilePath));
}else{
Map<String, Object> config = UEditorUtil.getConfig(actionType, jsonFilePath);
this.print(JsonUtil.map2Json(config));
}
}

@RequestMapping(value = "/exec", method = RequestMethod.POST)
@ResponseBody
public void exec(@RequestParam(value = "upfile") MultipartFile file){
String jsonFilePath=getRequest().getServletContext().getRealPath(UEditorConstant.CONFIG_FILEPATH);
String actionType = getRequest().getParameter("action");

if(actionType.equals(UEditorConstant.CONFIG)){
this.print(UEditorUtil.getAllConfig(jsonFilePath));
}else{
Map<String, Object> returnMsg=new HashMap<String, Object>();
//上传图片
String filename = file.getOriginalFilename();
returnMsg.put("original", filename);
returnMsg.put("title", filename);
String filePath=UEditorConstant.STORE_DIR_UEDITOR+AttachmentUtil.getRelativePath();
File dir=new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
String newFileName = UUID.getUUID() + "." + FilenameUtils.getExtension(filename);
File newFile = new File(dir+ "/"+ newFileName);
try {
//保存图片
file.transferTo(newFile);
returnMsg.put("state", "SUCCESS");
//TODO
returnMsg.put("url", "");
} catch (IOException e) {
e.printStackTrace();
}
this.print(JsonUtil.map2Json(returnMsg));
}
}


3.常量参数
public class UEditorConstant {
//初次加载配置
public static final String CONFIG = "config";
//上传图片
public static final String UPLOAD_IMAGE = "uploadimage";
//上传涂鸦
public static final String UPLOAD_SCRAWL = "uploadscrawl";
//上传视频
public static final String UPLOAD_VIDEO = "uploadvideo";
//上传文件
public static final String UPLOAD_FILE = "uploadfile";
//抓图截图
public static final String CATCH_IMAGE = "uploadimage";
//列出文件
public static final String LIST_FILE = "listfile";
//列出图片
public static final String LIST_IMAGE = "listimage";

//配置文件的路径
public static final String CONFIG_FILEPATH = "/component/module/ueditor/jsp/config.json";

//富文本图片的保存地址
public static final String STORE_DIR_UEDITOR = "D:"+  "/"  +"upload"+ "/" +"ueditor"+ "/";
}


4.方法:从文件中获取配置信息,转成json对象

public class UEditorUtil {

public static String getAllConfig(String jsonFilePath){
return readJsonFile(jsonFilePath);
}

public static Map<String, Object> getConfig(String type,String jsonFilePath) {
JSONObject jsonConfig=JSONObject.parseObject(readJsonFile(jsonFilePath));
Map<String, Object> conf = new HashMap<String, Object>();
if(type.equals(UEditorConstant.UPLOAD_IMAGE)){//上传图片
conf.put("imageMaxSize", jsonConfig.getLong("imageMaxSize"));
conf.put("imageAllowFiles", jsonConfig.getJSONArray("imageAllowFiles"));
conf.put("imageFieldName", jsonConfig.getString("imageFieldName"));
conf.put("imageCompressEnable", jsonConfig.getBoolean("imageCompressEnable"));
conf.put("imageCompressBorder", jsonConfig.getInteger("imageCompressBorder"));
}
return conf;
}

private static String readJsonFile(String path){
StringBuilder builder = new StringBuilder();
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(path));
BufferedReader bufferedReader = new BufferedReader(reader);
String tempContent = null;
while ((tempContent=bufferedReader.readLine())!=null) {
builder.append(tempContent);
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return filter(builder.toString());
}
private static String filter(String input){
return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: