摘要:目的本文旨在整合百度前端富文本与,使用作为的后端,提供上传图片等后台相关的功能,即使用替换官方提供的后台方式。
目的
本文旨在整合百度前端富文本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
摘要:目的本文旨在整合百度前端富文本与,使用作为的后端,提供上传图片等后台相关的功能,即使用替换官方提供的后台方式。 目的 本文旨在整合百度前端富文本Ueditor与SpringMVC,使用Spring Controller作为Ueditor的后端,提供上传图片等后台相关的功能,即使用SpringMVC替换官方提供的JSP后台方式。 步骤 创建web工程,本文以maven进行创建和管理,最...
摘要:前言由于写的文章已经是有点多了,为了自己和大家的检索方便,于是我就做了这么一个博客导航。 前言 由于写的文章已经是有点多了,为了自己和大家的检索方便,于是我就做了这么一个博客导航。 由于更新比较频繁,因此隔一段时间才会更新目录导航哦~想要获取最新原创的技术文章欢迎关注我的公众号:Java3y Java3y文章目录导航 Java基础 泛型就这么简单 注解就这么简单 Druid数据库连接池...
摘要:的整合大致结构中放置的配置文件,由于这个例子很简单,所以配置得比较简单。在与的整合中,在这里不用配置,因为在整合包中有的扫描类。中配置的是和整合的配置。其中包括数据源数据池的配置的配置扫描器的配置还有事务的配置。所以将改了就解决问题了 1. springMVC+spring+mybatis的整合大致结构: showImg(https://segmentfault.com/img/bVb...
阅读 3854·2021-11-12 10:36
阅读 3917·2021-09-22 15:48
阅读 3618·2019-08-30 15:54
阅读 2702·2019-08-29 16:44
阅读 2437·2019-08-29 16:08
阅读 2518·2019-08-29 16:06
阅读 1386·2019-08-29 15:21
阅读 3353·2019-08-29 12:39
极致性价比!云服务器续费无忧!
Tesla A100/A800、Tesla V100S等多种GPU云主机特惠2折起,不限台数,续费同价。
NVIDIA RTX 40系,高性价比推理显卡,满足AI应用场景需要。
乌兰察布+上海青浦,满足东推西训AI场景需要