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

📄 paramutil.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/ParamUtil.java,v 1.25.2.1 2006/07/10 12:55:30 phuongpdd Exp $
 * $Author: phuongpdd $
 * $Revision: 1.25.2.1 $
 * $Date: 2006/07/10 12:55:30 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2006 by MyVietnam.net
 *
 * All copyright notices regarding MyVietnam and MyVietnam CoreLib
 * MUST remain intact in the scripts and source code.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Correspondence and Marketing Questions can be sent to:
 * info at MyVietnam net
 *
 * @author: Minh Nguyen  
 * @author: Mai  Nguyen  
 */
package net.myvietnam.mvncore.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.myvietnam.mvncore.MVNCoreConfig;
import net.myvietnam.mvncore.MVNCoreResourceBundle;
import net.myvietnam.mvncore.exception.BadInputException;
import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;

public final class ParamUtil {

    private ParamUtil() { // prevent instantiation
    }

    //private static String contextPath = (new ParamOptions()).contextPath;
    //private static String serverPath = (new ParamOptions()).serverPath;//@todo combine 2 line to a static block

    private static DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");

    public static String getContextPath() {
        return MVNCoreConfig.getContextPath();
    }

    public static String getServerPath() {
        return MVNCoreConfig.getServerPath();
    }

    public static String getServer(HttpServletRequest request) {

        StringBuffer server = new StringBuffer(128);
        String scheme = request.getScheme();
        int port = request.getServerPort();
        if (port < 0) {
            port = 80; // Work around java.net.URL bug
        }
        server.append(scheme);
        server.append ("://");
        server.append(request.getServerName());
        if ( (scheme.equals("http") && (port != 80))
            || (scheme.equals("https") && (port != 443)) ) {
            server.append(':');
            server.append(port);
        }
        return server.toString();
    }

    public static String getServer2(HttpServletRequest request) {

        StringBuffer server = new StringBuffer(128);
        server.append(request.getScheme());
        server.append ("://");
        server.append(request.getHeader("host"));
        return server.toString();
    }

    public static String getParameter(HttpServletRequest request, String param) {

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

    public static String getParameterFilter(HttpServletRequest request, String param) {
        return DisableHtmlTagFilter.filter(getParameter(request, param));
    }

    public static String getParameter(HttpServletRequest request, String param, boolean checkEmpty)
        throws BadInputException {

        String ret = request.getParameter(param);
        if (ret == null) ret = "";
        ret = ret.trim();
        if ( checkEmpty && (ret.length() == 0) ) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_allow_to_be_empty", new Object[] {DisableHtmlTagFilter.filter(param)});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static String getParameterFilter(HttpServletRequest request, String param, boolean checkEmpty)
        throws BadInputException {
        return DisableHtmlTagFilter.filter(getParameter(request, param, checkEmpty)); 
    }

    /** @todo review this method */
    public static String getParameterSafe(HttpServletRequest request, String param, boolean checkEmpty)
        throws BadInputException {

        String ret = getParameter(request, param, checkEmpty);
        if ( (ret.indexOf('<') != -1) ||
             (ret.indexOf('>') != -1)) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.parameter_safe", new Object[] {DisableHtmlTagFilter.filter(param)});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

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

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

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

        int retValue = getParameterInt(request, param);
        if (retValue < 0) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object[] {DisableHtmlTagFilter.filter(param)});
            throw new BadInputException(localizedMessage);
        }
        return retValue;
    }

    public static int getParameterInt(HttpServletRequest request, String param, int defaultValue)
        throws BadInputException {

        String inputStr = getParameter(request, param, false);
        if (inputStr.length() == 0) {
            return defaultValue;
        }
        int ret;
        try {
            ret = Integer.parseInt(inputStr);
        } catch (NumberFormatException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object[] {DisableHtmlTagFilter.filter(param), "int"});
            throw new BadInputException(localizedMessage);
        }
        return ret;
    }

    public static int getParameterUnsignedInt(HttpServletRequest request, String param, int defaultValue)
        throws BadInputException {

        int retValue = getParameterInt(request, param, defaultValue);
        if (retValue < 0) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object[] {DisableHtmlTagFilter.filter(param)});
            throw new BadInputException(localizedMessage);
        }
        return retValue;
    }

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

        String inputStr = getParameter(request, param, true);
        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;
    }

    public static long getParameterLong(HttpServletRequest request, String param, long defaultValue)
        throws BadInputException {

        String inputStr = getParameter(request, param, false);

⌨️ 快捷键说明

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