Kindeditor
程序员文章站
2022-04-13 13:12:40
...
Kindeditor上传图片和查询图片库
<html>
<head>
<script charset="utf-8" src="kindeditor-all-min.js"></script>
<script charset="utf-8" src="lang/zh-CN.js"></script>
<script>
KindEditor.ready(function(K) {
window.editor = K.create('#editor_id',{
uploadJson:"${pageContext.request.contextPath}/kindeditor/upload",
filePostName:"img",
fileManagerJson:"${pageContext.request.contextPath}/kindeditor/getAllImg",
allowFileManager:true
});
});
</script>
</head>
<body>
<textarea id="editor_id" name="content" style="width:700px;height:300px;">
<strong>HTML内容</strong>
</textarea>
</body>
</html>
后台代码
返回的数据格式有点复杂,需要自己封装
@RequestMapping("/upload")
public Map<String,Object> upload(HttpServletRequest request, MultipartFile img) throws IOException {
Map<String, Object> map = new HashMap<>();
String realPath = request.getSession().getServletContext().getRealPath("/upload/");
File file = new File(realPath);
if (!file.exists()){
file.mkdir();
}
String filename = img.getOriginalFilename();
String newFileName =new Date().getTime() + "-" + filename;
img.transferTo(new File(realPath,newFileName));
map.put("error",0);
//动态获取url
String scheme = request.getScheme();
InetAddress localHost = Inet4Address.getLocalHost();
String hostAddress = localHost.getHostAddress();
int port = request.getServerPort();
String contextPath = request.getContextPath();
String url = scheme + "://" + hostAddress + ":" + port + contextPath + "/upload/" + newFileName;
map.put("url",url);
return map;
}
@RequestMapping("getAllImg")
public Map<String,Object> getAllImg(HttpServletRequest request) throws UnknownHostException {
Map<String, Object> map = new HashMap<>();
List<Map<String ,Object>> list = new ArrayList<>();
String realPath = request.getSession().getServletContext().getRealPath("/upload/");
File file = new File(realPath);
String[] names = file.list();
for (String name : names) {
HashMap<String, Object> map1 = new HashMap<>();
map1.put("is_dir",false);
map1.put("has_file",false);
File file1 = new File(realPath, name);
long length = file1.length();
map1.put("filesize",length);
map1.put("is_photo",true);
String s = name.substring(name.lastIndexOf(".") + 1);
map1.put("filetype",s);
map1.put("filename",name);
boolean b = name.contains("-");
if (b){
String s1 = name.split("-")[0];
Long aLong = Long.valueOf(s1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s2 = format.format(aLong);
map1.put("datetime",s2);
}
if (!b){
map1.put("datetime",new Date());
}
map1.put("dir_path","");
list.add(map1);
}
String scheme = request.getScheme();
InetAddress localHost = Inet4Address.getLocalHost();
String address = localHost.getHostAddress();
int port = request.getServerPort();
String path = request.getContextPath();
String url=scheme+"://"+address+":"+port+path+"/upload/";
map.put("moveup_dir_path","");
map.put("current_dir_path","");
map.put("current_url",url);
map.put("total_count",names.length);
map.put("file_list",list);
return map;
}
推荐阅读
-
node.js中实现kindEditor图片上传功能的方法教程
-
(代码修正)OT后台KindEditor编辑器图片上传错误
-
ASP.NET配置KindEditor文本编辑器图文教程
-
ASP.NET配置KindEditor文本编辑器图文教程
-
KindEditor怎么使用?KindEditor编辑器使用图文教程
-
asp.net core集成kindeditor实现图片上传功能
-
解决OneThink中无法异步提交kindeditor文本框中修改后的内容方法
-
KindEditor怎么使用?KindEditor编辑器使用图文教程
-
JAVA开发微信公众平台使用KindEditor编辑图文消息。
-
Kindeditor(版本号4.0.5)编辑器添加上传flv视频功能