常用工具类(三):获取接口参数与统一返回结果

public class ParamUtils {
    private static Logger logger = LoggerFactory.getLogger(ParamUtils.class);
    private static Gson gson = new Gson();
    private static View viewer = new View();

    public static String getParam(HttpServletRequest request) {
        String param = null;
        if ("GET".equals(request.getMethod())) {
            param = request.getParameter("param");
        } else {
            param = getPostParm(request);
        }
        return cleanXSS(param);
    }

    private static String getPostParm(HttpServletRequest request) {
        StringBuffer jb = new StringBuffer();
        String line = null;
        BufferedReader reader = null;
        try {
            reader = request.getReader();
            if (!reader.ready()) return jb.toString();
            while ((line = reader.readLine()) != null)
                jb.append(line);
        } catch (IllegalStateException e){
            logger.error("getReader 被调用了两次的原因么" );
        } catch (Exception e) {
            logger.error("读取请求参数出错", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    logger.error("BufferedReader 关闭有误", e);
                }
            }
        }
        return jb.toString();
    }

    public static String encodeURI(String uri) {
        try {
            return URLEncoder.encode(uri, "UTF-8");
        } catch (Exception e) {
            return uri;
        }
    }

    //切割一个范围  "limit":"1,10"
    public static int[] parseLimit(String limit) {
        String[] strings = limit.split(",");
        int[] result = new int[2];
        result[0] = Integer.parseInt(strings[0]);
        result[1] = Integer.parseInt(strings[1]);
        return result;
    }

    private static String cleanXSS(String value) {
        if (value == null) {
            return null;
        }
        value = value.replaceAll("eval", "");
        value = value.replaceAll("
                    
                    

你可能感兴趣的:(java)