资讯专栏INFORMATION COLUMN

Ueditor与SpringMVC整合

lbool / 1047人阅读

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

目的

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

步骤

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

创建Ueditor统一后台Controller服务

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/ued")
public class UeditorController{
    
    @RequestMapping("/serverUrl")
    @ResponseBody
    public Object test(HttpServletRequest request,
                       @RequestParam(value = "action") String action, 
                       @RequestParam(value = "upfile", required = false) MultipartFile file) throws Exception {
       switch (action) {
           case "config": // 加载返回ueditor配置文件conf/config.json
               return getConfig();
           case "uploadimage": // 上传图片
               return uploadImage(request, file);
           case "uploadvideo": // 上传视频
               return "视频处理方法";
           case "uploadfile": // 上传文件
               return "文件处理方法";
           default:
               return "无效action";
        }
    }
   
    private String getConfig() throws Exception {
        File file = ResourceUtils.getFile("classpath:conf/config.json");
        String json = FileUtils.readFileToString(file, "utf-8");
        return json;
    }
    
    private Map uploadImage(HttpServletRequest request, MultipartFile file) {
        
        String state = "SUCCESS";
        String savedDir = request.getSession().getServletContext().getRealPath("upload");
        String filename = file.getOriginalFilename();
        File filepath = new File(savedDir,filename);
        if (!filepath.getParentFile().exists()) {
            filepath.getParentFile().mkdirs();
        }
        // 写到服务器路径下,可扩展,比如上传到云端或文件服务器
        file.transferTo(new File(savedDir + File.separator + filename));
        String uploadHttpUrl = "http://localhost:8083/upload"+ File.separator + filename;
        return resultMap(file, state, uploadHttpUrl);
    }
    
    private Map resultMap(MultipartFile file, String state, String uploadHttpUrl) {
        Map resMap = new HashMap();
        resMap.put("state", state);  //"SUCCESS" 表示成功
        resMap.put("title", file.getOriginalFilename());
        resMap.put("original", file.getOriginalFilename());
        resMap.put("type", file.getContentType());
        resMap.put("size", file.getSize());
        resMap.put("url", uploadHttpUrl);
        return resMap;
    }
   
}

资源加载帮助类

 import org.springframework.util.Assert;
 import org.springframework.util.ClassUtils;
 import org.springframework.util.StringUtils;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 public class ResourceUtils{
     public static File getFile(String resourceLocation) throws FileNotFoundException {
         Assert.notNull(resourceLocation, "Resource location must not be null");
         if (resourceLocation.startsWith("classpath:")) {
             String path = resourceLocation.substring("classpath:".length());
             String description = "class path resource [" + path + "]";
             ClassLoader cl = ClassUtils.getDefaultClassLoader();
             URL url = cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path);
             if (url == null) {
                 throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not exist");
                 
             }else{
                 return getFile(url, description);
             }
         }else{
             try {
                 return getFile(new URL(resourceLocation));
             }catch (MalformedURLException var5) {
                 return new File(resourceLocation);
             }
         }
     }
     public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
         Assert.notNull(resourceUrl, "Resource URL must not be null");
         if (!"file".equals(resourceUrl.getProtocol())) {
             throw new FileNotFoundException(description + " cannot be resolved to absolute file path because it does not reside in the file system: " + resourceUrl);
         }else{
             try {
                 return new File(toURI(resourceUrl).getSchemeSpecificPart());
             } catch (URISyntaxException var3) {
                 return new File(resourceUrl.getFile());
             }
         }
     }
     public static URI toURI(URL url) throws URISyntaxException {
         return toURI(url.toString());
     }
     public static URI toURI(String location) throws URISyntaxException {
         return new URI(StringUtils.replace(location, " ", "%20"));
     }
     public static File getFile(URL resourceUrl) throws FileNotFoundException {
         return getFile(resourceUrl, "URL");
     }
 }

配置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元查看
<