⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paramutil.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (inputStr.length() == 0) {
            return defaultValue;
        }

        long ret;
        try {
            ret = Long.parseLong(inputStr);
        } catch (NumberFormatException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "long"});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    /**
     * @param  param is the name of variable
     * @return true if the value of param is not empty
     */
    public static boolean getParameterBoolean(HttpServletRequest request, String param) {

        String inputStr = getParameter(request, param);
        if (inputStr.length() == 0) return false;
        return true;
    }

    public static byte getParameterByte(HttpServletRequest request, String param)
        throws BadInputException {

        String inputStr = getParameter(request, param, true);
        byte ret;
        try {
            ret = Byte.parseByte(inputStr);
        } catch (NumberFormatException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "byte"});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static double getParameterDouble(HttpServletRequest request, String param)
        throws BadInputException {

        String inputStr = getParameter(request, param, true);
        double ret;
        try {
            ret = Double.parseDouble(inputStr);
        } catch (NumberFormatException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "double"});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static String getParameterUrl(HttpServletRequest request, String param)
        throws BadInputException {

        String ret = getParameter(request, param);
        if ( ret.length() > 0 ) {
            if ( !ret.startsWith("http://") &&
                 !ret.startsWith("https://") &&
                 !ret.startsWith("ftp://") ) {
                Locale locale = I18nUtil.getLocaleInRequest(request);
                String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_url", new Object[] {DisableHtmlTagFilter.filter(param)});
                throw new BadInputException(localizedMessage);
            }
        }
        return ret;
    }

    public static String getParameterPassword(HttpServletRequest request, String param, int minLength, int option)
        throws BadInputException {

        if (minLength < 1) minLength = 1;

        String ret = request.getParameter(param);
        if (ret == null) ret = "";
        ret = ret.trim();

        if ( ret.length() < minLength ) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.password_too_short", new Object[] {new Integer(minLength)});
            throw new BadInputException(localizedMessage);
        }

        /** @todo implement this feature */
        if (option == 1) {//char and number

        } else if (option == 2) {// lower char, upper char and number

        }
        return ret;
    }

    public static String getParameterEmail(HttpServletRequest request, String param)
        throws BadInputException {

        String email = getParameterSafe(request, param, true);
        MailUtil.checkGoodEmail(email);
        return email;
    }

    /**
     *
     */
    public static java.sql.Date getParameterDate(HttpServletRequest request, String param)
        throws BadInputException {

        String inputStr = getParameter(request, param, true);
        java.util.Date ret;
        try {
            ret = dateFormat.parse(inputStr);
        } catch (java.text.ParseException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[]{DisableHtmlTagFilter.filter(param), "Date"});
            throw new BadInputException(localizedMessage);
        }
        return new java.sql.Date(ret.getTime());
    }

    /**
     *
     */
    public static java.util.Date getParameterDateUtil(HttpServletRequest request, String param)
        throws BadInputException {

        String inputStr = getParameter(request, param, true);
        java.util.Date ret;
        try {
            ret = dateFormat.parse(inputStr);
        } catch (java.text.ParseException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[]{DisableHtmlTagFilter.filter(param), "Date"});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    /**
     *
     */
    public static java.sql.Date getParameterDate(HttpServletRequest request, String paramDay, String paramMonth, String paramYear)
        throws BadInputException {

        int day = getParameterInt(request, paramDay);
        int month = getParameterInt(request, paramMonth);
        int year = getParameterInt(request, paramYear);
        StringBuffer buffer = new StringBuffer();
        buffer.append(day).append("/").append(month).append("/").append(year);
        String inputStr = buffer.toString();

        java.util.Date ret;
        try {
            ret = dateFormat.parse(inputStr);
        } catch (java.text.ParseException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[]{DisableHtmlTagFilter.filter(inputStr), "Date"});
            throw new BadInputException(localizedMessage);
        }
        return new java.sql.Date(ret.getTime());
    }

    public static double getParameterTimeZone(HttpServletRequest request, String param)
        throws BadInputException {

        double timeZone = getParameterDouble(request, param);
        if (timeZone < -12 || timeZone > 13) {
            timeZone = 0;
        }
        return timeZone;
    }

    public static String getAttribute(HttpSession session, String name) {

        String ret = (String)session.getAttribute(name);
        if (ret == null) ret = "";
        return ret.trim();
    }

    /**
     * Note: sometime in the dispatched request, we dont receive HttpServletRequest
     * but receive ServletRequest, such as com.caucho.server.webapp.DispatchRequest
     *
     * @param request ServletRequest
     * @param name String
     * @return String
     */
    public static String getAttribute(ServletRequest request, String name) {

        String ret = (String)request.getAttribute(name);
        if (ret == null) ret = "";
        return ret.trim();
    }

    /**
     * Note: we have to use this method because (very strange) that the
     * HttpServletRequest object does not accept above method
     * getAttribute(ServletRequest request, String name)
     *
     * @param request HttpServletRequest
     * @param name String
     * @return String
     */
    public static String getAttribute(HttpServletRequest request, String name) {

        String ret = (String)request.getAttribute(name);
        if (ret == null) ret = "";
        return ret.trim();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -