Exemplo n.º 1
0
  @EipkRequest("/s/article/man")
  public String articleMan(Model m) {

    String search_where = "";
    if (SysUtil.StrToInt(m.getPar("clear")) == 0) {
      search_where =
          m.getSessionPar("search_where") != null ? (String) m.getSessionPar("search_where") : "";
    } else {
      int search_section = SysUtil.StrToInt(m.getPar("search_section"));
      String search_article_title = m.getPar("search_article_title");
      if (search_section != 0) {
        search_where += " and sectionId=" + search_section;
      }
      if (!search_article_title.equals("")) {
        search_where += " and title like '%" + search_article_title + "%'";
      }
    }
    m.setSessionPar("search_where", search_where);

    String sql = "";

    DB db = new DB();

    sql = "select id,sectionName from t_section order by id";
    List<Map<String, Object>> section_list = db.query(sql);

    sql =
        "select count(*) as num from t_article a,t_section s where a.sectionId=s.id "
            + search_where;
    int total = SysUtil.StrToInt("" + db.querySingle(sql).get("num"));
    int pagth = SysUtil.StrToInt(m.getPar("pagth"));
    int start = SysUtil.getSplitPageStartId(total, SysUtil.PAGE_SIZE, pagth);
    sql =
        "select a.id,a.pubDate,s.sectionName,a.title,a.hits from t_article a,t_section s where a.sectionId=s.id "
            + search_where
            + " order by pubDate desc limit ?,?";
    List<Map<String, Object>> article_list = db.query(sql, start, SysUtil.PAGE_SIZE);

    db.close();

    m.setAttr(
        "split_page_str", SysUtil.getSplitPageStr(m.getRequest(), total, SysUtil.PAGE_SIZE, pagth));
    m.setAttr("section_list", section_list);
    m.setAttr("article_list", article_list);

    return "";
  }
Exemplo n.º 2
0
  @EipkRequest(value = "/s/article/upload", method = RequestMethod.POST, ajax = true)
  public AjaxData articleUploadFile(Model m) {
    Map<String, Object> result = new HashMap<String, Object>();

    String savePath = m.getRequest().getServletContext().getRealPath("/upload/");
    File saveDirFile = new File(savePath);
    if (!saveDirFile.exists()) {
      saveDirFile.mkdirs();
    }
    FileItemFactory fac = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(fac);
    upload.setHeaderEncoding("utf-8");

    List<?> fileList = null;
    try {
      fileList = upload.parseRequest(m.getRequest());
    } catch (FileUploadException e) {
      e.printStackTrace();
      return null;
    }
    Iterator<?> par_itr = fileList.iterator(), file_itr = fileList.iterator();
    while (par_itr.hasNext()) {
      FileItem item = (FileItem) par_itr.next();
      if (item.isFormField()) {
        try {
          String par_name = item.getFieldName();
          BufferedReader br;
          br = new BufferedReader(new InputStreamReader(item.getInputStream()));
          if (par_name != null) {
            result.put(par_name, br.readLine());
          }
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

    while (file_itr.hasNext()) {
      FileItem item = (FileItem) file_itr.next();
      String fileName = item.getName();
      if (!item.isFormField()) {
        try {
          int index = fileName.lastIndexOf("\\");
          if (index != -1) {
            fileName = fileName.substring(index + 1);
          }
          result.put("fileName", fileName);
          result.put("fileType", fileName.substring(fileName.lastIndexOf(".") + 1));
          String file_disk_name = java.util.UUID.randomUUID().toString();
          fileName = file_disk_name + fileName.substring(fileName.lastIndexOf("."));
          File uploadedFile = new File(savePath, fileName);
          item.write(uploadedFile);
          result.put("fileSize", SysUtil.FileSizeStr(item.getSize()));
          result.put("fileDiskName", file_disk_name);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    DB db = new DB();
    String sql = "insert into t_file (fileName,fileDiskName,fileSize,fileType) values (?,?,?,?)";
    int file_id =
        db.execute(
            sql,
            result.get("fileName"),
            result.get("fileDiskName"),
            result.get("fileSize"),
            result.get("fileType"));
    sql = "insert into t_appendix (articleId,fileId) values (?,?)";
    db.execute(sql, SysUtil.StrToInt("" + result.get("article_id")), file_id);
    db.close();

    return new AjaxData(result);
  }