java ueditor java_UEditor for Java 配置(SpringMVC)

ueditor version : 1.4.32

web项目名为home,在项目的根路径下创建文件夹ue,ue下面存放UEditor的文件。

在ue下面放置了自定义的用于测试的ueditor.html文件,改文件定义如下HTML>

ueditor demo

$(function(){

//实例化编辑器

var ue = UE.getEditor('container');

})

UEditor Demo

创建如下的一个Handler@RequestMapping("/ue/upload.action")

public void upload(HttpServletRequest request,HttpServletResponse response) throws IOException{

request.setCharacterEncoding( "utf-8" );

response.setHeader("Content-Type" , "text/html");

String rootPath = PathUtil.ROOT_PATH ;

log.debug("UEditor 上传,rootPath:{}",rootPath);

ActionEnter action = new ActionEnter(request, rootPath ) ;

String res = action.exec() ;

log.debug("UEditor 上传操作结果:{}",res);

response.getWriter().print(res);

}

在ueditor.config.js里面修改serverUrl配置,格式 serverUrl: URL + "upload.action"。这个路径是UE在上传操作时候的请求路径。

注:这里的URL变量UEditor会自动设置为UE编辑器所在页面的路径。由于这里UE编辑器所在页面为ue下的ueditor.html中,所以URL为...../ue

修改config.json里面的p_w_picpathUrlPrefix 和 p_w_picpathPathFormat 值。如下:

"p_w_picpathUrlPrefix": "/home",

"p_w_picpathPathFormat": "/ue/upload/p_w_picpath/{yyyy}{mm}{dd}/{time}{rand:6}",

要让UE编辑器能使用上传的功能,我们需要保证访问以下地址能成功的返回正确的Json数据

http://localhost:8080/home/ue/upload.action?action=config

其数据格式(部分内容):{"videoMaxSize":102400000,"videoActionName":"uploadvideo","fileActionName":"uploadfile","fileManagerListPath":"/home/ueditor/jsp/upload/file/","p_w_picpathCompressBorder":1600,"p_w_picpathManagerAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"p_w_picpathManagerListPath":"/home/ueditor/jsp/upload/p_w_picpath/","fileMaxSize":51200000,"fileManagerAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp",".flv",".swf",".mkv",".avi",".rm",".rmvb",".mpeg",".mpg",".ogg",".ogv",".mov",".wmv",".mp4",".webm",".mp3",".wav",".mid",".rar",".zip",".tar",".gz",".7z",".bz2",".cab",".iso",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".pdf",".txt",".md",".xml"],"fileManagerActionName":"listfile","snapscreenInsertAlign":"none","scrawlActionName":"uploadscrawl","videoFieldName":"upfile","p_w_picpathCompressEnable":true,"videoUrlPrefix":"","fileManagerUrlPrefix":"","catcherAllowFiles":[".png",".jpg",".jpeg",".gif",".bmp"],"p_w_picpathManagerActionName":"listp_w_picpath","snapscreenPathFormat":"/ueditor/jsp/upload/p_w_picpath/{yyyy}{mm}{dd}/{time}{rand:6}","scrawlPathFormat":"/home/ue/jsp/upload/p_w_picpath/{yyyy}{mm}{dd}/{time}

由于在UE提供的上传功能的Java代码中,在获取文件信息的时候首先是要获取config.json文件,并且根据处理逻辑会获取请求路径下的config.json文件,这里即为.../home/ue路径下面的config.json文件。但由于该文件实际是放在...home/ue/jsp路径下,所以我将config.json文件拷贝到了..../home/ue下。

至此,配置完成,顺利运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring中实现UEditor图片上传可以参考以下步骤: 1. 在前端Vue代码中配置UEditor富文本编辑器,并对上传图片做出相关设置。 2. 在Spring后端代码中编写图片上传的控制器,处理前端传递的图片文件信息。 3. 在Spring配置文件中配置文件上传的相关参数。 下面是具体的实现方法: 1. 前端代码: 在Vue组件中引入UEditor富文本编辑器,可以使用UEditor官网提供的Vue UEditor Wrapper组件。并在UEditor配置项中设置上传图片的相关参数,如下所示: ``` <template> <div> <vue-ueditor-wrap v-model="content" :config="ueditorConfig" :z-index="100" ></vue-ueditor-wrap> </div> </template> <script> import VueUeditorWrap from 'vue-ueditor-wrap'; export default { components: { VueUeditorWrap }, data () { return { content: '', ueditorConfig: { UEDITOR_HOME_URL: '/static/UEditor/', serverUrl: '/api/upload', maximumWords: 50000, initialFrameWidth: '100%', initialFrameHeight: 500, autoHeightEnabled: false, autoFloatEnabled: false, toolbars: [ ['source', 'bold', 'italic', 'underline', 'strikethrough', 'removeformat', 'formatmatch', 'forecolor', 'backcolor', 'fontfamily', 'fontsize', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', 'touppercase', 'tolowercase', 'link', 'unlink', 'insertimage', 'emotion', 'scrawl', 'music', 'insertvideo', 'attachment', 'map', 'gmap', 'insertcode', 'template', 'background', 'date', 'time', 'spechars', 'searchreplace', 'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts' ] ] }, }; }, }; </script> ``` 在上述代码中,通过`serverUrl`参数设置了上传图片的后端接口地址为`/api/upload`。 2. 后端控制器代码: 在Spring中,可以通过编写一个控制器方法来实现UEditor上传图片的功能。具体代码如下: ``` @RequestMapping(value = "/api/upload", method = RequestMethod.POST) @ResponseBody public String uploadImage(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); response.setHeader("Content-Type", "text/html"); String rootPath = request.getSession().getServletContext().getRealPath("/"); String contextPath = request.getContextPath(); String basePath = rootPath + File.separator + "upload" + File.separator; String savePath = contextPath + "/upload/"; String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"}; String upfile = "upfile"; JSONObject result = new JSONObject(); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Iterator<String> iterator = multipartRequest.getFileNames(); while (iterator.hasNext()) { MultipartFile file = multipartRequest.getFile(iterator.next()); if (file != null) { String fileName = file.getOriginalFilename(); String fileExt = fileName.substring(fileName.lastIndexOf(".")).toLowerCase(); boolean isAllow = false; for (String ext : fileType) { if (ext.equals(fileExt)) { isAllow = true; break; } } if (!isAllow) { result.put("state", "不支持的文件类型!"); return result.toJSONString(); } String newFileName = UUID.randomUUID().toString() + fileExt; File uploadedFile = new File(basePath, newFileName); if (!uploadedFile.getParentFile().exists()) { uploadedFile.getParentFile().mkdirs(); } file.transferTo(uploadedFile); result.put("state", "SUCCESS"); result.put("url", savePath + newFileName); result.put("title", newFileName); result.put("original", fileName); result.put("type", fileExt); result.put("size", file.getSize()); } } return result.toJSONString(); } ``` 3. Spring配置文件: 在Spring的配置文件中,需要配置文件上传的相关参数。具体代码如下: ``` <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> <property name="defaultEncoding" value="UTF-8"/> <property name="resolveLazily" value="true"/> </bean> ``` 其中,`maxUploadSize`参数设置了上传文件的最大大小,单位为字节。 至此,我们就完成了在Spring+Vue中实现UEditor图片上传的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值