最近需求要引入类似微信公众号文章后台可编辑功能,所以考虑springboot(sb)项目引入ueditor富文本框。
首先就是要去官网下载ueditor需要的一些js脚本和插件
UEditor官网下载 我们选择最新的[1.4.3.3 Jsp 版本] 版本
然后把ueditor全家桶放在项目的静态文件目录下
以及一些封装的java方法 和demo
发现config.json要放在根目录下,这样才能运行不报错,
然后运行项目 访问index.html页面发现没有问题
这样我们基本就完成了,
最后修改config.json文件, 里面有一些图片路径前缀要修改下
可以修改为静态服务器路径也可以修改成云存储路径,就看你存储的时候怎么实现了。
下面是实现存储图片改到七牛云的方法,在原有基础上稍微修改了一下
public class BinaryUploader {
public static final State save(HttpServletRequest request,
Map conf) {
// FileItemStream fileStream = null;
// boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;
if (!ServletFileUpload.isMultipartContent(request)) {
return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
}
// ServletFileUpload upload = new ServletFileUpload(
// new DiskFileItemFactory());
//
// if ( isAjaxUpload ) {
// upload.setHeaderEncoding( "UTF-8" );
// }
try {
// FileItemIterator iterator = upload.getItemIterator(request);
//
// while (iterator.hasNext()) {
// fileStream = iterator.next();
//
// if (!fileStream.isFormField())
// break;
// fileStream = null;
// }
//
// if (fileStream == null) {
// return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
// }
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile(conf.get("fieldName").toString());
if(multipartFile==null){
return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
}
if (true) {
String originFileName = multipartFile.getOriginalFilename();
String suffix = FileType.getSuffixByFilename(originFileName);
originFileName = originFileName.substring(0,
originFileName.length() - suffix.length());
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
String imgurl = null;
try {
imgurl = QiniuUploadUtil.updateImg(multipartFile, uuid + suffix);
} catch (Exception e) {
e.printStackTrace();
}
State storageState = new BaseState(true);
storageState.putInfo("url", imgurl);
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
return storageState;
} else {
String savePath = (String) conf.get("savePath");
//String originFileName = fileStream.getName();
String originFileName = multipartFile.getOriginalFilename();
String suffix = FileType.getSuffixByFilename(originFileName);
originFileName = originFileName.substring(0,
originFileName.length() - suffix.length());
savePath = savePath + suffix;
long maxSize = ((Long) conf.get("maxSize")).longValue();
if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
}
savePath = PathFormat.parse(savePath, originFileName);
//String physicalPath = (String) conf.get("rootPath") + savePath;
String basePath=(String) conf.get("basePath");
String physicalPath = basePath + savePath;
//InputStream is = fileStream.openStream();
InputStream is = multipartFile.getInputStream();
State storageState = StorageManager.saveFileByInputStream(is,
physicalPath, maxSize);
is.close();
if (storageState.isSuccess()) {
storageState.putInfo("url", PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
}
return storageState;
}
// } catch (FileUploadException e) {
// return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
} catch (IOException e) {
}
return new BaseState(false, AppInfo.IO_ERROR);
}
private static boolean validType(String type, String[] allowTypes) {
List list = Arrays.asList(allowTypes);
return list.contains(type);
}
}
至此,我们功能需求已实现,可以自己实现一下