UEditor使用细节注意
来源:互联网 发布:减速机速比 算法 编辑:程序博客网 时间:2024/04/28 01:27
原理:先加载配置,后上传,最后回显。
UEditor给出了controller.jsp来进行加载配置,并上传,但是这些不是我们想要的。我们需要重写加载配置和上传回显操作。
serverUrl是我们需要修改的地方,它的作用是让我们加载配置。
这里我将serverUrl设置为:serverUrl: ROOT_PATH + "ueditor/upload"
upload方法(给出整段代码):
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.OutputStream;import java.util.Map;import java.util.UUID;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.io.IOUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.commons.CommonsMultipartFile;import com.google.common.collect.Maps;@Controller@RequestMapping("/ueditor")public class UeditorController extends BasicController{@RequestMappingpublic String index(){System.out.println(1);return "/ueditor/index";}@RequestMapping(value = "/upload", method = RequestMethod.GET) @ResponseBody public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException { String action = request.getParameter("action"); if ("config".equals(action)) { OutputStream os = response.getOutputStream(); IOUtils.copy(UeditorController.class.getClassLoader().getResourceAsStream("config.json"), os); } } @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public Map<String, String> upload(HttpServletRequest request,@RequestParam CommonsMultipartFile upfile) throws IOException { Map<String, String> result = Maps.newHashMap(); System.out.println(upfile.getFileItem().getFieldName()); String path = getFilePath(upfile); File file = new File(path); System.out.println(path); String state = "SUCCESS"; //返回类型 String rootPath = request.getContextPath(); result.put("url", rootPath + "/ueditor/show?filePath=" + path); result.put("size", String.valueOf(file.length())); result.put("type", file.getName().substring(file.getName().lastIndexOf("."))); result.put("state", state); return result; } @RequestMapping(value = "/show", method = RequestMethod.GET) public void show(String filePath, HttpServletResponse response) throws IOException { File file = getFile(filePath); response.setDateHeader("Expires", System.currentTimeMillis() + 1000 * 60 * 60 * 24); response.setHeader("Cache-Control", "max-age=60"); OutputStream os = response.getOutputStream(); FileInputStream is = null; try { is = new FileInputStream(file); IOUtils.copy(is, os); } catch (FileNotFoundException e) { response.setStatus(404); return; } finally { if (null != is) { is.close(); } if (null != os) { os.flush(); os.close(); } } } protected String getFilePath(CommonsMultipartFile uploadFile){ String absolutePath = "D:/upload"; File folder = new File(absolutePath); if (!folder.exists()) { folder.mkdirs(); } String rawName = uploadFile.getFileItem().getName(); String fileExt = rawName.substring(rawName.lastIndexOf(".")); String newName = System.currentTimeMillis() + UUID.randomUUID().toString() + fileExt; File saveFile = new File(absolutePath + File.separator + newName); try { uploadFile.getFileItem().write(saveFile); } catch (Exception e) { e.printStackTrace(); return ""; } return absolutePath + "/" + newName; } protected File getFile(String path){ File file = new File(path); return file; } }config.json,将这个配置放在resources里面。
需要注意的是,上传的那段代码,由于config.json中:
"imageFieldName": "upfile", /* 提交的图片表单名称 */
iageFiledName给的是upfile,所以我这里要写upfile,不然就会出错。
其实只要我们好好的理解ActionEnter具体是怎么实现的就行。
1 0
- UEditor使用细节注意
- ueditor使用注意点
- 百度编辑器-UEditor使用注意
- ActionBarSherlock 使用注意细节
- 使用stl 注意细节
- mybatis使用注意的细节
- mybatis使用注意的细节
- mybatis使用注意的细节
- mybatis使用注意的细节
- mybatis使用注意的细节
- 使用powerdesigner的一些注意细节
- 使用OpenCV需要注意的小细节
- svn使用注意的小细节
- 【PHP】substr的使用细节注意
- SolrCloud 译文以及一些使用注意细节
- STL使用中注意的细节
- jQuery $.data()方法使用注意细节
- JavaScript使用需要注意的细节
- 阿里巴巴总参谋长曾鸣: 战略是一门手艺淘宝阿里巴巴总参谋长曾鸣: 战略是一门手艺
- 马云的“余额宝”插手购房首付款 这是要“逼疯”银行的节奏么?淘宝马云的“余额宝”插手购房首付款 这是要“逼疯”银行的节奏么?
- MongoDb 抛出"Error retrieving nonce"异常
- springmvc 视频上传 ffmepg 转换为 flv格式 在转换flv时卡死,需关闭tomcat解决方案
- android logo 大小多少合适
- UEditor使用细节注意
- Linux LVM硬盘管理及LVM扩容 -【linux学习笔记】
- Struts学习(三)web.xml
- 数据库隔离级别
- 子线程真的无法更新UI吗?
- 【《软件设计模式与体系结构》学习笔记】软件设计模式概论
- 侦测程序句柄泄露的统计方法
- 关于listview的一些属性
- T-SQL:取最值的几种方式的简单比较.