一:引入css以及js

WangEditor5 官网 https://www.wangeditor.com/

推荐下载到本地引入,有时候发现CDN 访问不成功。

二:H5前端整合编辑器

这里遇到的坑:原生的图片和视频上传,不懂为什么配置多个上传参数无效(即maxNumberOfFiles参数),配置成多个后台也只能接收到一个,返回参数即使按照官网所需,上传第二个的时候js就报错了。所以这里直接用自定义方法上传了,后台只需返回图片或视频URL即可。 有发现并解决这个问题的伙伴麻烦解答一下~

三:后台接收并保存文件至本地以及FTP

我这里是因为工作的需求文件才放到FTP,没必要的直接存本地即可。

package com.txy.modules.business.Wangeditor;

import com.txy.modules.common.dto.output.ApiResult;

import com.txy.modules.common.entity.Attachment;

import com.txy.utils.ftp.FtpUtil;

import com.txy.utils.spring.SpringUtil;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;

import org.apache.logging.log4j.util.PropertiesUtil;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.configurationprocessor.json.JSONArray;

import org.springframework.boot.configurationprocessor.json.JSONException;

import org.springframework.boot.configurationprocessor.json.JSONObject;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.time.LocalDateTime;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.UUID;

@RestController

@RequestMapping("api/business/wangeditor")

@Api(value = "wangeditor后台配置")

public class WangEditorController {

private String basicLocation = "D:\\images\\";

@Autowired

private FtpUtil ftpUtil;

@ResponseBody

@PostMapping("/upload")

@ApiOperation("上传图片")

public ApiResult save_main(HttpServletRequest request, HttpServletResponse response,

@RequestPart("file") MultipartFile file, @ApiParam("com.zhoukai.modules.common.dto.input.validate.CommonParamsInput")String params) throws IOException, JSONException {

String suffix = "";

String originalFilename = file.getOriginalFilename();

String uuid = UUID.randomUUID().toString();

if (originalFilename.contains(".")) {

suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

}

String fileName=uuid+suffix;

File fileSave = new File(basicLocation ,fileName);

if (!fileSave.getParentFile().exists()) {

fileSave.getParentFile().mkdirs();

}

FileOutputStream fos = null;

try {

fos = new FileOutputStream(fileSave);

fos.write(file.getBytes());

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

//接着上传到FTP文件服务器上面 这里用了Hutool工具库 实现方法就不粘贴出来了

ftpUtil.upload("wangEditor", fileName, fileSave);

Map map = new HashMap();

map.put("status",0);

map.put("url","http://192.168.0.219:2233/wangEditor/"+fileName);

return ApiResult.success(map);

}

}

四:配置文件,通过url访问本地的图片

package com.zhoukai.modules.business.editor;

import org.springframework.stereotype.Component;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component

public class PhotoUtils implements WebMvcConfigurer {

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

/**

* 访问路径:http://localhost:2233/images/test.jpg

* "/image/**" 为前端URL访问路径

* "file:" + bmpPath 是本地磁盘映射

*/

String bmpPath = "D:\\images\\";

registry.addResourceHandler("/images/**").addResourceLocations("file:"+bmpPath );

}

}

五:实现截图

六:总结

WangEditor 自带的文件上传(官网支持多个上传)功能未实现~~,报错在于index.js中,没深究其原因,希望实现的大佬刷到此文章踢我一脚

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。

发表评论

返回顶部暗黑模式