获取web访问路径的ip,项目的本地路路径,生成随机数

获取web访问路径的ip,项目的本地路路径,生成随机数


package com.evergreen.util;

import java.io.File;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;

public class PathCtrl {
    public PathCtrl() {
    }

//得到访问项目的本地路路径
    public String getSysPath(String strFolder) {
        String strCode = System.getProperty("user.dir");
        String strPath = strCode + File.separator + strFolder;
        File fl = new File(strPath);
        if (!fl.exists()) {
            fl.mkdir();
        }

        return strPath;
    }

//获取访问的IP地址
    public String getIPAddress(HttpServletRequest request) {
        String strReturn = request.getHeader("x-forwarded-for");
        if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
            strReturn = request.getHeader("Proxy-Client-IP");
        }

        if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
            strReturn = request.getHeader("WL-Proxy-Client-IP");
        }

        if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
            strReturn = request.getHeader("HTTP_Client_IP");
        }

        if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
            strReturn = request.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if (strReturn == null || strReturn.length() == 0 || "unknown".equalsIgnoreCase(strReturn)) {
            strReturn = request.getRemoteAddr();
        }

        if (strReturn != null && strReturn.length() > 15 && strReturn.indexOf(",") > 0) {
            strReturn = strReturn.substring(0, strReturn.indexOf(","));
        }

        return strReturn;
    }

//生成随机树
    public String getRandomString(int iLength) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < iLength; ++i) {
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }

        return sb.toString();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值