UEditor 是由百度「FEX前端研发团队」开发的所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码。我们在做网站文字编辑的时候会经常用到它。但是ueditor默认支持保存图片在网站的根目录下面。但是,对于网站来说,我们大多数的时候都要把静态资源保存到另外的服务器上,与应用服务器分开,以增加网站的性能
下面介绍一下这个方法:
一,
ueditor 1.4.2+ 推荐使用唯一的请求地址,通过GET参数action指定不同请求类型。 但很多用户都希望使用自己写好的上传地址,下面提供一种解决方法: 由于所有ueditor请求都通过editor对象的getActionUrl方法获取请求地址,可以直接通过复写这个方法实现,把这段js单独写到一个js中,加入的网页,并且在放在引用uditor的js之后。例子如下:
UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl; UE.Editor.prototype.getActionUrl = function(action) { if (action == ‘uploadimage‘ || action == ‘uploadscrawl‘ || action == ‘uploadimage‘) { return ‘http://a.b.com/upload.php‘; } else if (action == ‘uploadvideo‘) { return ‘http://a.b.com/video.php‘; } else { return this._bkGetActionUrl.call(this, action); } }
package com.gametech.controller; import java.io.File; import java.io.IOException; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import com.alibaba.fastjson.JSON; import com.gametech.entity.ReturnUpLoadImage; import com.gametech.utils.StringUtils; @Controller @RequestMapping("upload") public class UploadController { /** * * @return */ @RequestMapping("upImage") @ResponseBody public String upImage(HttpServletRequest request, HttpServletResponse response) { // 创建一个通用的多部分解析器 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); String[] filenames = null; // 判断 request 是否有文件上传,即多部分请求 //{"state": "SUCCESS","title": "1437299650668072896.jpg","original": "rdn_508f4a9624572.jpg","type": ".jpg","url": "/ueditor/upload/image/20150719/1437299650668072896.jpg","size": "14262"} ReturnUpLoadImage upLoadImage = new ReturnUpLoadImage(); upLoadImage.setUrl("upload/1.jpg"); if (multipartResolver.isMultipart(request)) { // 转换成多部分request MultipartHttpServletRequest multiRequest = multipartResolver.resolveMultipart(request); filenames = new String[multiRequest.getFileMap().size()]; // 取得request中的所有文件名 Iterator<String> iter = multiRequest.getFileNames(); int i = 0; while (iter.hasNext()) { // 记录上传过程起始时的时间,用来计算上传时间 int pre = (int) System.currentTimeMillis(); // 取得上传文件 MultipartFile file = multiRequest.getFile(iter.next()); if (file != null) { // 取得当前上传文件的文件名称 String myFileName = file.getOriginalFilename(); upLoadImage.setOriginal(myFileName); // 如果名称不为“”,说明该文件存在,否则说明该文件不存在 if (myFileName.trim() != "") { System.out.println(myFileName); // 重命名上传后的文件名 String fileName = "demoUpload" + file.getOriginalFilename(); upLoadImage.setTitle(fileName); filenames[i] = fileName; i++; // 定义上传路径 String path = "E:/" + fileName; File localFile = new File(path); try { file.transferTo(localFile); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // 记录上传该文件后的时间 int finaltime = (int) System.currentTimeMillis(); System.out.println(finaltime - pre); } } upLoadImage.setType(".jpg"); upLoadImage.setSize("1024"); String result = JSON.toJSONString(upLoadImage); result = StringUtils.chinaToUnicode(result); return result; //return "{\"state\": \"SUCCESS\",\"title\": \"1437300241099035569.jpg\",\"original\": \"QQ\u622a\u56fe20150327230133.jpg\",\"type\": \".jpg\",\"url\": \"/ueditor/upload/image/20150719/1437300241099035569.jpg\",\"size\": \"463908\"}"; } }
/** * 把中文转成Unicode码 * @param str * @return */ public static String chinaToUnicode(String str){ String result=""; for (int i = 0; i < str.length(); i++){ int chr1 = (char) str.charAt(i); if(chr1>=19968&&chr1<=171941){//汉字范围 \u4e00-\u9fa5 (中文) result+="\\u" + Integer.toHexString(chr1); }else{ result+=str.charAt(i); } } return result; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/youxijishu/article/details/46958515