Exemplo n.º 1
0
public class ExcelUtil {
  private static Logger logger = Log.getLogger(ExcelUtil.class);

  public static List<Map<String, String>> getXlsUserInfoData(String path) {
    try {
      File xlsFile = new File(path);
      Workbook book = Workbook.getWorkbook(xlsFile);
      Sheet[] sheet = book.getSheets();
      if (sheet.length > 0) {
        Sheet page = sheet[0];
        int columns = page.getColumns();
        System.out.println(columns);
        int rows = page.getRows();
        System.out.println(page.getName());
        if (rows < 2) return null;

        int columnsCount = 0;

        List<String> titleList = new ArrayList<String>();
        for (int i = 0; i < columns; i++) {
          // 列    行
          Cell cell = page.getCell(i, 0);
          String key = cell.getContents();
          if (key == null || key.equals("")) {
            continue;
          }
          columnsCount++;
          logger.debug(key);
          titleList.add(key);
        }
        columns = columnsCount;
        List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
        for (int i = 1; i < rows; i++) {
          logger.info("ii:" + i);
          Map<String, String> dataMap = new HashMap<String, String>();
          for (int j = 0; j < columns; j++) {
            String key = titleList.get(j);
            String value = page.getCell(j, i).getContents();
            dataMap.put(key, value);
            logger.debug(key + "  " + value);
          }
          dataList.add(dataMap);
        }
        logger.debug("xls length:" + dataList.size());
        return dataList;
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<Map<String, String>> list =
        getXlsUserInfoData(
            "/Users/yikuair/Documents/workspace/yikuairAPI/WebContent/static/excel/employee_list.xls");
    System.out.println(list.size());
  }
}
Exemplo n.º 2
0
public class Lists {
  private static Logger logger = Log.getLogger(Lists.class);

  public String getTasks(HttpParams params) {
    String user_id = params.getStr("id", "");

    String username = params.getStr("username", "");
    String password = params.getStr("password", "");

    if (username.equals("") || password.equals("")) {
      LogMsg err = new LogMsg();
      err.setCode(201);
      err.setMessage("用户名和密码不能为空");
      return JsonUtil.ObjectToJsonString(err);
    }

    if (user_id.equals("")) {
      LogMsg err = new LogMsg();
      err.setCode(201);
      err.setMessage("请传入当前id");
      return JsonUtil.ObjectToJsonString(err);
    }

    String sql =
        "select b.title as title,b.address as address, b.user_id as from_id,b.filepath as filepath,b.createTime as createTime,"
            + " b.btime as btime ,b.etime as etime "
            + "from t_taskcomfirm a left join t_tasklist b on a.task_id = b.id where a.user_id="
            + user_id
            + " and (b.btime >= '"
            + DateUtil.getTimeString("yyyy-MM-dd HH:mm:ss")
            + "') order by b.createTime";
    logger.debug("tasks:" + sql);
    BaseDao dao = BaseDao.getBaseDao();
    List<Map<String, String>> list = dao.getData(sql);
    LogMsg logMsg = new LogMsg();
    logMsg.setCode(200);
    logMsg.setMessage("success");
    logMsg.setData(list);
    return JsonUtil.ObjectToJsonString(logMsg);
  }
}
public class HttpParams {
  private static Logger logger = Log.getLogger(HttpParams.class);
  public static final String password = "******";
  public Map<String, String> avs = null;
  private HttpServletRequest request = null;
  Map<String, FileItem> fileitemMap = null;
  private Map<String, String> params = null;
  public boolean isMultipart = false, encoded = false;
  /**
   * Title: 带参数的够着函数
   *
   * <p>Description: 获取请求中的参数对象
   *
   * @param request
   * @throws UnsupportedEncodingException
   */
  public HttpParams() {}

  public HttpParams(HttpServletRequest request) throws UnsupportedEncodingException {
    // 检测是否是一个文件上传请求
    this.request = request;
    isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      upload.setHeaderEncoding("UTF-8");
      RequestContext requestContext = new ServletRequestContext(request);
      try {
        List<FileItem> items = upload.parseRequest(requestContext);
        Iterator<FileItem> itr = items.iterator();
        avs = new HashMap<String, String>();
        fileitemMap = new HashMap<String, FileItem>();
        while (itr.hasNext()) {
          FileItem item = (FileItem) itr.next();
          String fieldName = item.getFieldName();
          if (item.isFormField()) {
            avs.put(fieldName, item.getString("UTF-8"));
          } else {
            fileitemMap.put(fieldName, item);
          }
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    } else { // 非文件上传请求  (参数加密)
      request.setCharacterEncoding("utf-8");
      String s = request.getParameter("__");
      // s = URLDecoder.decode(s,"UTF-8");
      // logger.info("http reqeust params: "+s);
      if (s != null && !s.equals("")) {
        try {
          encoded = true;
          //					String str = DataUtil.decodeECBString(password, DataUtil.HexString2Bytes(s));
          String str = DataUtil.decodeECBString(password, DataUtil.decodeBase64(s));
          String[] pairs = str.split("&");
          params = new HashMap<String, String>();

          for (int i = 0; i < pairs.length; i++) {
            // String[] tokens = pairs[i].split("=");
            int index = pairs[i].indexOf("=");
            String key = pairs[i].substring(0, index);
            String value = pairs[i].substring(index + 1, pairs[i].length());
            params.put(key, URLDecoder.decode(value, "UTF-8"));
          }
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    }
  }

  /**
   * @Title: encryptParams @Description: TODO(这里用一句话描述这个方法的作用)
   *
   * @param @param params
   * @param @return
   * @param @throws Exception 设定文件
   * @return String 把url参数加密
   * @throws
   */
  public static String encryptParams(Map<String, String> params) throws Exception {
    Iterator<String> iter = params.keySet().iterator();
    StringBuffer sb = new StringBuffer();
    while (iter.hasNext()) {
      String k = iter.next();
      String v = params.get(k);
      if (sb.length() > 0) sb.append("&");
      sb.append(k);
      sb.append('=');
      sb.append(URLEncoder.encode(v, "UTF-8"));
    }
    String str = sb.toString();

    // return "__=" + DataUtil.encodeECBAsHexString(password, str);
    return "__=" + DataUtil.encodeECBAsBase64String(password, str);
  }

  public String getStr(String attr, String not_found_value) {
    String v =
        isMultipart
            ? avs.get(attr)
            : (!encoded) ? HttpUtil.getStr(this.request, attr, not_found_value) : params.get(attr);
    if (v == null) return not_found_value;
    return v;
  }

  /**
   * @Title: getFileItem @Description: TODO(获取文件蚊香)
   *
   * @param @param attr
   * @param @return
   * @param @throws Exception 设定文件
   * @return FileItem 返回类型
   * @throws
   */
  public FileItem getFileItem(String attr) throws Exception {
    return fileitemMap.get(attr);
  }

  /**
   * @Title: main @Description: TODO(这里用一句话描述这个方法的作用)
   *
   * @param @param args 设定文件
   * @return void 返回类型
   * @throws
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    String s = "ddd=wwasd=";
    int index = s.indexOf("=");
    String key = s.substring(0, index);
    String value = s.substring(index + 1, s.length());
    System.out.println(key + "------" + value);
  }
}