zoukankan      html  css  js  c++  java
  • JAVA工具类

    SpringUtils

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.stereotype.Component;
    
    /**
     * spring工具类 方便在非spring管理环境中获取bean
     */
    @Component
    public class SpringUtils implements BeanFactoryPostProcessor {
    
        private static ConfigurableListableBeanFactory beanFactory;
    
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
            SpringUtils.beanFactory = configurableListableBeanFactory;
        }
    
    
        /**
         * 获取对象
         *
         * @param name
         * @return Object 一个以所给名字注册的bean的实例
         * @throws org.springframework.beans.BeansException
         */
        @SuppressWarnings("unchecked")
        public static <T> T getBean(String name) throws BeansException {
            return (T) beanFactory.getBean(name);
        }
    
        /**
         * 获取类型为requiredType的对象
         *
         * @param clz
         * @return
         * @throws org.springframework.beans.BeansException
         */
        public static <T> T getBean(Class<T> clz) throws BeansException {
            return (T) beanFactory.getBean(clz);
        }
    }
    

    MessageUtils

    import com.hemou.ruoyi.common.utils.spring.SpringUtils;
    import org.springframework.context.MessageSource;
    import org.springframework.context.i18n.LocaleContextHolder;
    
    /**
     * @Author HeMOu
     */
    public class MessageUtils {
    
        /**
         * 根据消息键和参数 获取消息 委托给spring messageSource
         *
         * @param key 消息键
         * @param args 参数
         * @return 获取国际化翻译值
         */
        public static String message(String key, Object... args) {
            MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
            return messageSource.getMessage(key, args, LocaleContextHolder.getLocale());
        }
    }
    

    AddressUtils

    获取Ip的真实地址

    import cn.hutool.http.HttpUtil;
    import com.alibaba.fastjson.JSONObject;
    import org.apache.commons.lang3.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.HashMap;
    
    /**
     * 获取地址工具类
     *
     * @Author HeMOu
     */
    public class AddressUtils {
    
        private static Logger log = LoggerFactory.getLogger(AddressUtils.class);
    
        public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
    
        public static String getRealAddressByIP(String ip) {
            String address = "XX XX";
            // 内网不查询
            if (IpUtils.internalIp(ip)) {
                return "内网IP";
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("ip", ip);
            String rspStr = HttpUtil.post(IP_URL, map);
            if (StringUtils.isEmpty(rspStr)) {
                log.error("获取地理位置异常 {}", ip);
                return address;
            }
            try{
                JSONObject obj = JSONObject.parseObject(rspStr);
                JSONObject data = obj.getObject("data", JSONObject.class);
                String region = data.getString("region");
                String city = data.getString("city");
                address = region + " " + city;
                return address;
            }catch (Exception e){
                log.error("获取地理位置异常 {}", ip);
                return address;
            }
        }
    }
    

    ServletUtils

    获取request和session对象

    import cn.hutool.core.convert.Convert;
    import org.springframework.web.context.request.RequestAttributes;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * 客户端工具类
     */
    public class ServletUtils {
    
        /**
         * 获取请求属性
         *
         * @return 请求属性
         */
        public static ServletRequestAttributes getRequestAttributes() {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            return (ServletRequestAttributes) attributes;
        }
    
        /**
         * 获取session
         */
        public static HttpSession getSession() {
            return getRequest().getSession();
        }
    
        /**
         * 获取request
         *
         * @return request
         */
        public static HttpServletRequest getRequest() {
            return getRequestAttributes().getRequest();
        }
    
        /**
         * 获取response
         *
         * @return response
         */
        public static HttpServletResponse getResponse() {
            return getRequestAttributes().getResponse();
        }
    
        /**
         * 获取String参数
         */
        public static String getParameter(String name) {
            return getRequest().getParameter(name);
        }
    
        /**
         * 获取String参数
         */
        public static String getParameter(String name, String defaultValue) {
            return Convert.toStr(getRequest().getParameter(name), defaultValue);
        }
    
    
        /**
         * 获取Integer参数
         */
        public static Integer getParameterToInt(String name) {
            return Convert.toInt(getRequest().getParameter(name));
        }
    
        /**
         * 获取Integer参数
         */
        public static Integer getParameterToInt(String name, Integer defaultValue) {
            return Convert.toInt(getRequest().getParameter(name), defaultValue);
        }
    }
    

    需要注意的是,在SSM项目中如果直接使用该工具类,则会抛出一个空指针异常,原因是还需要在web.xml中配置一个监听器,代码如下:

    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    

    IpUtils

    从request中获取ip地址。判断ip是否为内网地址

    import javax.servlet.http.HttpServletRequest;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    /**
     * 获取Ip工具类
     */
    public class IpUtils {
    
        /**
         * 获取Ip地址
         *
         * @param request
         * @return
         */
        public static String getIpAddr(HttpServletRequest request) {
            if (request == null) {
                return "unknown";
            }
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Forwarded-For");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Real-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
        }
    
    
        public static boolean internalIp(String ip) {
            byte[] addr = textToNumericFormatV4(ip);
            return internalIp(addr) || "127.0.0.1".equals(ip);
        }
    
        private static boolean internalIp(byte[] addr) {
            if (addr == null || addr.length < 2) {
                return true;
            }
            final byte b0 = addr[0];
            final byte b1 = addr[1];
            // 10.x.x.x/8
            final byte SECTION_1 = 0x0A;
            // 172.16.x.x/12
            final byte SECTION_2 = (byte) 0xAC;
            final byte SECTION_3 = (byte) 0x10;
            final byte SECTION_4 = (byte) 0x1F;
            // 192.168.x.x/16
            final byte SECTION_5 = (byte) 0xC0;
            final byte SECTION_6 = (byte) 0xA8;
            switch (b0) {
                case SECTION_1:
                    return true;
                case SECTION_2:
                    if (b1 >= SECTION_3 && b1 <= SECTION_4) {
                        return true;
                    }
                case SECTION_5:
                    switch (b1) {
                        case SECTION_6:
                            return true;
                    }
                default:
                    return false;
            }
        }
    
        /**
         * 将IPv4地址转换成字节
         *
         * @param text IPv4地址
         * @return byte 字节
         */
        public static byte[] textToNumericFormatV4(String text) {
            if (text.length() == 0) {
                return null;
            }
    
            byte[] bytes = new byte[4];
            String[] elements = text.split("\.", -1);
            try {
                long l;
                int i;
                switch (elements.length) {
                    case 1:
                        l = Long.parseLong(elements[0]);
                        if ((l < 0L) || (l > 4294967295L))
                            return null;
                        bytes[0] = (byte) (int) (l >> 24 & 0xFF);
                        bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
                        bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
                        bytes[3] = (byte) (int) (l & 0xFF);
                        break;
                    case 2:
                        l = Integer.parseInt(elements[0]);
                        if ((l < 0L) || (l > 255L))
                            return null;
                        bytes[0] = (byte) (int) (l & 0xFF);
                        l = Integer.parseInt(elements[1]);
                        if ((l < 0L) || (l > 16777215L))
                            return null;
                        bytes[1] = (byte) (int) (l >> 16 & 0xFF);
                        bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
                        bytes[3] = (byte) (int) (l & 0xFF);
                        break;
                    case 3:
                        for (i = 0; i < 2; ++i) {
                            l = Integer.parseInt(elements[i]);
                            if ((l < 0L) || (l > 255L))
                                return null;
                            bytes[i] = (byte) (int) (l & 0xFF);
                        }
                        l = Integer.parseInt(elements[2]);
                        if ((l < 0L) || (l > 65535L))
                            return null;
                        bytes[2] = (byte) (int) (l >> 8 & 0xFF);
                        bytes[3] = (byte) (int) (l & 0xFF);
                        break;
                    case 4:
                        for (i = 0; i < 4; ++i) {
                            l = Integer.parseInt(elements[i]);
                            if ((l < 0L) || (l > 255L))
                                return null;
                            bytes[i] = (byte) (int) (l & 0xFF);
                        }
                        break;
                    default:
                        return null;
                }
            } catch (NumberFormatException e) {
                return null;
            }
            return bytes;
        }
    
        public static String getHostIp() {
            try {
                return InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException ignore) {}
            return "127.0.0.1";
        }
    
        public static String getHostName() {
            try {
                return InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException ignore) {}
            return "未知";
        }
    }
    

    Servlet项目工具

    CommUtil

    需要 beanutils

    public class CommonUtil {
    
        /**
         * 返回一个不重复的字符串
         * @return
         */
        public static String uuid() {
            return UUID.randomUUID().toString().replace("-", "").toUpperCase();
        }
    
        /**
         * 把 map 转换成对象
         * @param map
         * @param clazz
         * @return 把Map转换成指定类型
         */
        @SuppressWarnings("rawtypes")
        public static <T> T toBean(Map map, Class<T> clazz) {
            try {
                /*
                 * 1. 通过参数clazz创建实例
                 * 2. 使用BeanUtils.populate把map的数据封闭到bean中
                 */
                T bean = clazz.newInstance();
                ConvertUtils.register(new DateConverter(), java.util.Date.class);
                BeanUtils.populate(bean, map);
                return bean;
            } catch(Exception e) {
                throw new RuntimeException(e);
            }
        }
        
        public static class DateConverter implements Converter {
    
            @SuppressWarnings("unchecked")
            @Override
            public Object convert(Class type, Object value) {
                if(value == null) return null;//如果要转换成值为null,那么直接返回null
                if(!(value instanceof String)) {//如果要转换的值不是String,那么就不转换了,直接返回
                    return value;
                }
                String val = (String) value;//把值转换成String
    
                // 使用SimpleDateFormat进行转换
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return sdf.parse(val);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    

    BaseServlet

    public class BaseServlet extends HttpServlet {
    
        @Override
        public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");//处理响应编码
    
            String methodName = request.getParameter("method");
            Method method = null;
            try {
                method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            } catch (Exception e) {
                throw new RuntimeException("您要调用的方法:" + methodName + "它不存在!", e);
            }
            
            try {
                String result = (String) method.invoke(this, request, response);
                if (result != null && !result.trim().isEmpty()) {//如果请求处理方法返回不为空
                    if (result.startsWith("f:")) {
                        request.getRequestDispatcher(result.replaceFirst("f:", "")).forward(request, response);
                    } else if (result.startsWith("r:")) {
                        response.sendRedirect(request.getContextPath() + result.replaceFirst("d:", ""));
                    } else {
                        response.getWriter().write(result);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        protected void startPage(HttpServletRequest request) {
            int pageNum = getParameterToInt(request, "pageNum", 0);
            int pageSize = getParameterToInt(request, "pageSize", 10);
            PageHelper.startPage(pageNum, pageSize);
        }
    
        private int getParameterToInt(HttpServletRequest request, String key, int defaultValue) {
            int value = defaultValue;
            String param = request.getParameter(key);
            if (param != null && !param.trim().isEmpty()) {
                try {
                    value = Integer.parseInt(param);
                } catch (Exception ignore) {}
            }
            return value;
        }
    }
    
    作者: 贺墨于
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    技术选型总结
    这些年来收集的好用的好玩的软件,强烈推荐给大家
    如何解决markdown中图片上传的问题
    01.如何把.py文件打包成为exe,重点讲解pyinstaller的用法
    Asp.net中汉字转换成为拼音
    程序员常用网址收集
    通过IP来判断所在城市
    以太坊
    分布式系统领域经典论文翻译集
    T50
  • 原文地址:https://www.cnblogs.com/hemou/p/14345828.html
Copyright © 2011-2022 走看看