首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >国内技术强强联手之Nutz+KindEditor+LHGDialog+My97DatePicker

国内技术强强联手之Nutz+KindEditor+LHGDialog+My97DatePicker

作者头像
凡梦星尘
发布2024-11-20 17:19:31
发布2024-11-20 17:19:31
1310
举报
文章被收录于专栏:Hugo博客Hugo博客

有段时间没关注过国内IT技术发展情况了,前些天在学习国内的一个开源技术Nutz时想练个手,但一时又不知写些什么好,想了一会还是选择了自己的“老友”KindEditor。对它虽不敢说是透彻的了解(个人的JS水平有限,呵~),但至少也能很熟练的运用。官网很早便推出了大家期待已久的KE4,不过我一起都没有更新,正好这次拿它来历练下,嘻~。可是想到前面写的那些KE应用示例都是单调的,上次的那个还好有EasyUI做衬托,不过这个UI框架对于小小于的练手项目来说还是庞大了点。于是又便开始寻思着找别的UI看看,突然间起了以前用过的LHGDialog弹出窗口组件还蛮不错的,便去它官网逛了一圈。没有想到还真是让人喜出望外呀,LHG现也更新为4的版本了,那效果的炫丽真是让人颇然心动。下面就先来欣赏下花费2个多小时的劳动成果吧(现在是真相时间),呵呵……

nutz-ke-lgh-my97-1
nutz-ke-lgh-my97-1
nutz-ke-lgh-my97-2
nutz-ke-lgh-my97-2

在此不得不称赞下Nutz的高效简约之美,和以前的KE版本一样还是把上传部分的JSP页面翻译成后台JAVA代码,唯一不同的就是那些相同功能的实现代码精简了好多呀,官网示例中的两个JSP文件被有压缩成了一个只有不到400行的JAVA后台代码,源码如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

package org.nutz.kindeditor4.plugin; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.log.Log; import org.nutz.log.Logs; import org.nutz.mvc.annotation.AdaptBy; import org.nutz.mvc.annotation.At; import org.nutz.mvc.annotation.Param; import org.nutz.mvc.upload.UploadAdaptor; /** * KindEditor在线编辑器文件上传,管理模块 * @author Elkan(elkan1788@gmail.com) */ @At("/nutz/ke4plugin") @AdaptBy(type = UploadAdaptor.class) @IocBean public class KindEditor4Module { // 日志输出对象 private static Log log = Logs.get(); // 文件目录名称 private String fileDir; // 文件后缀名称 private String fileExt; // 当前站点上下文 private String pageCtx; // 站点真实路径 private String relPath; // 上传文件保存路径 private String savePath; // 允许上传文件后缀MAP数组 private static final HashMap<String, String> extMap = new HashMap<String, String>(); // 允许上传文件大小MAP数组 private static final HashMap<String,Long> sizeMap = new HashMap<String, Long>(); // 上传文件存放根目录 private String filePath = "/attached/"; static { // 初始后缀名称MAP数组 extMap.put("image", "gif,jpg,jpeg,png,bmp"); extMap.put("flash", "swf,flv"); extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"); extMap.put("file", "doc,docx,xls,xlsx,ppt,txt,zip,rar"); // 初始文件大小MAP数组 sizeMap.put("image", 100 * 1024l); sizeMap.put("flash", 10 * 1024 * 1024l); sizeMap.put("media", 10 * 1024 * 1024l); sizeMap.put("file", 10 * 1024 * 1024l); } @At public Map<String, Object> upload(@Param("imgFile") File tempFile, @Param("dir") String dir, ServletContext context) { // 初始相关变量 Map<String, Object> execute = new HashMap<String, Object>(); pageCtx = context.getContextPath().concat(filePath); relPath = context.getRealPath(filePath); fileDir = dir; if (null == dir || dir.isEmpty()) { fileDir = "file"; } // 检查是否有上传文件 if (null == tempFile) { execute.put("error", 1); execute.put("message", "请选择上传文件."); return execute; } // 检查上传文件保存目录是否存在或可写 if (!isExistOrRwFolder()) { execute.put("error", 1); execute.put("message", "上传文件保存目录不存在或\n是没有写入权限,请检查."); return execute; } // 检查目录名称是否正确 if (!extMap.containsKey(fileDir)) { execute.put("error", 1); execute.put("message", "目录名不正确,请检查."); return execute; } // 计算出文件后缀名 String tempFileName = tempFile.getName(); fileExt = tempFileName.substring(tempFileName.lastIndexOf(".") + 1); // 检查上传文件类型 if(!Arrays.<String>asList(extMap.get(fileDir).split(",")).contains(fileExt)){ execute.put("error", 1); execute.put("message", "上传文件的格式被拒绝,\n只允许" + extMap.get(fileDir) + "格式的文件."); return execute; } // 检查上传文件的大小 long maxSize = sizeMap.get(fileDir); if (tempFile.length() > maxSize) { execute.put("error", 1); String size = null; if(maxSize < 1024) { size = maxSize + "B"; } if(maxSize > 1024 && maxSize < 1024 * 1024){ size = maxSize/1024 + "KB"; } if(maxSize > 1024 * 1024){ size = maxSize/(1024 * 1024) + "MB"; } execute.put("message", "上传文件大小超过限制,只允\n许上传小于 " + size + " 的文件."); return execute; } // 生成新的文件名,并按日期分类 newSavePath(); // 拷贝上传文件至指定存放目录 copy(tempFile, savePath); // 计算出文件输出路径 int point = savePath.lastIndexOf("/") - 8; StringBuilder url = new StringBuilder(pageCtx); url.append(fileDir).append("/"); url.append(savePath.substring(point)); // 返回上传文件的输出路径至前端 execute.put("error", 0); execute.put("url", url.toString()); return execute; } @At public Map<String, Object> manager(@Param("dir") String dir, @Param("path") String path, @Param("order") String order, ServletContext context) { // 初始相关变量 Map<String, Object> execute = new HashMap<String, Object>(); pageCtx = context.getContextPath().concat(filePath); relPath = context.getRealPath(filePath); fileDir = dir; if (null == dir || dir.isEmpty()) { fileDir = "file"; } if (!extMap.containsKey(fileDir)) { execute.put("error", 1); execute.put("message", "目录名不正确,请检查."); return execute; } String tempPath = null == path ? fileDir.concat("/") : fileDir.concat("/"+path); String curPath = pageCtx.concat(tempPath); String curFileDir = relPath.concat("/"+tempPath); String curDir = path; String moveupDir = ""; // 检查当前目录是否为根目录 if (!"".equals(path)) { String str = curDir.substring(0, curDir.length() - 1); moveupDir = str.lastIndexOf("/") >= 0 ? str.substring(0, str.lastIndexOf("/") + 1) : ""; } // 检查..命令 if(path.indexOf("..") >= 0){ execute.put("error", 1); execute.put("message", "不允许使用..命令返回上一层."); return execute; } //最后一个字符不是/ if (!"".equals(path) && !path.endsWith("/")) { execute.put("error", 1); execute.put("message", "文件路径不合法."); return execute; } // 检查当前目录 File curPathFile = new File(curFileDir); if (!curPathFile.isDirectory()) { execute.put("error", 1); execute.put("message", "当前目录不存在."); return execute; } //遍历目录取的文件信息 List<HashMap> fileList = new ArrayList<HashMap>(); if (curPathFile.listFiles() != null) { for (File file : curPathFile.listFiles()) { HashMap<String, Object> hash = new HashMap<String, Object>(); String fileName = file.getName(); if (file.isDirectory()) { hash.put("is_dir", true); hash.put("has_file", (file.listFiles() != null)); hash.put("filesize", 0L); hash.put("is_photo", false); hash.put("filetype", ""); } else if (file.isFile()) { fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); hash.put("is_dir", false); hash.put("has_file", false); hash.put("filesize", file.length()); hash.put("is_photo", Arrays.<String>asList(extMap.get("image").split(",")).contains(fileExt)); hash.put("filetype", fileExt); } hash.put("filename", fileName); hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified())); fileList.add(hash); } } // 文件排序方式 String tempOrder = order != null ? order.toLowerCase() : "name"; if ("size".equals(tempOrder)) { Collections.sort(fileList, new SizeComparator()); } else if ("type".equals(tempOrder)) { Collections.sort(fileList, new TypeComparator()); } else { Collections.sort(fileList, new NameComparator()); } // 输出遍历后的文件信息数据 execute.put("moveup_dir_path", moveupDir); execute.put("current_dir_path", curDir); execute.put("current_url", curPath); execute.put("total_count", fileList.size()); execute.put("file_list", fileList); return execute; } /** * 判断文件上传保存的文件夹是否存在或可写 * @return 如果存在且可写返回"true",否则返回"false" */ public boolean isExistOrRwFolder(){ if(null == relPath || relPath.isEmpty()) { return false; } File folder = new File(relPath); if(!folder.exists()) return false; if(!folder.isDirectory()) return false; if(!folder.canWrite()) return false; return true; } /** * 生成新的文件名,且按日期分类管理 */ public void newSavePath() { StringBuilder tempPath = new StringBuilder(relPath); tempPath.append("/").append(fileDir).append("/"); SimpleDateFormat folderNameFormat = new SimpleDateFormat("yyyyMMdd"); tempPath.append(folderNameFormat.format(new Date())); File temp = new File(tempPath.toString()); if(!temp.exists()) temp.mkdirs(); SimpleDateFormat fileNameFormat = new SimpleDateFormat("yyyyMMddkkmmss_S"); tempPath.append("/").append(fileNameFormat.format(new Date())); tempPath.append(".").append(fileExt); savePath = tempPath.toString().replaceAll("\\\\", "/"); } /** * 拷贝文件 * @param src 源文件 * @param tar 目标路径 */ public void copy(File src, String tar) { // 判断源文件或目标路径是否为空 if (null == src || null == tar || tar.isEmpty()) { return; } InputStream srcIs = null; OutputStream tarOs = null; try { srcIs = new FileInputStream(src); File tarFile = new File(tar); tarOs = new FileOutputStream(tarFile); byte[] buffer = new byte[4096]; int n = 0; while (-1 != (n = srcIs.read(buffer))) { tarOs.write(buffer, 0, n); } } catch (IOException e) { log.error("Copy File is Fali, Because "+e); } finally { try { if (null != srcIs) { srcIs.close(); } if (null != tarOs) { tarOs.close(); } } catch (IOException e) { log.error("Close Stream is Fail, Because "+e); } } } /** * 根据文件名称排序 */ public class NameComparator implements Comparator { @Override public int compare(Object a, Object b) { HashMap hashA = (HashMap) a; HashMap hashB = (HashMap) b; if (((Boolean) hashA.get("is_dir")) && !((Boolean) hashB.get("is_dir"))) { return -1; } else if (!((Boolean) hashA.get("is_dir")) && ((Boolean) hashB.get("is_dir"))) { return 1; } else { return ((String) hashA.get("filename")).compareTo((String) hashB.get("filename")); } } } /** * 根据文件大小排序 */ public class SizeComparator implements Comparator { @Override public int compare(Object a, Object b) { HashMap hashA = (HashMap) a; HashMap hashB = (HashMap) b; if (((Boolean) hashA.get("is_dir")) && !((Boolean) hashB.get("is_dir"))) { return -1; } else if (!((Boolean) hashA.get("is_dir")) && ((Boolean) hashB.get("is_dir"))) { return 1; } else { if (((Long) hashA.get("filesize")) > ((Long) hashB.get("filesize"))) { return 1; } else if (((Long) hashA.get("filesize")) < ((Long) hashB.get("filesize"))) { return -1; } else { return 0; } } } } /** * 根据文件类型排序 */ public class TypeComparator implements Comparator { @Override public int compare(Object a, Object b) { HashMap hashA = (HashMap) a; HashMap hashB = (HashMap) b; if (((Boolean) hashA.get("is_dir")) && !((Boolean) hashB.get("is_dir"))) { return -1; } else if (!((Boolean) hashA.get("is_dir")) && ((Boolean) hashB.get("is_dir"))) { return 1; } else { return ((String) hashA.get("filetype")).compareTo((String) hashB.get("filetype")); } } } }

虽说代码精简了,但是功能却是一个没有含糊呀,接着欣赏效果图吧。

nutz-ke-lgh-my97-3
nutz-ke-lgh-my97-3
nutz-ke-lgh-my97-4
nutz-ke-lgh-my97-4
nutz-ke-lgh-my97-5
nutz-ke-lgh-my97-5
nutz-ke-lgh-my97-6
nutz-ke-lgh-my97-6

Nutz对于JAVA程序员来说,是除SSH外的另一个选择,一个美好的开始,如果你有使用相信你定会深学的爱上它,嘻~,别的就不多说了,下面直接把源码给奉上吧,因为时间紧迫(待会就要坐车回家了,嘎~,终于放假了)就只是随笔写了下,写的不好还望大家见谅。

PS: 示例源下载

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档