UEditor扩展上传

来源:互联网 发布:天刀女性明星捏脸数据 编辑:程序博客网 时间:2024/04/30 20:35

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)@ResponseBodypublic 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)@ResponseBodypublic 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");//TODOreturnMsg.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]*?\\*/", "");}}



0 0
原创粉丝点击