/** 上传ruleset */ public void upRuleset() throws Exception { /** * form中的enctype必须是multipart/... 组件提供方法检测form表单的enctype属性 在isMultipartContent方法中同时检测了是否是post提交 * 如果不是post提交则返回false */ if (ServletFileUpload.isMultipartContent(request)) { String RulePath = (String) application.getAttribute("Ruleset"); DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(new File(RulePath + "tmp")); // 临时文件目录 // 内存最大占用 factory.setSizeThreshold(1024000); ServletFileUpload sfu = new ServletFileUpload(factory); // 单个文件最大值byte sfu.setFileSizeMax(102400000); // 所有上传文件的总和最大值byte sfu.setSizeMax(204800000); List<FileItem> items = null; try { items = sfu.parseRequest(request); } catch (SizeLimitExceededException e) { error("size limit exception!"); return; } catch (Exception e) { error("Exception:" + e.getMessage()); return; } Json j = new Json(1, "ok"); JsonObjectNode data = j.createData(); JsonArrayNode files = new JsonArrayNode("filename"); data.addChild(files); Iterator<FileItem> iter = (items == null) ? null : items.iterator(); while (iter != null && iter.hasNext()) { FileItem item = (FileItem) iter.next(); // 文件域 if (!item.isFormField()) { String fileName = item.getName(); int index = fileName.lastIndexOf("\\"); if (index < 0) index = 0; fileName = fileName.substring(index); if (!fileName.endsWith(".xml")) fileName += ".xml"; BufferedInputStream in = new BufferedInputStream(item.getInputStream()); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(RulePath + fileName))); Streams.copy(in, out, true); files.addItem(new JsonLeafNode("", fileName)); } } echo(j.toString()); } else { error("enctype error!"); } }
/** 获得rulesets */ public void rulesets() throws Exception { Json j = new Json(1); JsonObjectNode data = j.createData(); JsonArrayNode rules = new JsonArrayNode("rules"); data.addChild(rules); List<String> files = ToolUtil.getFileFromPath((String) application.getAttribute("Ruleset"), "xml"); Iterator<String> it = files.iterator(); while (it.hasNext()) { rules.addItem(new JsonLeafNode("", it.next())); } echo(j.toString()); }