资讯专栏INFORMATION COLUMN

Ueditor与SpringMVC整合

lbool / 1325人阅读

摘要:目的本文旨在整合百度前端富文本与,使用作为的后端,提供上传图片等后台相关的功能,即使用替换官方提供的后台方式。

目的

本文旨在整合百度前端富文本Ueditor与SpringMVC,使用Spring Controller作为Ueditor的后端,提供上传图片等后台相关的功能,即使用SpringMVC替换官方提供的JSP后台方式。

步骤

创建web工程,本文以maven进行创建和管理,最终目录结构如下:

创建Ueditor统一后台Controller服务

</>复制代码

  1. import org.apache.commons.io.FileUtils;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.util.ResourceUtils;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import javax.servlet.http.HttpServletRequest;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. @Controller
  14. @RequestMapping("/ued")
  15. public class UeditorController{
  16. @RequestMapping("/serverUrl")
  17. @ResponseBody
  18. public Object test(HttpServletRequest request,
  19. @RequestParam(value = "action") String action,
  20. @RequestParam(value = "upfile", required = false) MultipartFile file) throws Exception {
  21. switch (action) {
  22. case "config": // 加载返回ueditor配置文件conf/config.json
  23. return getConfig();
  24. case "uploadimage": // 上传图片
  25. return uploadImage(request, file);
  26. case "uploadvideo": // 上传视频
  27. return "视频处理方法";
  28. case "uploadfile": // 上传文件
  29. return "文件处理方法";
  30. default:
  31. return "无效action";
  32. }
  33. }
  34. private String getConfig() throws Exception {
  35. File file = ResourceUtils.getFile("classpath:conf/config.json");
  36. String json = FileUtils.readFileToString(file, "utf-8");
  37. return json;
  38. }
  39. private Map uploadImage(HttpServletRequest request, MultipartFile file) {
  40. String state = "SUCCESS";
  41. String savedDir = request.getSession().getServletContext().getRealPath("upload");
  42. String filename = file.getOriginalFilename();
  43. File filepath = new File(savedDir,filename);
  44. if (!filepath.getParentFile().exists()) {
  45. filepath.getParentFile().mkdirs();
  46. }
  47. // 写到服务器路径下,可扩展,比如上传到云端或文件服务器
  48. file.transferTo(new File(savedDir + File.separator + filename));
  49. String uploadHttpUrl = "http://localhost:8083/upload"+ File.separator + filename;
  50. return resultMap(file, state, uploadHttpUrl);
  51. }
  52. private Map resultMap(MultipartFile file, String state, String uploadHttpUrl) {
  53. Map resMap = new HashMap();
  54. resMap.put("state", state); //"SUCCESS" 表示成功
  55. resMap.put("title", file.getOriginalFilename());
  56. resMap.put("original", file.getOriginalFilename());
  57. resMap.put("type", file.getContentType());
  58. resMap.put("size", file.getSize());
  59. resMap.put("url", uploadHttpUrl);
  60. return resMap;
  61. }
  62. }

资源加载帮助类

</>复制代码

  1. import org.springframework.util.Assert;
  2. import org.springframework.util.ClassUtils;
  3. import org.springframework.util.StringUtils;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.net.MalformedURLException;
  7. import java.net.URI;
  8. import java.net.URISyntaxException;
  9. import java.net.URL;
  10. public class ResourceUtils{
  11. public static File getFile(String resourceLocation) throws FileNotFoundException {
  12. Assert.notNull(resourceLocation, "Resource location must not be null");
  13. if (resourceLocation.startsWith("classpath:")) {
  14. String path = resourceLocation.substring("classpath:".length());
  15. String description = "class path resource [" + path + "]";
  16. ClassLoader cl = ClassUtils.getDefaultClassLoader();
  17. URL url = cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path);
  18. if (url == null) {
  19. throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not exist");
  20. }else{
  21. return getFile(url, description);
  22. }
  23. }else{
  24. try {
  25. return getFile(new URL(resourceLocation));
  26. }catch (MalformedURLException var5) {
  27. return new File(resourceLocation);
  28. }
  29. }
  30. }
  31. public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
  32. Assert.notNull(resourceUrl, "Resource URL must not be null");
  33. if (!"file".equals(resourceUrl.getProtocol())) {
  34. throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUrl);
  35. }else{
  36. try {
  37. return new File(toURI(resourceUrl).getSchemeSpecificPart());
  38. } catch (URISyntaxException var3) {
  39. return new File(resourceUrl.getFile());
  40. }
  41. }
  42. }
  43. public static URI toURI(URL url) throws URISyntaxException {
  44. return toURI(url.toString());
  45. }
  46. public static URI toURI(String location) throws URISyntaxException {
  47. return new URI(StringUtils.replace(location, " ", "%20"));
  48. }
  49. public static File getFile(URL resourceUrl) throws FileNotFoundException {
  50. return getFile(resourceUrl, "URL");
  51. }
  52. }

配置ueditor.config.js

把文件中的serverUrl: URL + "jsp/controller.jsp",修改为serverUrl: "/ued/serverUrl" 即可。

效果

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69047.html

相关文章

  • UeditorSpringMVC整合

    摘要:目的本文旨在整合百度前端富文本与,使用作为的后端,提供上传图片等后台相关的功能,即使用替换官方提供的后台方式。 目的 本文旨在整合百度前端富文本Ueditor与SpringMVC,使用Spring Controller作为Ueditor的后端,提供上传图片等后台相关的功能,即使用SpringMVC替换官方提供的JSP后台方式。 步骤 创建web工程,本文以maven进行创建和管理,最...

    linkin 评论0 收藏0
  • ueditor笔记

    摘要:下载完后的包三将集成到项目第一步新建一个项目,并把引入到项目中。项目中配置其它上传其它上传如视频上传等等参考图片上传,修改即可。一、ueditor是什么 UEditor 是由百度「FEX前端研发团队」开发的所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码。 二、ueditor的下载 下载网址:https://ueditor.ba...

    番茄西红柿 评论0 收藏0
  • Java3y文章目录导航

    摘要:前言由于写的文章已经是有点多了,为了自己和大家的检索方便,于是我就做了这么一个博客导航。 前言 由于写的文章已经是有点多了,为了自己和大家的检索方便,于是我就做了这么一个博客导航。 由于更新比较频繁,因此隔一段时间才会更新目录导航哦~想要获取最新原创的技术文章欢迎关注我的公众号:Java3y Java3y文章目录导航 Java基础 泛型就这么简单 注解就这么简单 Druid数据库连接池...

    KevinYan 评论0 收藏0
  • mybatis和springMVC整合及其中的问题

    摘要:的整合大致结构中放置的配置文件,由于这个例子很简单,所以配置得比较简单。在与的整合中,在这里不用配置,因为在整合包中有的扫描类。中配置的是和整合的配置。其中包括数据源数据池的配置的配置扫描器的配置还有事务的配置。所以将改了就解决问题了 1. springMVC+spring+mybatis的整合大致结构: showImg(https://segmentfault.com/img/bVb...

    EscapedDog 评论0 收藏0
  • Java后端

    摘要:,面向切面编程,中最主要的是用于事务方面的使用。目标达成后还会有去构建微服务,希望大家多多支持。原文地址手把手教程优雅的应用四手把手实现后端搭建第四期 SpringMVC 干货系列:从零搭建 SpringMVC+mybatis(四):Spring 两大核心之 AOP 学习 | 掘金技术征文 原本地址:SpringMVC 干货系列:从零搭建 SpringMVC+mybatis(四):Sp...

    joyvw 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<