zoukankan      html  css  js  c++  java
  • 年月日时分秒毫秒+随机数getSerialNum

    package com.creditharmony.apporveadapter.core.utils;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Date;
    import java.util.Random;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.commons.lang3.StringUtils;
    import org.apache.commons.lang3.time.DateFormatUtils;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.io.xml.Dom4JDriver;
    import com.thoughtworks.xstream.mapper.MapperWrapper;
    
    /**
     * 接口业务,共同处理.
     */
    public class AdapterUtils {
    
        /**
         * 根据传入的数字, 产生 【年月日时分秒毫秒+随机数】. 格式:yyyyMMddHHmmssSSS + '_' + 四位随机数
         * 
         * @return 25位 随机数
         */
        public static String getSerialNum() {
            Random random = new Random();
            String sRand = "";
            for (int i = 0; i < 2; i++) {
                String rand = String.valueOf(random.nextInt(10));
                sRand += rand;
            }
            for (int i = 0; i < 2; i++) {
                random = new Random();
                String rand = String.valueOf(random.nextInt(10));
                sRand += rand;
            }
    
            StringBuilder randNoSb = new StringBuilder();
    		String dateStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS");
    		randNoSb.append(dateStr).append(sRand);
            
            return randNoSb.toString();
        }
    
        /**
         * 取得邮件地址的服务商
         * 
         * @param userAddr 邮件地址
         * @return 邮件服务商
         */
        public static String getMailExtName(String userAddr) {
            if (StringUtils.isEmpty(userAddr) == false) {
                if (userAddr.indexOf("@") > 0) {
                    int lastIdx = userAddr.lastIndexOf("@");
                    return userAddr.substring(lastIdx);
                } else {
                    return "";
                }
            } else {
                return "";
            }
        }
    
        /**
         * 按照指定的格式取出区间参数.
         * 
         * @param param 信息
         * @return 状态号
         */
        public static String getParam(String param, String inPattern) {
            String code = "";
    
            String pattern = "\<" + inPattern + ">(.*?)\</" + inPattern + ">";
            // 正则表达式,取<error>和</error>之间的字符串
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(param);
            // 取得对应的返回编号
            while (m.find()) {
                code = m.group(1);
            }
            return code;
        }
    
        /**
         * xml转bean. 将传入的XML转为对应的javaBean 该方法可忽略缺少或者多出的XML中节点对象
         * 
         * @param xml 源XML内容
         * @param clazz 对象Class
         * @return 转换后Bean
         */
        @SuppressWarnings("unchecked")
        public static <T> T xmlToBean(String xml, Class<T>... clazz) {
    
            T t = null;
            // xml转对象需要用到dom4j.jar包
            XStream xStream = new XStream(new Dom4JDriver()) {
    
                protected MapperWrapper wrapMapper(MapperWrapper next) {
                    return new MapperWrapper(next) {
                        @SuppressWarnings("rawtypes")
                        public boolean shouldSerializeMember(Class definedIn, String fieldName) {
    
                            if (definedIn == Object.class) {
                                try {
                                    return this.realClass(fieldName) != null;
    
                                } catch (Exception e) {
                                    return false;
                                }
                            }
                            return super.shouldSerializeMember(definedIn, fieldName);
                        }
                    };
                }
    
            };
            xStream.setMode(XStream.NO_REFERENCES);
            xStream.processAnnotations(clazz);
            t = (T) xStream.fromXML(xml);
    
            return t;
        }
    
        /**
         * 文件byte转文件InputStream 处理.
         * 
         * @param buf 文件byte
         * @return 文件InputStream
         */
        public static final InputStream byte2Input(byte[] buf) {
            return new ByteArrayInputStream(buf);
        }
    
        /**
         * 文件InputStream转文件byte 处理.
         * 
         * @param inStream 文件InputStream
         * @return 文件byte
         */
        public static final byte[] input2byte(InputStream inStream) throws IOException {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = inStream.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            swapStream.close();
            byte[] in2b = swapStream.toByteArray();
            return in2b;
        }
        
        /**
         * 获取客户端IP地址.
         * @param serialNum 请求唯一序列号
         * @return IP地址
         */
        public static String getClientIP(HttpServletRequest request, String serialNum) {
            try {
                if (request.getHeader("x-forwarded-for") == null) {
                    return request.getRemoteAddr();
                }
                return request.getHeader("x-forwarded-for");
            } catch (Exception e) {
            }
            return "";
        }
        
        /**
         * 获取当前项目URL地址.
         * 
         * @return URL地址
         */
        public static String getRequestPath() {
    		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    		String path = request.getRequestURL() + "/";
    		return path;
        }
        
        public static String readString3(String pathName)
    	{
    	    String str="";
    	    File file=new File(pathName);
    	    try {
    
    	        FileInputStream in=new FileInputStream(file);
    
    	        // size  为字串的长度 ,这里一次性读完
    
    	        int size=in.available();
    
    	        byte[] buffer=new byte[size];
    
    	        in.read(buffer);
    
    	        in.close();
    
    	        str=new String(buffer,"utf-8");
    
    	    } catch (IOException e) {
    	        // TODO Auto-generated catch block
    	        return null;
    	    }
    	    return str;
    
    	}
    }
    

      

    www.beicaiduo.com
  • 相关阅读:
    石子合并之一
    最长公共子序列
    最长上升子序列
    剑圣的逃跑
    方格取数
    数字三角形
    Omkar and Last Class of Math
    IDEA中对代码进行测试
    Spark(一)【spark-3.0安装和入门】
    HTTP请求 Java API
  • 原文地址:https://www.cnblogs.com/hoge66/p/10742588.html
Copyright © 2011-2022 走看看