All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mycomm.YesHttp.comm.SystemUtil Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycomm.YesHttp.comm;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.File;
import java.io.FileInputStream;
import java.io.Reader;

/**
 *
 * @author jw362j
 */
public class SystemUtil {
    private static String default_charSet = "UTF-8";

    public static InputStream getInputStream(String msg) {
//        logger.info("the delivery msg is:" + msg);
        if ("".equals(msg) || msg == null) {
            return null;
        }
        InputStream inputStream = null;
        try {
            inputStream = new ByteArrayInputStream(msg.getBytes(default_charSet));
        } catch (UnsupportedEncodingException e) {
        }
        return inputStream;
    }

    public static boolean isEmailFormat(String email) {
        if ("".equals(email) || email == null) {
            return false;
        }
        String reg = "^\\w+([-.]\\w+)*@\\w+([-]\\w+)*\\.(\\w+([-]\\w+)*\\.)*[a-z]{2,3}$";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

   

    public static boolean isPasswordValid(String password) {
        boolean result = true;
        if ("".equals(password) || password == null) {
            return false;
        }
        if (password.length() < 6) {
            return false;
        }
        if (password.startsWith("asd")) {
            result = false;
        }

        return result;
    }

    /**
     * 将图片文件转化为字符串,并对其进行Base64编码处理
     *
     * @param imgPath
     * @return
     */
    
    public static String getMACAddress(String ip) {
        String str;
        String macAddress = "";
        try {
            Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
            InputStreamReader ir = new InputStreamReader(p.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (int i = 1; i < 100; i++) {
                str = input.readLine();
                if (str != null) {
                    if (str.indexOf("MAC Address") > 1) {
                        macAddress = str.substring(
                                str.indexOf("MAC Address") + 14, str.length());
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return macAddress;
    }

    
    public static String ReadFile(String fileFullPath, String charSet, String[] allowed_file_postfix_list) {
        String extentsion = getExtensionName(fileFullPath);
        extentsion = extentsion.toLowerCase();
        if (!isContainItem(extentsion, allowed_file_postfix_list)) {
            return "bad file extention type!";
        }
        return ReadFromFile(fileFullPath, charSet);
    }

    public static String ReadFromFile(String fileFullPath, String charSet) {
        charSet = (charSet == null || "".equals(charSet)) ? default_charSet : charSet;
        StringBuilder sb = new StringBuilder();
        if ("".equals(fileFullPath) || fileFullPath == null) {
            sb.append("文件不存在:").append(fileFullPath);
            return sb.toString();
        }

        File file = new File(fileFullPath);
        if (!file.exists()) {
            sb.append("文件不存在:").append(fileFullPath);
            return sb.toString();
        }
        Reader reader = null;
        try {
            // 一次读一个字符
            reader = new InputStreamReader(new FileInputStream(fileFullPath), charSet);
            int tempchar;
            while ((tempchar = reader.read()) != -1) {
                sb.append((char) tempchar);
            }
            reader.close();
        } catch (Exception e) {
            sb.append("file error:").append(e.getMessage());
        }
        if ("".equals(sb.toString())) {
            return "file content is empty!";
        }

        return sb.toString();
    }

    public static boolean isContainItem(String filename, String[] items) {
        boolean result = false;
        if (items == null || items.length < 1) {
            return result;
        }
        for (String name : items) {
            if (name.equals(filename)) {
                result = true;
                break;
            }
        }
        return result;
    }

    public static String[] StringArrayMerge(String[] strArray1, String[] strArray2) {
        String[] targetArray = null;
        if (strArray1 == null && strArray2 == null) {
            return targetArray;
        }
        if (strArray1 != null || strArray2 != null) {
            int targetLength = (strArray1 == null ? 0 : strArray1.length) + (strArray2 == null ? 0 : strArray2.length);
            if (targetLength == 0) {
                return targetArray;
            }
            targetArray = new String[targetLength];
            int index = 0;

            if (strArray1 != null && strArray1.length > 0) {
                for (String tem_a1 : strArray1) {
                    targetArray[index] = tem_a1;
                    index++;
                }
            }
            if (strArray2 != null && strArray2.length > 0) {
                for (String tem_a2 : strArray2) {
                    targetArray[index] = tem_a2;
                    index++;
                }
            }
        }
        return targetArray;
    }

    /* 
     * Java文件操作 获取文件扩展名 
     * 
     *  Created on: 2011-8-2 
     *      Author: blueeagle 
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    /* 
     * Java文件操作 获取不带扩展名的文件名 
     * 
     *  Created on: 2011-8-2 
     *      Author: blueeagle 
     */
    public static String getFileNameNoPostFix(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }

    public static String getQueryStringItem(String qstring, String keyItem) {
        String result = null;
        if ("".equals(qstring) || qstring == null || "".equals(keyItem) || keyItem == null) {
            return result;
        }
        String[] qs = qstring.split("&");
        if (qs == null || qs.length < 1) {
            return result;
        }
        for (String s : qs) {
            String[] s_ = s.split("=");
            if (s_ != null && s_.length == 2) {
                if (keyItem.equals(s_[0])) {
                    result = s_[1];
                    break;
                }
            }
        }
        return result;

    }

    public static boolean isNumberString(String target) {
        boolean result = false;
        if ("".equals(target) || target == null) {
            return result;
        }
        return Pattern.compile("[0-9]*").matcher(target).matches();

    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static String byteArraytoHex(byte[] buf) {
        StringBuilder strbuf = new StringBuilder(buf.length * 2);
        int i;
        for (i = 0; i < buf.length; i++) {
            if (((int) buf[i] & 0xff) < 0x10) {
                strbuf.append("0");
            }
            strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
        }
        return strbuf.toString();
    }
  
 

    /*
     * 中文转unicode编码
     */
    public static String cn2Unicode(final String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for (int i = 0; i < utfBytes.length; i++) {
            String hexB = Integer.toHexString(utfBytes[i]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        return unicodeBytes;
    }

    /*
     * unicode编码转中文
    unicode编码必须为6位长度 如果不足则在前面补0 ,如4E03 补为004E03
     */
    public static String Unicode2cn(final String dataStr) {

        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }

    private static boolean isChineseCharacter(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {
            return true;
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy