Exemplo n.º 1
0
  /** 上传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!");
    }
  }
Exemplo n.º 2
0
 /** 获得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());
 }
Exemplo n.º 3
0
  /** 列出第p页的pattern */
  public void page() throws Exception {
    int ONE_PAGE_COUNT = ToolUtil.strToPositiveInt(request.getParameter("rp"), 25);
    ; // 一页pattern的个数
    int page = ToolUtil.strToPositiveInt(request.getParameter("page"), 1); // 页码数

    try {
      PatternDao dao = new PatternDaoImpl();
      List<Pattern> list = dao.list(page, ONE_PAGE_COUNT, "");
      long Count = dao.count("");
      JsonObjectNode j = new JsonObjectNode("");
      j.addChild(new JsonLeafNode("page", String.valueOf(page)));
      j.addChild(new JsonLeafNode("total", String.valueOf(Count)));
      JsonArrayNode rows = new JsonArrayNode("rows");
      Iterator<Pattern> it = list.iterator();
      while (it.hasNext()) {
        rows.addItem(it.next().getTBJsonNode());
      }

      j.addChild(rows);
      echo(j.toString());
    } catch (Exception e) {
      error("Data Access Fail:" + e.getMessage());
    }
  }