UEditor扩展上传
2015-07-27 08:30
609 查看
1.修改配置文件ueditor.config.js,重写后台请求方法,serverUrl
2.对post/get传递的进行重写
3.常量参数
4.方法:从文件中获取配置信息,转成json对象
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]*?\\*/", ""); } }
相关文章推荐
- H - Can you answer these queries? - (区间查询更新)
- Android MenuItem 设置文本颜色-TextColor设置
- Leetcode[232] Implement Queue using Stacks
- request_response详解
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
- 三十而立,从零开始学ios开发(十二):Table Views(中)UITableViewCell定制 - minglz - 博客园
- easyUI校验封装
- 设置UIButton,UITextFild边框圆角(上半边或下半边)
- iOS开发——UI进阶篇(六)键盘处理
- UI-框架---快切
- Key-value存储简介
- html中类似 <input type=“text” name="username[]" value="1">中的方括号的含义
- Unique path ii
- Leetcode #60 Permutation Sequence
- MarqueeTextview(跑马灯)
- UIScrollView的属性总结
- UI___UIImageView
- php中break与continue的区别
- CDOJ 485 UESTC 485 Game (八数码变形,映射,逆cantor展开)
- UGUI下拉列表控件