zoukankan      html  css  js  c++  java
  • java打开目录(含推断操作系统工具类和解压缩工具类)

    1、Runtime.getRuntime().exec("explorer D:\Java");


    2、java.awt.Desktop.getDesktop().open(new File("D:\Java"));    

    4、java.awt.Desktop.getDesktop().browse(...)


    3、    try {  
                    String[] cmd = new String[5];  
                    cmd[0] = "cmd";  
                    cmd[1] = "/c";  
                    cmd[2] = "start";  
                    cmd[3] = " ";  
                    cmd[4] = FILE_PATH;  
                    Runtime.getRuntime().exec(cmd);  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  

    4、    /*** 
             *  
             * @param folder 
             *            : directory 
             */  
            public static void open_directory(String folder) {  
                File file = new File(folder);  
                if (!file.exists()) {  
                    return;  
                }  
                Runtime runtime = null;  
                try {  
                    runtime = Runtime.getRuntime();  
                    if (!SystemUtil.isWindows) {  
                        // System.out.println("is linux");  
                        runtime.exec("nautilus " + folder);  
                    } else {  
                        runtime.exec("cmd /c start explorer " + folder);  
                    }  
                } catch (IOException ex) {  
                    ex.printStackTrace();  
                } finally {  
                    if (null != runtime) {  
                        runtime.runFinalization();  
                    }  
                }  
            }  

    5、    /*** 
             *  
             * @param filePath 
             *            : only regular file 
             */  
            public static void open_file(String filePath) {  
                File file = new File(filePath);  
                if (!file.exists()) {  
                    return;  
                }  
                Runtime runtime = null;  
                try {  
                    runtime = Runtime.getRuntime();  
                    if (!SystemUtil.isWindows) {  
                        // System.out.println("is linux");  
                        runtime.exec("nautilus " + filePath);  
                    } else {  
                        runtime.exec("cmd /c start explorer /select,/e, " + filePath);  
                    }  
                } catch (IOException ex) {  
                    ex.printStackTrace();  
                } finally {  
                    if (null != runtime) {  
                        runtime.runFinalization();  
                    }  
                }  
            }  



    其它工具类

    package com.common.util;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.Serializable;
    import java.io.UnsupportedEncodingException;
    import java.lang.reflect.Method;
    import java.lang.reflect.ParameterizedType;
    import java.lang.reflect.Type;
    import java.math.BigInteger;
    import java.net.URLEncoder;
    import java.security.Key;
    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.SecureRandom;
    import java.security.Signature;
    import java.security.cert.CertificateException;
    import java.security.cert.CertificateFactory;
    import java.security.cert.X509Certificate;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.RSAPrivateKeySpec;
    import java.security.spec.RSAPublicKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Random;
    import java.util.Set;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import javax.crypto.Cipher;
    import javax.crypto.Mac;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.SecretKeySpec;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    import org.apache.commons.lang.StringUtils;
    
    import com.io.hw.exception.MyException;
    import com.io.hw.file.util.FileUtils;
    import com.string.widget.util.ValueWidget;
    import com.time.util.TimeHWUtil;
    
    public class SystemUtil {
    	public static final String OSNAME = System.getProperty("os.name");
    	public static final String OSARCH = System.getProperty("os.arch");
    	public static final String SEPARATOR = System.getProperty("file.separator");
    	public static final String USER_DIR = System.getProperty("user.dir");
    	public static final String CURR_ENCODING = System
    			.getProperty("file.encoding");
    	public static final String LINE_SEPARATOR = System
    			.getProperty("line.separator");
    	public static final String CRLF = LINE_SEPARATOR; // Carriage Return/Line
    	// Feed
    	public static final String CRLF_LINUX = "
    ";
    	public static final String CHARSET_UTF = "UTF-8";
    	public static final String CHARSET_GBK = "GBK";
    	public static final String CHARSET_GB2312 = "GB2312";
    	public static final String CHARSET_GB18030 = "GB18030";
    	public static final String CHARSET_UNICODE = "UNICODE";
    	public static final String CHARSET_ISO88591 = "ISO-8859-1";
    
    	public static boolean isWindows = false;
    	public static boolean isHP_UX = false;
    	public static boolean isSolaris = false;
    	public static boolean isOS32bit = true;
    	public static final int BUFF_SIZE = 4096;
    	public static final String KEY_ALGORITHM_RSA = "RSA";
    	public final static String KEY_ALGORITHM_DES = "DES";
    	public static final String KEY_ALGORITHM_SHA1withRSA = "SHA1withRSA";
    	public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
    	public static final String KEY_SHA = "SHA";
    	public static final String KEY_SHA1 = "SHA-1";
    	public static final String KEY_MD5 = "MD5";
    	public static final String KEY_HMAC_SHA256 = "HMACSHA256";
    	public static final String KEY_HMAC_SHA1 = "HmacSHA1";
    	public static final String CERTIFICATEFACTORY_X509 = "X.509";
    	public static final char[] HEXCHAR = { '0', '1', '2', '3', '4', '5', '6',
    			'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    	public static BigInteger bigInt1 = BigInteger.valueOf(1l);
    	public static final String CONTENTTYPE_HTML = "text/html";
    	public static final String CONTENTTYPE_JSON = "application/json";
    	public static final String CONTENTTYPE_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
    
    	public static final String CONTENTTYPE_OCTET_STREAM = "application/octet-stream";
    
    	public static final String PROCOTOL_HTTP = "http";
    	public static final String PROCOTOL_HTTPS = "https";
    	public static final String COLON = ":";
        /**
         * 字符串空格
         */
        public static final String BLANK = " ";
        public static final String ENGLISH_PERIOD = ".";
    	public static final String DIVIDING_LINE = "---------------------------------------";
    
    	public static final int NEGATIVE_ONE = -1;
    	public static final String EMPTY = "";
    	static {
    		if (SystemUtil.OSNAME.toLowerCase().contains("window")) {
    			isWindows = true;
    		}
    		if (SystemUtil.OSNAME.toLowerCase().contains("hp-ux")) {
    			isHP_UX = true;
    		}
    		if (SystemUtil.OSNAME.toLowerCase().contains("Solaris")) {
    			isSolaris = true;
    		}
    		if (SystemUtil.OSARCH.contains("64")) {
    			isOS32bit = false;
    		}
    	}
    
    	private SystemUtil() {
    		throw new Error("Don't let anyone instantiate this class.");
    	}
    
    	public static void copyFile(String resourceFileName, String targetFileName)
    			throws IOException {
    		File resourceFile = new File(resourceFileName);
    		File targetFile = new File(targetFileName);
    		if (!resourceFile.exists()) {
    			System.out.println("[copyFile ]: resource file has not been found:"
    					+ resourceFileName);
    		}
    		if (!resourceFile.isFile()) {
    			System.out.println("[copyFile ]: directory can not be copyed:"
    					+ resourceFileName);
    		}
    
    		if (targetFile.isDirectory()) {
    			targetFile = new File(targetFile, resourceFile.getName());
    		}
    
    		FileInputStream resource = null;
    		FileOutputStream target = null;
    		try {
    			resource = new FileInputStream(resourceFile);
    			target = new FileOutputStream(targetFile);
    			copyFile(resourceFile, targetFile);
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			if (resource != null) {
    				resource.close();
    			}
    			if (target != null) {
    				target.close();
    			}
    		}
    	}
    
    	/**
    	 * 
    	 * @param srcFile
    	 *            :must be regular file,can not be folder;
    	 * @param targetFile
    	 *            :must be regular file,can not be folder;
    	 * @throws IOException
    	 */
    	public static void copyFile(File srcFile, File targetFile)
    			throws IOException {
    		FileInputStream in = new FileInputStream(srcFile);
    
    		FileOutputStream out = new FileOutputStream(targetFile);
    		copyFile(in, out);
    
    	}
    
    	public static void copyFile(InputStream in, FileOutputStream target)
    			throws IOException {
    		// File targetFile = new File(targetFileName);
    		// FileOutputStream target = null;
    		// if (targetFile.isDirectory())
    		// {
    		// targetFile = new File(targetFile, simpleName);
    		// }
    		try {
    			// target = new FileOutputStream(targetFile);
    			byte[] buffer = new byte[BUFF_SIZE];
    			int byteNum;
    
    			while ((byteNum = in.read(buffer)) != NEGATIVE_ONE) {
    				target.write(buffer, 0, byteNum);
    
    			}
    			System.out.println("[SystemUtil:copyFile]:file copy successfully!");
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			if (in != null) {
    				in.close();
    				in = null;
    			}
    			if (target != null) {
    				target.close();
    				target = null;
    			}
    		}
    	}
    
    	/**
    	 * 
    	 * @param fullpath
    	 *            :/a/b/c/d
    	 * @return /a/b/c/
    	 */
    	public static String getParentDir(String fullpath) {
    		// String parentDir = null;
    		if (ValueWidget.isNullOrEmpty(fullpath)) {
    			System.out.println("The first argument can not be null");
    			return null;
    		}
    
    		// if (fullpath.contains("/")) {
    		// int index=fullpath.lastIndexOf("/") ;
    		// parentDir = fullpath.substring(0, index+ 1);
    		// }
    		return new File(fullpath).getParent();
    	}
    
    	/**
    	 * 
    	 * @param fullpath
    	 *            :/a/b/c/d
    	 * @return d
    	 */
    	public static String getFileSimpleName(String fullpath) {
    		// String parentDir = null;
    		if (null == fullpath) {
    			System.out.println("The first argument can not be null");
    			return null;
    		}
    		// if (fullpath.contains("/")) {
    		// parentDir = fullpath.substring(fullpath.lastIndexOf("/") + 1);
    		// }
    		return new File(fullpath).getName();
    	}
    
    	// public static void main(String[] args) throws IOException
    	// {
    	// copyFile("/home/whuang2/study/linux/study/c/main.exe", "/home/whuang2");
    	// }
    	public static String convertUTF2ISO(String oldName) {
    		if (oldName == null) {
    			return oldName;
    		}
    		try {
    			return new String(oldName.getBytes(CHARSET_UTF), CHARSET_ISO88591);
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	public static String convertGBK2ISO(String input) {
    		if (input == null) {
    			return input;
    		}
    		try {
    			return new String(input.getBytes(CHARSET_GBK), CHARSET_ISO88591);
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	/***
    	 * convert GBK to UTF-8
    	 * 
    	 * @param input
    	 * @return
    	 * @throws UnsupportedEncodingException
    	 */
    	public static byte[] convertGBK2UTF(byte[] input)
    			throws UnsupportedEncodingException {
    		return new String(input, SystemUtil.CHARSET_GBK)
    				.getBytes(SystemUtil.CHARSET_UTF);
    	}
    
    	public static byte[] convertFromeGBK(byte[] input, String toCharset)
    			throws UnsupportedEncodingException {
    		return new String(input, SystemUtil.CHARSET_GBK).getBytes(toCharset);
    	}
    
    	/***
    	 * convert utf-8 to gbk
    	 * 
    	 * @param input
    	 * @return
    	 */
    	public static String convertUTF2GBK(String input) {
    		if (input == null) {
    			return input;
    		}
    		try {
    			return new String(input.getBytes(CHARSET_UTF), CHARSET_GBK);
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	public static String convertISO2UTF(String oldName) {
    		if (oldName == null) {
    			return oldName;
    		}
    		try {
    			return new String(oldName.getBytes(CHARSET_ISO88591), CHARSET_UTF);
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	public static String convertISO2GBK(String input) {
    		if (input == null) {
    			return input;
    		}
    		try {
    			return new String(input.getBytes(CHARSET_ISO88591), CHARSET_GBK);
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	public static void printFilesSimpleName(File[] files) {
    		for (File file : files) {
    			System.out.println(file.getName());
    		}
    	}
    
    	public static void printFilesFilePath(File[] files) {
    		for (File file : files) {
    			System.out.println(file.getAbsolutePath());
    		}
    	}
    
    	/***
    	 * 
    	 * @param srcfile
    	 *            : source file
    	 * @param targfile
    	 *            : target file
    	 * @param inputCharset
    	 *            : from charset
    	 * @param outputCharset
    	 *            : to charset
    	 */
    	public static void convertEncoding(File srcfile, File targfile,
    			String inputCharset, String outputCharset) {
    		FileInputStream fin = null;
    		FileOutputStream fout = null;
    		char[] cbuf = new char[BUFF_SIZE];
    		int size_char;
    		try {
    			fin = new FileInputStream(srcfile);
    			fout = new FileOutputStream(targfile);
    			InputStreamReader isr = null;
    			OutputStreamWriter osw = null;
    			try {
    				isr = new InputStreamReader(fin, inputCharset);
    				osw = new OutputStreamWriter(fout, outputCharset);
    				while ((size_char = isr.read(cbuf)) != NEGATIVE_ONE) {
    					osw.write(cbuf, 0, size_char);
    				}
    				//
    			} catch (UnsupportedEncodingException e1) {
    				e1.printStackTrace();
    			} catch (IOException e1) {
    				e1.printStackTrace();
    			} finally {
    				try {
    					isr.close();
    					osw.close();
    				} catch (IOException e1) {
    					e1.printStackTrace();
    				}
    
    			}
    
    		} catch (FileNotFoundException e1) {
    			e1.printStackTrace();
    		} finally {
    			try {
    				if (fin != null) {
    					fin.close();
    				}
    				if (fout != null) {
    					fout.close();
    				}
    			} catch (IOException e1) {
    				e1.printStackTrace();
    			}
    		}
    	}
    
    	/**
    	 * delete the same one
    	 * 
    	 * @param list
    	 * @return
    	 */
    	public static List<String> guolv(List<String> list) {
    		List<String> newlist = new ArrayList<String>();
    		if (list != null && list.size() > 0) {
    			newlist.add(list.get(0));
    			for (int i = 1; i < list.size(); i++) {
    				if (!newlist.contains(list.get(i))) {
    					newlist.add(list.get(i));
    				}
    			}
    		}
    		return newlist;
    	}
    
    	/***
    	 * delete CRLF; delete empty line ;delete blank lines
    	 * 
    	 * @param input
    	 * @return
    	 */
    	private static String deleteCRLFOnce(String input) {
    		if (ValueWidget.isHasValue(input)) {
    			return input.replaceAll("((
    )|
    )[\s	 ]*(\1)+", "$1")
    					.replaceAll("^((
    )|
    )", "");
    		} else {
    			return null;
    		}
    	}
    
    	/***
    	 * Delete all spaces
    	 * 
    	 * @param input
    	 * @return
    	 */
    	public static String deleteAllCRLF(String input) {
    		return input.replaceAll("((
    )|
    )[\s	 ]*", "").replaceAll(
    				"^((
    )|
    )", "");
    	}
    
    	/**
    	 * delete CRLF; delete empty line ;delete blank lines
    	 * 
    	 * @param input
    	 * @return
    	 */
    	public static String deleteCRLF(String input) {
    		input = SystemUtil.deleteCRLFOnce(input);
    		return SystemUtil.deleteCRLFOnce(input);
    	}
    
    	/***
    	 * Use uniqueness of collection
    	 * 
    	 * @param list
    	 * @return
    	 */
    	public static List<String> guolv2(List<String> list) {
    		Set<String> set = new HashSet<String>(list);
    		return new ArrayList<String>(set);
    	}
    
    	/**
    	 * delete the same one
    	 * 
    	 * @param list
    	 * @return
    	 */
    	public static List<Integer> guolvInteger(List<Integer> list) {
    		List<Integer> newlist = new ArrayList<Integer>();
    		if (list != null && list.size() > 0) {
    			newlist.add(list.get(0));
    			for (int i = 1; i < list.size(); i++) {
    				if (!newlist.contains(list.get(i))) {
    					newlist.add(list.get(i));
    				}
    			}
    		}
    		return newlist;
    	}
    
    	public static List<Integer> guolvInteger2(List<Integer> list) {
    		Set<Integer> set = new HashSet<Integer>(list);
    		return new ArrayList<Integer>(set);
    	}
    
    	/**
    	 * 字节数大于1,则返回true
    	 * 
    	 * @param c
    	 * @return
    	 */
    	public static boolean isChinese(char c) {
    		return String.valueOf(c).getBytes().length > 1;
    	}
    
    	/**
    	 * 推断str 中的全部字符是否全部是中文字符(包括中文的标点符号)
    	 * 
    	 * @param str
    	 * @return
    	 */
    	public static boolean isAllChinese(String str) {
    		char[] cs = null;
    		if (str.length() == 1) {
    			cs = new char[1];
    			cs[0] = str.charAt(0);
    		} else {
    			cs = str.toCharArray();
    		}
    		for (int i = 0; i < cs.length; i++) {
    			char c = cs[i];
    			if (!isChinese(c)) {
    				return false;
    			}
    		}
    		return true;
    	}
    
    	public static boolean isHasChinses(String str) {
    		String encodeName = "UTF-8";
    		for (int i = 0; i < str.length(); i++) {
    			try {
    				String singleStr = str.substring(i, i + 1);
    				int leng = getEncodeLength(singleStr, encodeName);
    				// System.out.println(singleStr + "	" + leng);
    				if (leng == 9)// 表示是中文字符
    				{
    					// System.out.println("有中文");
    					return true;
    				}
    			} catch (UnsupportedEncodingException e) {
    				e.printStackTrace();
    			} catch (MyException e) {
    				e.printStackTrace();
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    		return false;
    	}
    
    	public static boolean isHasChinses2(String str) {
    		String encodeName = "UTF-8";
    		char[] chars = str.toCharArray();
    		for (int i = 0; i < chars.length; i++) {
    			try {
    				char c = chars[i];
    				int leng = getEncodeLength(c, encodeName);
    				// System.out.println(singleStr + "	" + leng);
    				if (leng == 9)// 表示是中文字符
    				{
    					// System.out.println("有中文");
    					return true;
    				}
    			} catch (UnsupportedEncodingException e) {
    				e.printStackTrace();
    			} catch (MyException e) {
    				e.printStackTrace();
    			}
    		}
    		return false;
    	}
    
    	public static int getEncodeLength(String str, String encodeName)
    			throws Exception, UnsupportedEncodingException {// 返回值为9 的话,则说明有中文。

    if (str.length() != 1) { throw new Exception("超过一个字符"); } String encod = URLEncoder.encode(str, "UTF-8"); return encod.length(); } public static int getEncodeLength(char c, String encodeName) throws MyException, UnsupportedEncodingException {// 返回值为9 // 的话,则说明有中文。

    String encod = URLEncoder.encode(String.valueOf(c), "UTF-8"); return encod.length(); } /** * 删除input字符串中的html格式 * * @param input * @param length * 显示的字符的个数 * @return */ public static String splitAndFilterString(String input, int length) { if (input == null || input.trim().equals("")) { return ""; } // 去掉全部html元素, String str = input.replaceAll("\&[a-zA-Z]{1,10};", "").replaceAll( "<[^>]*>", ""); str = str.replaceAll("[(/>)<]", ""); int len = str.length(); if (len <= length) { return str; } else { str = str.substring(0, length); str += "......"; } return str; } /** * 返回纯文本,去掉html的全部标签,而且去掉空行 * * @param input * @return */ public static String splitAndFilterString(String input) { if (input == null || input.trim().equals("")) { return ""; } // 去掉全部html元素, String str = input.replaceAll("\&[a-zA-Z]{1,10};", "").replaceAll( "<[^>]*>", ""); str = str.replaceAll("[(/>)<]", ""); return SystemUtil.deleteCRLF(str); } public static boolean contains(List<Object> list, Object value) { if (list == null || list.size() == 0) { return false; } else { for (int i = 0; i < list.size(); i++) { String valueStr; if (value instanceof File) { valueStr = ((File) value).getName(); } else { valueStr = value.toString(); } Object obj = list.get(i); if (obj instanceof File) { if (list.contains(valueStr) || ((File) obj).getName().toString() .equals(valueStr)) { return true; } } else { if (list.contains(valueStr) || list.get(i).toString().equals(valueStr)) { return true; } } } } return false; } /** * minus Set * * @param oldList * @param list * @return */ public static List<Object> getMinusSet(List oldList, List list) { List selectedList = null; if (oldList != null) { selectedList = new ArrayList<Object>(); int leng = oldList.size(); if (leng != 0) { for (int i = 0; i < leng; i++) { Object obj = oldList.get(i); if (!contains(list, obj)) { selectedList.add(obj); } } } } return selectedList; } public static List<File> getMinusSetFile(List oldList, List list) { List selectedList = null; if (oldList != null) { selectedList = new ArrayList<File>(); int leng = oldList.size(); if (leng != 0) { for (int i = 0; i < leng; i++) { Object obj = oldList.get(i); if (!contains(list, obj)) { selectedList.add(obj); } } } } return selectedList; } public static List<String> getMinusSetStr(List oldList, List list) { List selectedList = null; if (oldList != null) { selectedList = new ArrayList<Object>(); int leng = oldList.size(); if (leng != 0) { for (int i = 0; i < leng; i++) { Object obj = oldList.get(i); if (!contains(list, obj)) { selectedList.add(obj); } } } } return selectedList; } /** * Get MD5 of one file:hex string,test OK! * * @param file * @return */ public static String getFileMD5(File file) { if (!file.exists() || !file.isFile()) { return null; } MessageDigest digest = null; FileInputStream in = null; byte buffer[] = new byte[1024]; int len; try { digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer, 0, 1024)) != NEGATIVE_ONE) { digest.update(buffer, 0, len); } in.close(); } catch (Exception e) { e.printStackTrace(); return null; } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); } /*** * Get MD5 of one file!

    test ok! * * @param filepath * @return */ public static String getFileMD5(String filepath) { File file = new File(filepath); return getFileMD5(file); } /** * MD5 encrypt,test ok * * @param data * @return byte[] * @throws Exception */ public static byte[] encryptMD5(byte[] data) throws Exception { MessageDigest md5 = MessageDigest.getInstance(SystemUtil.KEY_MD5); md5.update(data); return md5.digest(); } public static byte[] encryptMD5(String data) throws Exception { return encryptMD5(data.getBytes(SystemUtil.CHARSET_ISO88591)); } /*** * compare two file by Md5 * * @param file1 * @param file2 * @return */ public static boolean isSameMd5(File file1, File file2) { String md5_1 = SystemUtil.getFileMD5(file1); String md5_2 = SystemUtil.getFileMD5(file2); return md5_1.equals(md5_2); } /*** * compare two file by Md5 * * @param filepath1 * @param filepath2 * @return */ public static boolean isSameMd5(String filepath1, String filepath2) { File file1 = new File(filepath1); File file2 = new File(filepath2); return isSameMd5(file1, file2); } /*** * the times target occur in <code>int[] ints</code> * * @param ints * @param target * @return */ public static int count(int[] ints, int target) { int count = 0; for (int i = 0; i < ints.length; i++) { if (ints[i] == target) { count++; } } return count; } /*** * Ignore Case * * @param strs * @param target * @return */ public static int count(String[] strs, String target) { int count = 0; for (int i = 0; i < strs.length; i++) { if (strs[i].equalsIgnoreCase(target)) { count++; } } return count; } /*** * Ignore Case * * @param list * @param target * @return */ public static int count(List<String> list, String target) { int count = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i).equalsIgnoreCase(target)) { count++; } } return count; } // public static void printSet(Set<Integer>set ){ // for(Iterator<Integer> it=set.iterator();it.hasNext();){ // Integer age=it.next(); // System.out.println(age); // } // } /*** * * @param list */ public static void printList(List<?> list, boolean isNewline, String delimiter) { for (int i = 0; i < list.size(); i++) { Object obj = list.get(i); if (isNewline) { System.out.println(obj); } else { System.out.print(obj + delimiter); } } } public static void printList(List<?> list, String delimiter) { printList(list, true, delimiter); } public static void printStrList(List<String> list) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } public static void printSet(Set<Object> set) { for (Iterator<Object> it = set.iterator(); it.hasNext();) { Object age = it.next(); System.out.println(age); } } public static <T extends Serializable> T clone2(T obj) { T clonedObj = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(obj); oos.close(); ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); clonedObj = (T) ois.readObject(); ois.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return clonedObj; } /*** * convert byte array to public key algorithm : RSA * * @param keyBytes * byte[] * @return RSAPublicKey * @throws Exception */ public static PublicKey convert2PublicKey(byte[] keyBytes) throws Exception { X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory .getInstance(SystemUtil.KEY_ALGORITHM_RSA);// RSA PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); return publicKey; } /*** * * @param keyHexStr * : hex(16) string * @return PublicKey * @throws Exception */ public static PublicKey convert2PublicKey(String keyHexStr) throws Exception { byte[] keyBytes = toBytes(keyHexStr); return convert2PublicKey(keyBytes); } /** * convert public key to hex(16) bit string * * @param publicKey * @return hex(16) bit string */ public static String convert4PublicKey(PublicKey publicKey) { return toHexString(publicKey.getEncoded()); } public static PublicKey getPublicKey(InputStream in) throws CertificateException { CertificateFactory cf = CertificateFactory .getInstance(SystemUtil.CERTIFICATEFACTORY_X509); X509Certificate oCertServer = (X509Certificate) cf .generateCertificate(in); PublicKey pubKey = oCertServer.getPublicKey(); return pubKey; } /*** * * @param hex * :hex(16) bit string * @return PublicKey * @throws CertificateException */ public static PublicKey getPublicKey(String hex) throws CertificateException { InputStream in = FileUtils.getInputSream2hexString(hex); return getPublicKey(in); } /*** * * @param modulus * :N * @param publicExponent * :E * @return * @throws Exception */ public static PublicKey getPublicKey(String modulus, String publicExponent) throws Exception { BigInteger m = new BigInteger(modulus); BigInteger e = new BigInteger(publicExponent); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e); KeyFactory keyFactory = KeyFactory .getInstance(SystemUtil.KEY_ALGORITHM_RSA); PublicKey publicKey = keyFactory.generatePublic(keySpec); return publicKey; } // public static PublicKey getPublicKey(BigInteger m, BigInteger e){ // RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e); // KeyFactory keyFactory = KeyFactory // .getInstance(SystemUtil.KEY_ALGORITHM_RSA); // // PublicKey publicKey = keyFactory.generatePublic(keySpec); // return publicKey; // } /*** * * @param modulus * @param ePublicExponent * @return * @throws Exception */ public static PublicKey getPublicKey(BigInteger modulus, BigInteger ePublicExponent) throws Exception { RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, ePublicExponent); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(keySpec); return publicKey; } /*** * * @param m * @param publicExponent * @return * @throws Exception */ public static PublicKey getPublicKey(BigInteger m, byte[] publicExponent) throws Exception { BigInteger e = new BigInteger(publicExponent); return getPublicKey(m, e); } /** * convert byte array to private key algorithm : RSA * * @param keyBytes * byte[] * @return RSAPrivateKey * @throws Exception */ public static PrivateKey convert2PrivateKey(byte[] keyBytes) throws Exception { PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory .getInstance(SystemUtil.KEY_ALGORITHM_RSA);// RSA PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); return privateKey; } /*** * * @param keyString * : hex(16) string * @return * @throws Exception */ public static PrivateKey convert2PrivateKey(String keyString) throws Exception { byte[] keyBytes = toBytes(keyString); return convert2PrivateKey(keyBytes); } /*** * convert private key to hex bit string * * @param privateKey * @return keyString : hex(16) string */ public static String convert4PrivateKey(PrivateKey privateKey) { return toHexString(privateKey.getEncoded()); } /** * decrypt,key can be a public key, can also be a private key algorithm : * RSA * * @param message * @return * @throws Exception */ public static byte[] decrypt(byte[] message, Key key) throws Exception { Cipher cipher = Cipher.getInstance(SystemUtil.KEY_ALGORITHM_RSA); cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(message); } /** * decrypt,key can be a public key。 can also be a private key * * @param message * @return * @throws Exception */ public static byte[] decrypt(String message, Key key) throws Exception { return SystemUtil.decrypt(SystemUtil.toBytes(message), key); } /** * 解密<br> * 用私钥解密 * * @param data * @param publicKeyStr * @return * @throws Exception */ public static byte[] decryptByPublicKey(byte[] data, String publicKeyStr) throws Exception { // 对密钥解密 byte[] keyBytes = SystemUtil.toBytes(publicKeyStr); // 取得公钥 PublicKey publicKey = SystemUtil.convert2PublicKey(keyBytes); return SystemUtil.decrypt(data, publicKey); } /** * decrypt use private key to decrypt http://www.5a520.cn * http://www.feng123.com * * @param data * @param privateKeyStr * @return * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] data, String privateKeyStr) throws Exception { byte[] keyBytes = SystemUtil.toBytes(privateKeyStr); return decryptByPrivateKey(data, keyBytes); } public static byte[] decryptByPrivateKey(byte[] data, byte[] keyBytes) throws Exception { PrivateKey privateKey = SystemUtil.convert2PrivateKey(keyBytes); return SystemUtil.decrypt(data, privateKey); } /*** * * @param data * @param N * :modulus * @param D * :private exponent * @return * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] data, byte[] N, byte[] D) throws Exception { PrivateKey privateKey = getPrivateKey(N, D); return decrypt(data, privateKey); } /*** * * @param dataHex * :hex bit string * @param privateKeyStr * @param charSet * @return * @throws UnsupportedEncodingException * @throws Exception */ public static byte[] decryptByPrivateKey(String dataHex, String privateKeyStr) throws UnsupportedEncodingException, Exception { return decryptByPrivateKey(SystemUtil.toBytes(dataHex), privateKeyStr); } /** * DES * * @param data * @param key * @return * @throws Exception */ public static byte[] decryptDES(byte[] data, byte[] key) throws Exception { // Generate a random number generator which can be trusted SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); // Create a key factory, and then use it to convert DESKeySpec to // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory .getInstance(SystemUtil.KEY_ALGORITHM_DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(SystemUtil.KEY_ALGORITHM_DES); cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); } /** * DES * * @param data * @param key * @return * @throws IOException * @throws Exception */ public static String decryptDES(String data, String key) throws IOException, Exception { if (data == null) return null; byte[] buf = SystemUtil.decodeBase64(data); byte[] bt = SystemUtil.decryptDES(buf, key.getBytes(SystemUtil.CHARSET_UTF)); return new String(bt, SystemUtil.CHARSET_UTF); } /** * encrypt,key can be a public key。can also be a private key algorithm : RSA * * @param message * @return * @throws Exception */ public static byte[] encrypt(byte[] message, Key key) throws Exception { Cipher cipher = Cipher.getInstance(SystemUtil.KEY_ALGORITHM_RSA); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(message); } /** * encrypt,key can be a public key。can also be a private key * * @param message * @return * @throws Exception */ public static byte[] encrypt(String message, Key key) throws Exception { return SystemUtil.encrypt( message.getBytes(SystemUtil.CHARSET_ISO88591), key); } /** * encrypt use public key * * @param data * @param publicKeyStr * : hex bit string * @return * @throws Exception */ public static byte[] encryptByPublicKey(byte[] data, String publicKeyStr) throws Exception { byte[] keyBytes = toBytes(publicKeyStr); // get public key PublicKey publicKey = SystemUtil.convert2PublicKey(keyBytes); return SystemUtil.encrypt(data, publicKey); } /*** * * @param data * @param publicKeyStr * : hex bit string * @param charSet * @return * @throws UnsupportedEncodingException * @throws Exception */ public static byte[] encryptByPublicKey(String data, String publicKeyStr, String charSet) throws UnsupportedEncodingException, Exception { return encryptByPublicKey(data.getBytes(charSet), publicKeyStr); } /** * encrypt use private key * * @param data * @param privateKeyStr * @return * @throws Exception */ public static byte[] encryptByPrivateKey(byte[] data, String privateKeyStr) throws Exception { byte[] keyBytes = toBytes(privateKeyStr); // get private key Key privateKey = SystemUtil.convert2PrivateKey(keyBytes); return SystemUtil.encrypt(data, privateKey); } /*** * * @param data * @param privateKeyStr * : hex bit string * @param charSet * @return * @throws UnsupportedEncodingException * @throws Exception */ public static byte[] encryptByPrivateKey(String data, String privateKeyStr, String charSet) throws UnsupportedEncodingException, Exception { return encryptByPrivateKey(data.getBytes(charSet), privateKeyStr); } /** * DES * * @param data * @param key * @return * @throws Exception */ public static byte[] encryptDES(byte[] data, byte[] key) throws Exception { // Generate a random number generator which can be trusted SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); // Create a key factory, and then use it to convert DESKeySpec to // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory .getInstance(SystemUtil.KEY_ALGORITHM_DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(SystemUtil.KEY_ALGORITHM_DES); cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); } /** * DES * * @param data * @param key * @return * @throws Exception */ public static String encryptDES(String data, String key) throws Exception { byte[] bt = SystemUtil.encryptDES( data.getBytes(SystemUtil.CHARSET_UTF), key.getBytes(SystemUtil.CHARSET_UTF)); String strs = SystemUtil.encodeBase64(bt); return strs; } /** * use private key sign algorithm : RSA * * @param message * @param key * @return * @throws Exception */ public static byte[] sign(byte[] message, PrivateKey key) throws Exception { Signature signetcheck = Signature .getInstance(SystemUtil.SIGNATURE_ALGORITHM); signetcheck.initSign(key); signetcheck.update(message); return signetcheck.sign(); } /** * use private key sign * * @algorithm : RSA * @param message * data encrypted * @param key * @return * @throws Exception */ public static byte[] sign(String message, PrivateKey key) throws Exception { return SystemUtil.sign(message.getBytes(SystemUtil.CHARSET_ISO88591), key); } /** * use private key to generate digit sign * * @param data * data encrypted * @param privateKeyStr * private key * * @return * @throws Exception */ public static byte[] sign(byte[] data, String privateKeyStr) throws Exception { PrivateKey priKey = SystemUtil.convert2PrivateKey(privateKeyStr); return SystemUtil.sign(data, priKey); } /** * * @param message * data encrypted * @param privateKeyStr * hex(16) string * @return * @throws Exception */ public static byte[] sign(String message, String privateKeyStr) throws Exception { return sign(toBytes(message), privateKeyStr); } /** * use public key verify sign * * @param message * @param signStr * @return * @throws Exception */ public static boolean verifySign(byte[] message, byte[] signBytes, PublicKey key) throws Exception { if (message == null || signBytes == null || key == null) { return false; } Signature signetcheck = Signature .getInstance(SystemUtil.SIGNATURE_ALGORITHM); signetcheck.initVerify(key); signetcheck.update(message); return signetcheck.verify(signBytes); } public static boolean verifySign(byte[] message, String signStr, PublicKey key) throws Exception { byte[] signBytes = toBytes(signStr); return verifySign(message, signBytes, key); } /** * use public key verify sign * * @algorithm : RSA * @param message * data encrypted * @param signStr * @return * @throws Exception */ public static boolean verifySign(String message, String signStr, PublicKey key) throws Exception { return SystemUtil.verifySign( message.getBytes(SystemUtil.CHARSET_ISO88591), toBytes(signStr), key); } /** * verify digit sign * * @param data * date encrypted * @param publicKeyStr * @param sign * * @return success:true ;fail:false * @throws Exception * */ public static boolean verify(byte[] data, String publicKeyStr, String sign) throws Exception { // get public key PublicKey pubKey = SystemUtil.convert2PublicKey(publicKeyStr); return SystemUtil.verifySign(data, sign, pubKey); } /*** * convert hex(16) bit string to byte array * * @param sHex * : hex(16) bit string * @return byte[] */ public static final byte[] toBytes(String sHex) { int length = sHex.length(); if (length % 2 != 0) { String message = "Hex bit string length must be even"; System.err.println(message); throw new RuntimeException(message); } byte[] bytes; bytes = new byte[sHex.length() / 2]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) Integer.parseInt( sHex.substring(2 * i, 2 * i + 2), 16); } return bytes; } /*** * convert byte array to hex(16) bit string * * @param byte[] * @return hex(16) bit string */ public static String toHexString(byte[] b) { StringBuilder sb = new StringBuilder(b.length * 2); for (int i = 0; i < b.length; i++) { sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]); sb.append(HEXCHAR[b[i] & 0x0f]); } return sb.toString(); } /** * * @param byte[] * @return * @throws UnsupportedEncodingException */ public static String toString(byte[] b) throws UnsupportedEncodingException { return new String(b, CHARSET_ISO88591); } /** * SHA encrypt * * @param data * @return byte[] * @throws Exception */ public static byte[] encryptSHA(byte[] data) throws Exception { MessageDigest sha = MessageDigest.getInstance(SystemUtil.KEY_SHA); sha.update(data); return sha.digest(); } /*** * * @param data * @return * @throws Exception */ public static byte[] encryptSHA(String data) throws Exception { return encryptSHA(data.getBytes(SystemUtil.CHARSET_ISO88591)); } /*** * sha-1 * * @param data * byte[] * @return * @throws Exception */ public static byte[] encryptSHA1(byte[] data) throws Exception { MessageDigest sha = MessageDigest.getInstance(SystemUtil.KEY_SHA1); sha.update(data); return sha.digest(); } /*** * sha-1 * * @param data * :String * @return * @throws Exception */ public static byte[] encryptSHA1(String data) throws Exception { return encryptSHA1(data.getBytes(SystemUtil.CHARSET_ISO88591)); } /*** * * @param secretKey * @param input * @param algorithm * @return byte[] * @throws Exception */ public static byte[] getHMAC(byte[] secretKey, byte[] input, String algorithm) throws Exception { Mac mac = Mac.getInstance(algorithm); // get the bytes of the hmac key and data string SecretKey secret = new SecretKeySpec(secretKey, algorithm); mac.init(secret); // 对input 进行HMAC 加密 byte[] bytesF1 = mac.doFinal(input); return bytesF1; } /*** * HMACSHA256 * * @param secretKey * @param input * @return * @throws Exception */ public static byte[] getHMAC_SHA256(byte[] secretKey, byte[] input) throws Exception { return getHMAC(secretKey, input, SystemUtil.KEY_HMAC_SHA256); } /*** * HmacSHA1 * * @param secretKey * @param input * @return * @throws Exception */ public static byte[] getHMAC_SHA1(byte[] secretKey, byte[] input) throws Exception { return getHMAC(secretKey, input, SystemUtil.KEY_HMAC_SHA1); } /*** * * @param secretKey * : hex bit string * @param input * : hex bit string * @return byte array * @throws Exception */ public static byte[] getHMAC_SHA1(String secretKey, String input) throws Exception { return getHMAC_SHA1(SystemUtil.toBytes(secretKey), SystemUtil.toBytes(input)); } /*** * * @param keyInfo * @return * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException */ public static KeyPair getKeyPair(String keyInfo) throws NoSuchAlgorithmException, UnsupportedEncodingException { return getKeyPair(keyInfo.getBytes(SystemUtil.CHARSET_ISO88591)); } /*** * * @param keyInfo * @return * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException */ public static KeyPair getKeyPair(byte[] keyInfo) throws NoSuchAlgorithmException, UnsupportedEncodingException { KeyPairGenerator keygen = KeyPairGenerator .getInstance(SystemUtil.KEY_ALGORITHM_RSA); SecureRandom random = new SecureRandom(); random.setSeed(keyInfo); // 初始加密。长度为512,必须是大于512才干够的 keygen.initialize(512, random); // 取得密钥对 KeyPair kp = keygen.generateKeyPair(); return kp; } /*** * * @param modulus * :N * @param privateExponent * :D * @return * @throws Exception */ public static PrivateKey getPrivateKey(String modulus, String privateExponent) throws Exception { BigInteger m = new BigInteger(modulus); BigInteger D = new BigInteger(privateExponent); RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, D); KeyFactory keyFactory = KeyFactory .getInstance(SystemUtil.KEY_ALGORITHM_RSA); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } /*** * * @param m * : modulus * @param d * :private exponent * @return * @throws Exception */ public static PrivateKey getPrivateKey(BigInteger m, BigInteger d) throws Exception { RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, d); KeyFactory keyFactory = KeyFactory .getInstance(SystemUtil.KEY_ALGORITHM_RSA); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } // public static PrivateKey getPrivateKey(byte[] N_hex, byte[] D_hex){ // return SystemUtil.getPrivateKey(new BigInteger(N_hex), new // BigInteger(D_hex)); // } /*** * * @param m * @param privateExponent * :D * @return * @throws Exception */ public static PrivateKey getPrivateKey(BigInteger m, byte[] privateExponent)// TODO throws Exception { BigInteger d = new BigInteger(privateExponent); return getPrivateKey(m, d.negate()); } public static PrivateKey getPrivateKey(byte[] m, byte[] privateExponent) throws Exception { return getPrivateKey(SystemUtil.getBigIntegerByByteArr(m), SystemUtil.getBigIntegerByByteArr(privateExponent)); } /*** * OK * * @param publicKey * @param priKey * @return * @throws Exception */ public static boolean verifyPrivPubKey(PublicKey publicKey, PrivateKey priKey) throws Exception { String message = "3234"; RSAPublicKey rsaPublKey = (RSAPublicKey) publicKey; RSAPrivateKey rsaPriKey = (RSAPrivateKey) priKey; byte[] encryptBytes = SystemUtil.encrypt(message, publicKey); byte[] decryptBytes = SystemUtil.decrypt(encryptBytes, priKey); return new String(decryptBytes, SystemUtil.CHARSET_ISO88591) .equals(message) && rsaPriKey.getModulus().equals(rsaPublKey.getModulus()); } /*** * OK * * @param m * :modulus * @param e * :public key publicExponent * @param d * :private exponent * @return * @throws Exception */ public static boolean verifyPrivPubKey(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent) throws Exception { PublicKey pubKey = getPublicKey(modulus, publicExponent); PrivateKey priKey = getPrivateKey(modulus, privateExponent); return SystemUtil.verifyPrivPubKey(pubKey, priKey); } public static boolean verifyPrivPubKey(String modulus_decimal, String publicExponent_decimal, String privateExponent_decimal) throws Exception { BigInteger modulus = new BigInteger(modulus_decimal); BigInteger publicExponent = new BigInteger(publicExponent_decimal); BigInteger privateExponent = new BigInteger(privateExponent_decimal); return verifyPrivPubKey(modulus, publicExponent, privateExponent); } /*** * convert byte array to BigInteger * * @param bytes * @return */ public static BigInteger getBigIntegerByByteArr(byte[] bytes) { return new BigInteger(SystemUtil.toHexString(bytes), 16); } /*** * convert BigInteger to byte array * * @param bi * @return */ public static byte[] getBytesByBigInteger(BigInteger bi) { String hexString = bi.toString(16); return SystemUtil.toBytes(hexString); } /*** * get prime number * * @param base * @return */ // public static int generatePrime(int base) { // for (int i = base;; i++) { // if (isPrime(i)) { // return i; // } // } // } public static BigInteger generatePrime(int base) { return generatePrime(BigInteger.valueOf(base)); } /*** * get prime number which >=base * * @param base * @return BigInteger */ public static BigInteger generatePrime(BigInteger base) { BigInteger bigInt1 = BigInteger.valueOf(1l); for (BigInteger i = base;; i = i.add(bigInt1)) { if (isPrime(i)) { return i; } } } /*** * whether is a prime number * * @param num * @return */ public static boolean isPrime(int num) { return isPrime(BigInteger.valueOf(num)); // boolean isPrime = true; // for (int i = 2; i <= num / 2; i++) { // if (num % i == 0) { // isPrime = false; // break; // } // } // return isPrime; } /*** * whether is a prime number * * @param num * @return */ public static boolean isPrime(BigInteger num) { boolean isPrime = true; BigInteger bigIntTwo = BigInteger.valueOf(2l); BigInteger bigIntOne = BigInteger.valueOf(1l); for (BigInteger i = bigIntTwo; num.divide(bigIntTwo).compareTo(i) >= 0; i = i .add(bigIntOne)) { if (num.mod(i).intValue() == 0) { isPrime = false; break; } } return isPrime; } /*** * * @param ta * @param isAdd */ public static void addSubduction(JTextArea ta, boolean isAdd) { String argument1 = ta.getText(); BigInteger bigIntArg1 = new BigInteger(argument1); if (isAdd) { ta.setText(bigIntArg1.add(bigInt1).toString()); } else { ta.setText(bigIntArg1.subtract(bigInt1).toString()); } } /*** * * @param ta * @return */ public static BigInteger getBigInteger(JTextArea ta) { String data = ta.getText(); return new BigInteger(data); } /*** * * @param arg1 * @param arg2 * @return */ public static BigInteger mod(BigInteger arg1, BigInteger arg2) { return arg1.mod(arg2); } /*** * * @param ta1 * @param ta2 * @return */ public static BigInteger mod(JTextArea ta1, JTextArea ta2) { BigInteger arg1 = new BigInteger(ta1.getText()); BigInteger arg2 = new BigInteger(ta2.getText()); return mod(arg1, arg2); } /*** * * @param ta1 * @param ta2 * @return */ public static BigInteger mod(JTextField ta1, JTextField ta2) { BigInteger arg1 = new BigInteger(ta1.getText()); BigInteger arg2 = new BigInteger(ta2.getText()); return mod(arg1, arg2); } /*** * convert int to hex string * * @param bigInt * @return */ public static String toHexString(BigInteger bigInt) { return bigInt.toString(16); } /*** * * @param ta * @return */ public static String toHexString(JTextArea ta) { BigInteger arg1 = new BigInteger(ta.getText()); return toHexString(arg1); } /*** * encode by Base64 */ public static String encodeBase64(byte[] input) throws Exception { Class clazz = Class .forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64"); Method mainMethod = clazz.getMethod("encode", byte[].class); mainMethod.setAccessible(true); Object retObj = mainMethod.invoke(null, new Object[] { input }); return (String) retObj; } /*** * decode by Base64 */ public static byte[] decodeBase64(String input) throws Exception { Class clazz = Class .forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64"); Method mainMethod = clazz.getMethod("decode", String.class); mainMethod.setAccessible(true); Object retObj = mainMethod.invoke(null, input); return (byte[]) retObj; } /** * 编码 * * @param bstr * @return String */ public static String encode(byte[] bstr) { return new sun.misc.BASE64Encoder().encode(bstr); } /** * 解码 * * @param str * @return string */ public static byte[] decode(String str) { byte[] bt = null; try { sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); bt = decoder.decodeBuffer(str); } catch (IOException e) { e.printStackTrace(); } return bt; } /*** * 获取实际的子类的class * * @param clz * @return */ public static <T> Class<T> getGenricClassType( @SuppressWarnings("rawtypes") Class clz) { Type type = clz.getGenericSuperclass(); if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; Type[] types = pt.getActualTypeArguments(); if (types.length > 0 && types[0] instanceof Class) { return (Class) types[0]; } } return (Class) Object.class; } // public static byte[] decodeBufferBASE64Decoder(String data) { // // try { // Class clazz = Class.forName("sun.misc.BASE64Decoder"); // Method mainMethod; // mainMethod = clazz.getMethod("decodeBuffer", String.class); // mainMethod.setAccessible(true); // Object retObj = mainMethod.invoke(clazz.newInstance(), data); // return (byte[]) retObj; // } catch (SecurityException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (NoSuchMethodException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IllegalArgumentException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IllegalAccessException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (InvocationTargetException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (InstantiationException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (ClassNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // return null; // // } // // public static String encodeBASE64Encoder(byte[] bt) { // // try { // Class clazz = Class.forName("sun.misc.BASE64Decoder"); // Method mainMethod; // mainMethod = clazz.getMethod("encode", byte[].class); // mainMethod.setAccessible(true); // Object retObj = mainMethod.invoke(clazz.newInstance(), bt); // return (String) retObj; // } catch (SecurityException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (NoSuchMethodException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IllegalArgumentException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IllegalAccessException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (InvocationTargetException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (InstantiationException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (ClassNotFoundException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // return null; // // } /*** * print byte array * * @param bytes * @param isNeedPlus * : Whether to add a plus sign * @return such as * "[ 52, 116, -18, 34, 70, -43, 56, -60, 17, -67, -52, -97 ] ;length:16" */ public static String printBytes(byte[] bytes, boolean isNeedPlus) { StringBuffer sb = new StringBuffer("[ "); for (int i = 0; i < bytes.length; i++) { if (bytes[i] > 0 && isNeedPlus) { sb.append("+" + String.valueOf(bytes[i])); } else { sb.append(bytes[i]); } if (i < bytes.length - 1) { sb.append(", "); } } sb.append(" ]").append(" ;length:" + bytes.length); return sb.toString(); } /*** * Format a byte array * * @param bytes * @return */ public static String formatBytes(byte[] bytes) { return printBytes(bytes, false); } /*** * Format a byte array * * @param hex * @return */ public static String formatBytes(String hex) { return formatBytes(SystemUtil.toBytes(hex)); } /*** * * @param bytes */ public static void printBytes(byte[] bytes) { System.out.println(formatBytes(bytes)); } /*** * * @param hex */ public static void printBytes(String hex) { System.out.println(formatBytes(hex)); } /*** * 合并字节数组 * * @param a * @return */ public static byte[] mergeArray(byte[]... a) { // 合并完之后数组的总长度 int index = 0; int sum = 0; for (int i = 0; i < a.length; i++) { sum = sum + a[i].length; } byte[] result = new byte[sum]; for (int i = 0; i < a.length; i++) { int lengthOne = a[i].length; if (lengthOne == 0) { continue; } // 拷贝数组 System.arraycopy(a[i], 0, result, index, lengthOne); index = index + lengthOne; } return result; } /*** * merge two int array to a string * * @param a * @param b * @return */ public static String merge(int[] a, int[] b) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < a.length; i++) { sb.append(a[i]); sb.append(","); } for (int i = 0; i < b.length; i++) { sb.append(b[i]); sb.append(","); } int leng_str = sb.toString().length(); return sb.substring(0, leng_str - 1); } /*** * Get top <code>frontNum</code> bytes * * @param source * @param frontNum * @return */ public static byte[] getFrontBytes(byte[] source, int frontNum) { byte[] frontBytes = new byte[frontNum]; System.arraycopy(source, 0, frontBytes, 0, frontNum); return frontBytes; } public static byte[] getAfterBytes(byte[] source, int afterNum) { int length = source.length; byte[] afterBytes = new byte[afterNum]; System.arraycopy(source, length - afterNum, afterBytes, 0, afterNum); return afterBytes; } /*** * * @param frontNum * @param source * @return */ public static byte[] filterFrontBytes(int frontNum, byte[] source) { return copyByte(frontNum, source.length - frontNum, source); } public static byte[] copyByte(int start, int length, byte[] source) { byte[] des = new byte[length]; System.arraycopy(source, start, des, 0, length); return des; } public static boolean arrayIsEqual(byte[] a, byte[] b) { if (a != null && b != null) { if (a.length != b.length) { return false; } else { for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) { return false; } } } } return true; } /*** * Delete the slash which is in front of input * * @param input * @return */ public static String deleteFrontSlash(String input) { String result = input.replaceAll("^/", ""); return result; } /*** * Delete the brackets * * @param input * @return */ public static String deletebrackets(String input) { input = input.replaceAll("\[?(.*)\]?", "$1"); return input; } /*** * Delete the curly braces ({ }) * * @param input * @return */ public static String deleteCurlyBraces(String input) { input = input.replaceAll("\{?(.*)\}", "$1"); return input; } public static String deleteSingleQuotes(String input) { input = input.replaceAll("'?

    (.*)'", "$1"); return input; } /*** * 以斜杠和?

    切割,获取最后一个 / ? input * :http://localhost:8081/SSLServer/addUser.security?

    a=b * result:addUser.security */ public static String getSerlvetNameByQuestionMark(String url) { String input = null; input = url.replaceAll(".*/([\w\.]*)(\?.*)?$", "$1"); return input; } /*** * input :http://localhost:8081/SSLServer/addUser.security?a=b * result:addUser */ public static String getSerlvetName(String url) { String input = null; input = url.replaceAll(".*/([\w\.]*)(\..*)$", "$1"); if (input.contains("?

    ")) { input = getSerlvetNameByQuestionMark(input); } return input; } /*** * get port * * @param url * such as http://localhost:8081/SSLServer/addUser.A?

    a=b * @return 8081 * */ public static String getHttpPort(String url) { String input = url.replaceAll("^.+:([\d]+)/.*$", "$1"); return input; } /*** * * * @param url * such as localhost/SSLServer/addUser.A?

    a=b * @return SSLServer */ public static String getProjectName(String url) { String input = url.replaceAll("^.+(:[\d]+)?/(.*)/.*$", "$2"); return input; } /*** * get Http request ip * * @param url * @return */ public static String getHttpIp(String url) { String input = url.replaceAll("^(.*://)?([^/:]*)(:[\d]+)?

    /.*$", "$2"); return input; } /*** * be similar to grep in linux os * * @param keyWord * @param input * :List * @return */ public static List<String> grepStr(String keyWord, String input) { String regex = ".*" + keyWord + ".*"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); List<String> grepResult = new ArrayList<String>(); if (m.find()) { grepResult.add(m.group()); } return grepResult; } /**** * old:java.lang.String ; result: String * * @param input * @return */ public static String getLastNameByPeriod(String input) { input = input.replaceAll("^.*\.([\w]+)", "$1"); return input; } /*** * * @param input * :2013-06-15 * @return */ public static boolean isDate(String input) { String regex = "[\d]{4}-[\d]{1,2}-[\d]{1,2}"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(input); return m.matches(); } public static String grepSimple(String keyWord, String input) { List<String> grepResult = grepStr(keyWord, input); if (grepResult.size() > 0) { return grepResult.get(0); } else { return null; } } public static int indexOf(String hexStr, String keyWord) { return hexStr.indexOf(keyWord.toLowerCase()) / 2; } public static int indexOf(byte[] bytes, String keyWord) { return indexOf(SystemUtil.toHexString(bytes), keyWord.toLowerCase()); } public static int indexOf(byte[] bytes, byte[] keyWord) { return indexOf(SystemUtil.toHexString(bytes), SystemUtil.toHexString(keyWord).toLowerCase()); } /** * * The number of occurrences of find keyword in srcText * * @param srcText * @param keyword * @return */ public static int findStr1(String srcText, String keyword) { int count = 0; int leng = srcText.length(); int j = 0; for (int i = 0; i < leng; i++) { if (srcText.charAt(i) == keyword.charAt(j)) { j++; if (j == keyword.length()) { count++; j = 0; } } else { i = i - j;// should rollback when not match j = 0; } } return count; } public static int findStr2(String srcText, String keyword) { int count = 0; Pattern p = Pattern.compile(keyword); Matcher m = p.matcher(srcText); while (m.find()) { count++; } return count; } public static int findStr3(String srcText, String keyword) { return findStr(srcText, keyword, 0); } public static int findStr(String srcText, String keyWord, int pos) { int i, j, k = 0; i = pos; j = 0; while (i < srcText.length() && j < keyWord.length()) { if (srcText.charAt(i) == keyWord.charAt(j)) { ++i; ++j; if (j == keyWord.length()) { k = k + 1;// k++ j = 0; } } else { i = i - j + 1; j = 0; } } return k; } /*** * * @param source * @param findTarget * :key word * @param pos * :where start from * @return index */ public static int findBytes(byte[] source, byte[] findTarget, int pos) { int i, j, k = 0; i = pos; j = 0; while (i < source.length && j < findTarget.length) { if (source[i] == findTarget[j]) { ++i; ++j; if (j == findTarget.length) { k = k + 1;// k++ break; // j = 0; } } else { i = i - j + 1; j = 0; } } return k == 0 ?

    -1 : i - j; } /*** * start from 0 * * @param source * @param findTarget * @return */ public static int findBytes(byte[] source, byte[] findTarget) { return findBytes(source, findTarget, 0); } // / <summary> // / 推断两个byte[]包括的值是否相等 // / </summary> // / <param name="bytes1">byte[] 1</param> // / <param name="bytes2">byte[] 2</param> // / <returns>相等返回True,反之False</returns> public static boolean isEqualBytes(byte[] bytes1, byte[] bytes2) { // 比較长度是否一样 if (bytes1.length != bytes2.length) { return false; } // 比較成员是否相应相等 for (int i = 0; i < bytes1.length; i++) { if (bytes1[i] != bytes2[i]) { return false; } } return true; } /*** * compare tow byte[] * * @param bytes1 * @param bytes2 * @return */ public static boolean isEquals(byte[] bytes1, byte[] bytes2) { if (bytes1 == bytes2 && bytes1 == null) { return true; } else { if (bytes1.length != bytes2.length) { return false; } else { int index = findBytes(bytes1, bytes2, 0); if (index == -1) { return false; } else { return true; } } } } public static boolean isEqualChars(char[] chars1, char[] chars2) { // 比較长度是否一样 if (chars1.length != chars2.length) { return false; } // 比較成员是否相应相等 for (int i = 0; i < chars1.length; i++) { if (chars1[i] != chars2[i]) { return false; } } return true; } /*** * // * D:xxxeclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core * mp0wtpwebappsshop_goodsimages // * D:xxxeclipseworkspaceshop_goods upload * * @param realPath2 * @param projectName * @return */ public static String getRealPath(String realPath2, String projectName) { String realpath = realPath2.replaceAll(".metadata.*(" + projectName + ")", "$1"); return realpath; } /*** * java web // * D:xxxeclipseworkspace.metadata.pluginsorg.eclipse.wst.server * .core mp0wtpwebappsshop_goodsimages // * D:xxxeclipseworkspaceshop_goods upload * * @param uploadFolderName * @param projectName * @param sContext * @return */ public static String getUploadedPath(String uploadFolderName, String projectName, ServletContext sContext) { String uploadFolder_tmp = null; if (uploadFolderName.startsWith("/")) { uploadFolder_tmp = uploadFolderName; } else { uploadFolder_tmp = "/" + uploadFolderName;// "/upload" } String realpath = sContext.getRealPath(uploadFolder_tmp); // project name ,eg.shop_goods projectName = SystemUtil.deleteFrontSlash(projectName); realpath = SystemUtil.getRealPath(realpath, projectName); return realpath; } /*** * download file * * @param response * @param downloadFilePath * @param filename * @throws IOException */ public static void downloadFile(HttpServletResponse response, File downloadFilePath, String filename) throws IOException { FileInputStream fin = new FileInputStream(downloadFilePath); // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(fin); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header if (filename == null) { filename = downloadFilePath.getName(); } response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); response.addHeader("Content-Length", "" + downloadFilePath.length()); OutputStream toClient = new BufferedOutputStream( response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } /*** * download file * * @param response * @param downloadFilePath * @throws IOException */ public static void downloadFile(HttpServletResponse response, File downloadFilePath) throws IOException { downloadFile(response, downloadFilePath, null); } /*** * convert List to String[] * * @param list * @return */ public static String[] list2Array(List<String> list) { return list.toArray(new String[list.size()]); } /*** * print per * * @param strs */ public static void printArray(Object[] strs, boolean isNewLine, String delimiter) { List<Object> list = Arrays.asList(strs); printList(list, isNewLine, delimiter); // for(int i=0;i<strs.length;i++){ // System.out.println(strs[i]); // } } public static void printArray(int[] ints) { for (int i = 0; i < ints.length; i++) { System.out.print(ints[i]); if (i < ints.length - 1) { System.out.print(" ,"); } } System.out.println(); } /*** * Print two-dimensional array 0_0 0_1 0_2 0_3 0_4 1_0 1_1 1_2 1_3 1_4 * * @param arrays */ public static void printArrays(Object[][] arrays, String delimiter) { for (int i = 0; i < arrays.length; i++) { Object[] objs = arrays[i]; if (objs != null && objs.length > 0) { printArray(objs, false, delimiter); System.out.println(); } } } /*** * convert boolean to string * * @param bool * @return */ public static String getBooleanString(boolean bool) { return String.valueOf(bool); } /*** * 过滤数组中的空元素 * * @see read-write-excel项目中的 * com.kunlunsoft.excel.util.ExcelArraysUtil.public Object[][] * getArrays4excel(Sheet sheet, int[] location, boolean * isFilterEmptyRow) * @param arrays * @return */ public static Object[][] filterEmpty(Object[][] arrays) { int sumNotNull = 0; /*** * 统计非空元素的总个数 */ for (int i = 0; i < arrays.length; i++) { Object object = arrays[i]; if (!ValueWidget.isNullOrEmpty(object) && !SystemUtil.isNullOrEmpty((Object[]) object)) {// 推断元素是否为空 sumNotNull = sumNotNull + 1; } } Object[][] filtedObjs = new Object[sumNotNull][]; int index = 0; for (int i = 0; i < arrays.length; i++) { Object[] object_tmp = arrays[i]; if (!ValueWidget.isNullOrEmpty(object_tmp) && !SystemUtil.isNullOrEmpty((Object[]) object_tmp)) {// 推断元素是否为空 filtedObjs[index++] = object_tmp; } } return filtedObjs; } /*** * [, , ]-->true * * @param objs * @return */ public static boolean isNullOrEmpty(Object[] objs) { if (objs == null || objs.length == 0) { return true; } else { boolean isEmpty = true; for (int i = 0; i < objs.length; i++) { Object object = objs[i]; if (!ValueWidget.isNullOrEmpty(object)) { isEmpty = false; break; } } return isEmpty; } } /*** * [, , ]-->true * * @param objs * @return */ public static boolean isNullOrEmpty(String[] objs) { if (objs == null || objs.length == 0) { return true; } else { boolean isEmpty = true; for (int i = 0; i < objs.length; i++) { Object object = objs[i]; if (!ValueWidget.isNullOrEmpty(object)) { isEmpty = false; break; } } return isEmpty; } } public static int parseObj(Object obj) { if (null == obj) { return NEGATIVE_ONE; } return Integer.parseInt(obj.toString()); } /*** * * @param request * :HttpServletRequest * @param attName * :parameter name * @return */ public static int parseIntFromRequest(HttpServletRequest request, String parameterName) { String parameterValue = request.getParameter(parameterName); return parseObj(parameterValue); } /*** * Get all selected(checked) checkboxes * * @param request * @param sumAttribute * : The total number of the checkbox * @param prefix * : * @return */ public static List<String> getSelectedCheckbox(HttpServletRequest request, String sumAttribute, String prefix) { List<String> selectResults = new ArrayList<String>(); int question34Sum = parseObj(request.getParameter(sumAttribute)); for (int i = 0; i < question34Sum; i++) { String selectResult = request.getParameter(prefix + (i + 1)); if (null != selectResult) { selectResults.add(selectResult); } } return selectResults; } /*** * Join all Strings in the Array into a Single String separator needs * specifying * * @param strs * @param separator * @return */ public static String arrToString(String[] strs, String separator) { return StringUtils.join(strs, separator); } /*** * Join all Strings in the Array into a Single String separator is , * * @param strs * @return */ public static String arrToString(String[] strs) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < strs.length; i++) { String str = strs[i]; if (ValueWidget.isHasValue(str)) { sb.append(str); } if (i < strs.length - 1) { sb.append(","); } } return sb.toString(); } /** * 产生无反复的随机数 sumInt:总样本 (0....sumInt-1) resultSum: 产生的随机数个数 * * @return */ public static int[] randoms(int sumInt, int resultSum) { // Total sample int send[] = new int[sumInt];// 0....(sumInt-1) for (int i = 0; i < sumInt; i++) { send[i] = i; } return randoms(send, resultSum); } /*** * * @param totalSample * :total sample * @param resultSum * :Specified number * @return */ public static int[] randoms(int[] totalSample, int resultSum) { int temp1, temp2; Random r = new Random(); int len = totalSample.length;// The length of the total sample int returnValue[] = new int[resultSum];// Random number to return for (int i = 0; i < resultSum; i++) { // temp1 = Math.abs(r.nextInt()) % len; temp1 = r.nextInt(len);// between 0 (inclusive) and the specified // value (exclusive) temp2 = totalSample[temp1]; returnValue[i] = temp2; if (temp1 != len - 1) { totalSample[temp1] = totalSample[len - 1]; totalSample[len - 1] = temp2; } len--; } return returnValue; } /*** * whether j is involved in <code>int[] intArray</code> * * @param intArray * @param j * @return */ public static boolean isContains(int[] intArray, int j) { boolean initBool = false; for (int i : intArray) { if (i == j) { initBool = true; break; } } return initBool; } public static boolean isContains2(int[] intArray, int j) { return isContains(intArray, j, 2); } /*** * Match the specified number(times) * * @param intArray * @param j * @param time * @return */ public static boolean isContains(int[] intArray, int j, int time) { boolean initBool = false; int count = 0; for (int i : intArray) { if (i == j) { count++; } } if (count == time) { return true; } return initBool; } /*** * Filter out the elements of the same * * @param intArray * @return */ public static boolean uniqueInt(int[] intArray) { boolean initBool = true; for (int j : intArray) { if (!isContains(intArray, j, 1)) { initBool = false; break; } } return initBool; } public static boolean isEquals(List<String> aa, List<String> bb) { if (null == aa || null == bb) return false; boolean isEqual = true; if (aa.size() == bb.size()) { for (int i = 0; i < aa.size(); i++) { String aaStr = aa.get(i); String bbStr = bb.get(i); if (!aaStr.equals(bbStr)) { isEqual = false; break; } } } else { return false; } return isEqual; } /*** * generate a random string,eg 13_07_03_17_16_03_8 * * @return */ public static String getRandomStr() { Random rand = new Random(); return TimeHWUtil.formatTimestamp2(new Date()) + rand.nextInt(10); } /*** * * @param input * : 0 or 1 or false or true * @return true:1,true ; false:0,false */ public static boolean parse33(String input) { boolean result = false; if (input.equals("0") || input.equals("1")) { int resultint = Integer.parseInt(input); result = (resultint == 1); } else { result = Boolean.parseBoolean(input); } return result; } /*** * reverse map Note : value in oldMap must be unique. rever * * @param oldMap * @return */ public static Map reverseMap(Map oldMap) { Map newMap = new HashMap<Object, Object>(); for (Iterator it = oldMap.entrySet().iterator(); it.hasNext();) { Map.Entry<Object, String> entry = (Map.Entry<Object, String>) it .next(); newMap.put(entry.getValue(), entry.getKey()); } return newMap; } /*** * * @param arrays * @param columnIndex * : start from one * @return */ public static Object[] getProjection(Object[][] arrays, int columnIndex) { int length = arrays.length; Object[] objs = new Object[length]; for (int i = 0; i < length; i++) { if (arrays[i] == null) { objs[i] = null; } else { objs[i] = arrays[i][columnIndex - 1]; } } return objs; } /*** * convert request query string to map * * @param queryString * @return */ public static Map<String, Object> parseQueryString(String queryString) { Map<String, Object> argMap = new HashMap<String, Object>(); String[] queryArr = queryString.split("&"); for (int i = 0; i < queryArr.length; i++) { String string = queryArr[i]; String keyAndValue[] = string.split("="); if (keyAndValue.length != 2) { argMap.put(keyAndValue[0], EMPTY); } else { argMap.put(keyAndValue[0], keyAndValue[1]); } } return argMap; } /*** * convert Map<String, Object> to Map<String, String> * * @param oldMap * @return */ public static Map<String, String> convertMap(Map<String, Object> oldMap) { Map<String, String> newMap = new HashMap<String, String>(); for (Iterator it = oldMap.entrySet().iterator(); it.hasNext();) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it .next(); newMap.put(entry.getKey(), (String) entry.getValue()); } return newMap; } /*** * Get request query string, form method : post * * @param request * @return byte[] */ public static byte[] getRequestPostBytes(HttpServletRequest request) { int contentLength = request.getContentLength(); byte buffer[] = new byte[contentLength]; for (int i = 0; i < contentLength;) { try { int readlen = request.getInputStream().read(buffer, i, contentLength - i); if (readlen == -1) { break; } i += readlen; } catch (IOException ioexception) { ioexception.printStackTrace(); } finally { // logger.info("Json Request:" + requestPacket); } } return buffer; } /*** * Get request query string, form method : post * * @param request * @return * @throws UnsupportedEncodingException */ public static String getRequestPostStr(HttpServletRequest request) throws UnsupportedEncodingException { byte buffer[] = getRequestPostBytes(request); String charEncoding = request.getCharacterEncoding(); if (charEncoding == null) { charEncoding = "UTF-8"; } return new String(buffer, charEncoding); } /*** * Compatible with GET and POST * * @param request * @return : <code>byte[]</code> * @throws UnsupportedEncodingException */ public static byte[] getRequestQuery(HttpServletRequest request) throws UnsupportedEncodingException { String submitMehtod = request.getMethod(); String queryString = null; if (submitMehtod.equals("GET")) {// GET queryString = request.getQueryString(); String charEncoding = request.getCharacterEncoding();// charset if (charEncoding == null) { charEncoding = "UTF-8"; } return queryString.getBytes(charEncoding); } else {// POST return getRequestPostBytes(request); } } /** * Compatible with GET and POST * * @param request * @return : <code>String</code> * @throws UnsupportedEncodingException */ public static String getRequestQueryStr(HttpServletRequest request, String charEncoding) throws UnsupportedEncodingException { String submitMehtod = request.getMethod(); if (submitMehtod.equalsIgnoreCase("post")) { byte[] bytes = getRequestPostBytes(request); String charEncoding2 = request.getCharacterEncoding();// charset System.out.println("[getRequestQueryStr]charEncoding:" + charEncoding2); System.out.println("[getRequestQueryStr]Content-Type:" + request.getHeader("Content-Type")); System.out.println("[getRequestQueryStr]contentType:" + request.getHeader("contentType")); // if (charEncoding == null) { // charEncoding = "GBK"; // } return new String(bytes, charEncoding); } else {// form method :Get return request.getQueryString(); } } /*** * Send http request test ok * * @param response * @param bytes * :字节数组 * @param contentType * :if is null,default value is "application/json" * @param encoding * : 编码方式 * @throws IOException */ public static void sendRequestWriter(HttpServletResponse response, byte[] bytes, String contentType, String encoding) throws IOException { response.setContentLength(bytes.length); if (contentType == null) { contentType = CONTENTTYPE_JSON; } response.setContentType(contentType); PrintWriter printer = response.getWriter(); printer.println(new String(bytes, encoding)); printer.flush(); printer.close(); } /*** * * @param response * @param sendData * :<code>String</code> * @param contentType * @param encoding * : such as GBK/utf-8 * @throws IOException */ public static void sendRequestWriter(HttpServletResponse response, String sendData, String contentType, String encoding) throws IOException { // response.setContentLength(sendData.getBytes(encoding).length); if(encoding==null){ System.out.println("encoding/charset can not be null."); return; } byte[] bytes = sendData.getBytes(encoding); sendRequestWriter(response, bytes, contentType, encoding); } /*** * test ok * * @param response * @param bytes * @param contentType : default value:application/json * @param encoding * @throws IOException */ public static void sendRequestStream(HttpServletResponse response, byte[] bytes, String contentType, String responseEncoding) throws IOException { response.setContentLength(bytes.length); if (contentType == null) { contentType = CONTENTTYPE_JSON; } response.setContentType(contentType); if (responseEncoding != null) { response.setCharacterEncoding(responseEncoding); } ServletOutputStream sos = response.getOutputStream(); sos.write(bytes, 0, bytes.length); sos.flush(); sos.close(); } /*** * * @param response * @param bytes * @throws IOException */ public static void sendRequest(HttpServletResponse response, byte[] bytes) throws IOException { SystemUtil.sendRequestStream(response, bytes, null, response.getCharacterEncoding()); } }


    package com.common.util;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipException;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;
    
    import com.string.widget.util.RandomUtils;
    
    /**
     * Description User: I8800
     */
    public class ZipUtil {
    
    	/**
    	 * 获得zip压缩包中文件数(包含文件夹)
    	 * 
    	 * @param zipFile
    	 * @return
    	 */
    	public static int getFiles(final ZipFile zipFile) {
    		return zipFile.size();
    	}
    
    	/**
    	 * 获得zip压缩包二进制数组中文件数(包含文件夹)
    	 * 
    	 * @param zipBytes
    	 * @return
    	 * @throws IOException
    	 */
    	public static int getFiles(final byte[] zipBytes) throws IOException {
    		int files = 0;
    		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
    				zipBytes);
    		ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream);
    		while (zipInputStream.getNextEntry() != null) {
    			files++;
    		}
    		zipInputStream.closeEntry();
    		zipInputStream.close();
    		byteArrayInputStream.close();
    		return files;
    	}
    
    	/**
    	 * 获得zip压缩包中文件数(包含文件夹)
    	 * 
    	 * @param filename
    	 * @return
    	 * @throws IOException
    	 */
    	public static int getFiles(final String filename) throws IOException {
    		ZipFile zipFile = new ZipFile(filename);
    		int files = getFiles(zipFile);
    		zipFile.close();
    		return files;
    	}
    
    	/**
    	 * 获得zip压缩包中文件数(包含文件夹)
    	 * 
    	 * @param file
    	 * @return
    	 * @throws IOException
    	 */
    	public static int getFiles(final File file) throws IOException {
    		ZipFile zipFile = new ZipFile(file);
    		int files = getFiles(zipFile);
    		zipFile.close();
    		return files;
    	}
    
    	/**
    	 * 从zip包中读取给定文件名称的内容
    	 * 
    	 * @param zipFilename
    	 * @param name
    	 *            获得内容的文件全文件名称 注:大写和小写敏感
    	 * @return
    	 * @throws IOException
    	 */
    	public static byte[] getContent(final String zipFilename, final String name)
    			throws IOException {
    		ZipFile zipFile = new ZipFile(zipFilename);
    		byte[] bytes = getContent(zipFile, name);
    		zipFile.close();
    		return bytes;
    	}
    
    	/**
    	 * 从zip包中读取给定文件名称的内容
    	 * 
    	 * @param file
    	 * @param name
    	 *            获得内容的文件全文件名称 注:大写和小写敏感
    	 * @return
    	 * @throws IOException
    	 */
    	public static byte[] getContent(final File file, final String name)
    			throws IOException {
    		ZipFile zipFile = new ZipFile(file);
    		byte[] bytes = getContent(zipFile, name);
    		zipFile.close();
    		return bytes;
    	}
    
    	/**
    	 * 从zip包中读取给定文件名称的内容
    	 * 
    	 * @param zipFile
    	 * @param name
    	 *            获得内容的文件全文件名称 注:大写和小写敏感
    	 * @return
    	 * @throws IOException
    	 */
    	public static byte[] getContent(final ZipFile zipFile, final String name)
    			throws IOException {
    		ZipEntry zipEntry = zipFile.getEntry(name);
    		return getContent(zipFile, zipEntry);
    	}
    
    	/**
    	 * 从zip包中读取给定文件名称的内容
    	 * 
    	 * @param zipFile
    	 * @param zipEntry
    	 * @return
    	 * @throws IOException
    	 */
    	public static byte[] getContent(final ZipFile zipFile,
    			final ZipEntry zipEntry) throws IOException {
    		InputStream inputStream = zipFile.getInputStream(zipEntry);
    		byte[] buffer = new byte[1024];
    		byte[] bytes = new byte[0];
    		int length;
    		while ((length = (inputStream.read(buffer))) != -1) {
    			byte[] readBytes = new byte[length];
    			System.arraycopy(buffer, 0, readBytes, 0, length);
    			bytes = SystemUtil.mergeArray(bytes, readBytes);
    		}
    		inputStream.close();
    		return bytes;
    	}
    
    	/**
    	 * 从二进制zip包byte数组中获取给定文件名称的内容
    	 * 
    	 * @param zipBytes
    	 * @param name
    	 * @return
    	 * @throws IOException
    	 * @throws InterruptedException
    	 */
    	public static byte[] getContent(final byte[] zipBytes, final String name)
    			throws IOException, InterruptedException {
    		File file = File.createTempFile(RandomUtils.getTimeRandom(), "."
    				+ "zip");
    		FileOutputStream fileOutputStream = new FileOutputStream(file);
    		fileOutputStream.write(zipBytes);
    		fileOutputStream.flush();
    		fileOutputStream.close();
    		byte[] bytes = getContent(file, name);
    		file.delete();
    		return bytes;
    	}
    
    	/**
    	 * @param zipFilename
    	 * @param filename
    	 * @throws IOException
    	 */
    	public static void add(final String zipFilename, final String filename)
    			throws IOException {
    		File zipFile = new File(zipFilename);
    		File file = new File(filename);
    		add(zipFile, file);
    	}
    
    	/**
    	 * @param zipFile
    	 * @param file
    	 * @throws IOException
    	 */
    	public static void add(final File zipFile, final File file)
    			throws IOException {
    		FileInputStream fileInputStream = new FileInputStream(file);
    		int length = fileInputStream.available();
    		byte[] bytes = new byte[length];
    		fileInputStream.read(bytes);
    		fileInputStream.close();
    		add(zipFile, file.getName(), bytes);
    	}
    
    	/**
    	 * @param zipFile
    	 * @param filename
    	 * @param bytes
    	 * @throws IOException
    	 */
    	public static void add(final File zipFile, final String filename,
    			final byte[] bytes) throws IOException {
    		ZipFile zip = new ZipFile(zipFile);
    		Enumeration entries = zip.entries();
    		FileOutputStream outputStream = new FileOutputStream(zipFile, true);
    		ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    		while (entries.hasMoreElements()) {
    			ZipEntry entry = (ZipEntry) entries.nextElement();
    			addEntry(zipOutputStream, entry,
    					getContent(zipFile, entry.getName()));
    		}
    		ZipEntry zipEntry = new ZipEntry(filename);
    		addEntry(zipOutputStream, zipEntry, bytes);
    
    		zipOutputStream.flush();
    		zipOutputStream.close();
    
    		outputStream.flush();
    		outputStream.close();
    		zip.close();
    	}
    
    	/**
    	 * @param zipFile
    	 * @param filename
    	 * @throws IOException
    	 */
    	public static void delete(final File zipFile, final String filename)
    			throws IOException {
    		ZipFile zip = new ZipFile(zipFile);
    		Enumeration entries = zip.entries();
    		FileOutputStream outputStream = new FileOutputStream(zipFile, true);
    		ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
    		while (entries.hasMoreElements()) {
    			ZipEntry entry = (ZipEntry) entries.nextElement();
    			if (!entry.getName().equalsIgnoreCase(filename)) {
    				addEntry(zipOutputStream, entry,
    						getContent(zipFile, entry.getName()));
    			}
    		}
    		zipOutputStream.flush();
    		zipOutputStream.close();
    
    		outputStream.flush();
    		outputStream.close();
    		zip.close();
    	}
    
    	/**
    	 * 加入单个ZipEntry
    	 * 
    	 * @param zipOutputStream
    	 * @param zipEntry
    	 * @param bytes
    	 * @throws IOException
    	 */
    	private static void addEntry(ZipOutputStream zipOutputStream,
    			ZipEntry zipEntry, byte[] bytes) throws IOException {
    		zipOutputStream.putNextEntry(zipEntry);
    		zipOutputStream.write(bytes);
    		zipOutputStream.flush();
    		zipOutputStream.closeEntry();
    	}
    
    	/**
    	 * 获得zip包中的文件名称列表
    	 * 
    	 * @param zipFile
    	 * @return
    	 */
    	public static List<String> list(final ZipFile zipFile) {
    		List<String> list = new ArrayList<String>();
    		Enumeration zipEntries = zipFile.entries();
    		while (zipEntries.hasMoreElements()) {
    			ZipEntry entry = (ZipEntry) zipEntries.nextElement();
    			list.add(entry.getName());
    		}
    		return list;
    	}
    
    	/**
    	 * 获得zip包中的文件名称列表
    	 * 
    	 * @param file
    	 * @return
    	 */
    	public static List<String> list(final File file) throws IOException {
    		ZipFile zipFile = new ZipFile(file);
    		List<String> filenameList = list(zipFile);
    		zipFile.close();
    		return filenameList;
    	}
    
    	public static InputStream getInputStreamFromZip(final ZipFile zipFile,
    			final String name) throws IOException {
    		ZipEntry zipEntry = zipFile.getEntry(name);
    		InputStream inputStream = zipFile.getInputStream(zipEntry);
    		return inputStream;
    	}
    
    	/***
    	 * 
    	 * @param zipFilename
    	 * @param name
    	 * @return InputStream
    	 * @throws IOException
    	 */
    	public static InputStream getInputStreamFromZip(final String zipFilename,
    			final String name) throws IOException {
    		ZipFile zipFile = new ZipFile(zipFilename);
    		return getInputStreamFromZip(zipFile, name);
    	}
    
    	public static InputStream getInputStreamFromZip(final File file,
    			final String name) throws ZipException, IOException {
    		ZipFile zipFile = new ZipFile(file);
    		return getInputStreamFromZip(zipFile, name);
    	}
    
    	/**
    	 * 获得zip包中的文件名称列表
    	 * 
    	 * @param filename
    	 * @return
    	 * @throws IOException
    	 */
    	public static List<String> list(final String filename) throws IOException {
    		File file = new File(filename);
    		return list(file);
    	}
    
    	/**
    	 * 把源文件夹压缩成zip包
    	 * 
    	 * @param zipFile
    	 * @param srcFolder
    	 * @throws IOException
    	 */
    	public static void compress(final File zipFile, final String srcFolder)
    			throws IOException {
    		File folder = new File(srcFolder);
    		FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
    		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
    				fileOutputStream);
    		ZipOutputStream zipOutputStream = new ZipOutputStream(
    				bufferedOutputStream);
    		if (!folder.isDirectory()) {
    			compress(zipOutputStream, folder, srcFolder);
    		} else {
    			List<String> filenameList = listFilename(srcFolder);
    			for (String filename : filenameList) {
    				File file = new File(filename);
    				compress(zipOutputStream, file, srcFolder);
    			}
    		}
    		zipOutputStream.flush();
    		zipOutputStream.close();
    		bufferedOutputStream.flush();
    		bufferedOutputStream.close();
    		fileOutputStream.flush();
    		fileOutputStream.close();
    	}
    
    	/**
    	 * @param zipOutputStream
    	 * @param file
    	 * @throws IOException
    	 */
    	private static void compress(final ZipOutputStream zipOutputStream,
    			final File file, final String srcFolder) throws IOException {
    		FileInputStream fileInputStream = new FileInputStream(file);
    		BufferedInputStream bufferedInputStream = new BufferedInputStream(
    				fileInputStream, 1024);
    		ZipEntry entry = new ZipEntry(file.getPath().substring(
    				srcFolder.length() + 1));
    		zipOutputStream.putNextEntry(entry);
    		byte[] data = new byte[bufferedInputStream.available()];
    		bufferedInputStream.read(data);
    		zipOutputStream.write(data);
    		zipOutputStream.flush();
    		bufferedInputStream.close();
    		fileInputStream.close();
    	}
    
    	/**
    	 * 解压zip包到目标文件夹里面
    	 * 
    	 * @param zipFile
    	 * @param targetFolder
    	 * @throws IOException
    	 */
    	public static void decompress(final File zipFile, final String targetFolder)
    			throws IOException {
    		List<String> filenameList = list(zipFile);
    		for (String filename : filenameList) {
    			File file = new File(targetFolder + File.separatorChar + filename);
    			if (!file.exists()) {
    				File parentPath = file.getParentFile();
    				if (!parentPath.exists()) {
    					parentPath.mkdirs();
    				}
    				file.createNewFile();
    			}
    			write(file, getContent(zipFile, filename));
    		}
    	}
    
    	/**
    	 * 解压zip包二进制数组到目标文件夹里面
    	 * 
    	 * @param zipBytes
    	 * @param targetFolder
    	 * @throws IOException
    	 */
    	public static void decompress(final byte[] zipBytes,
    			final String targetFolder) throws IOException {
    		File tempFile = File.createTempFile(RandomUtils.getTimeRandom(), "."
    				+ "zip");
    		FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
    		fileOutputStream.write(zipBytes);
    		fileOutputStream.flush();
    		fileOutputStream.close();
    
    		System.out.println(tempFile.getPath());
    		decompress(tempFile, targetFolder);
    		tempFile.delete();
    	}
    
    	/**
    	 * 写入单个文件
    	 * 
    	 * @param file
    	 * @param bytes
    	 * @throws IOException
    	 */
    	private static void write(final File file, final byte[] bytes)
    			throws IOException {
    		FileOutputStream fileOutputStream = new FileOutputStream(file);
    		fileOutputStream.write(bytes);
    		fileOutputStream.flush();
    		fileOutputStream.close();
    	}
    
    	/**
    	 * 遍历出全部的文件名称和文件夹名
    	 * 
    	 * @param path
    	 * @return
    	 */
    	public static List<String> listFilename(final String path) {
    		List<String> list = new ArrayList<String>();
    		File folder = new File(path);
    		if (folder.isDirectory()) {
    			File[] files = folder.listFiles();
    			if (files != null && files.length > 0) {
    				for (File file : files) {
    					if (file.isDirectory()) {
    						for (String filename : listFilename(file.getPath())) {
    							list.add(filename);
    						}
    					} else {
    						list.add(file.getPath());
    					}
    				}
    			} else {
    				list.add(folder.getPath());
    			}
    		} else {
    			list.add(folder.getPath());
    		}
    		return list;
    	}
    }
    

    import java.io.IOException;
    
    public class ProcessUtil
    {
        private static Process process = null;
        static
        {
    
        }
        private ProcessUtil()
        {
            throw new Error("Don't let anyone instantiate this class.");
        }
    
        public static Process getPro(String command) throws IOException
        {
            process = Runtime.getRuntime().exec(command);
            return process;
        }
    
        public static Process getPro(String[] command) throws IOException
        {
            process = Runtime.getRuntime().exec(command);
            return process;
        }
    }
    


  • 相关阅读:
    Oracle Function:COUNT
    SQL Fundamentals: Using Single-Row Functions to Customize Output使用单行函数自定义输出
    Oracle Function: NVL
    将searchBar的背景灰色去掉
    cardboard sdk for unity 系统分析
    搭建CARDBOARD+ANDROID+unity3d的VR开发环境
    adb server is out of date.killing的解决办法
    Android SDK Manager国内无法更新的解决方案
    cardboard sdk for unity 下载地址
    转载:Eclipse build Android时不生成apk问题解决方法
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7101424.html
Copyright © 2011-2022 走看看