StringUtil.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.jsjty.util;
  2. import java.io.PrintWriter;
  3. import java.io.StringWriter;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.URLEncoder;
  6. import java.sql.Timestamp;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Collection;
  9. import java.util.Date;
  10. import java.util.Map;
  11. import java.util.UUID;
  12. import javax.servlet.http.HttpServletRequest;
  13. public class StringUtil {
  14. private final static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
  15. public static String formatDate(Date date) {
  16. return dateformat.format(date);
  17. }
  18. public static String formatTimestamp(Timestamp timestamp) {
  19. return dateformat.format(timestamp);
  20. }
  21. /**
  22. * 获取错误日志详细信息
  23. *
  24. * @param t
  25. * @return String
  26. */
  27. public static String getTrace(Throwable t) {
  28. StringWriter stringWriter = new StringWriter();
  29. PrintWriter writer = new PrintWriter(stringWriter);
  30. t.printStackTrace(writer);
  31. StringBuffer buffer = stringWriter.getBuffer();
  32. return buffer.toString();
  33. }
  34. public static String[] str2Array(String params) {
  35. if (params != null) { return params.split(","); }
  36. return null;
  37. }
  38. /**
  39. * 对文件流输出下载的中文文件名进行编码 屏蔽各种浏览器版本的差异性
  40. */
  41. public static String encodeChineseDownloadFileName(HttpServletRequest request, String pFileName) {
  42. String agent = request.getHeader("USER-AGENT");
  43. try {
  44. if (null != agent && -1 != agent.indexOf("MSIE")) {
  45. pFileName = URLEncoder.encode(pFileName, "utf-8");
  46. }
  47. else {
  48. pFileName = new String(pFileName.getBytes("utf-8"), "iso8859-1");
  49. }
  50. }
  51. catch (UnsupportedEncodingException e) {
  52. e.printStackTrace();
  53. }
  54. return pFileName;
  55. }
  56. /**
  57. * 判断对象是否Empty(null或元素为0)<br>
  58. * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
  59. *
  60. * @param pObj
  61. * 待检查对象
  62. * @return boolean 返回的布尔值
  63. */
  64. @SuppressWarnings("rawtypes")
  65. public static boolean isEmpty(Object pObj) {
  66. if (pObj == null) return true;
  67. if (pObj == "") return true;
  68. if (pObj instanceof String) {
  69. if (((String) pObj).length() == 0) { return true; }
  70. }
  71. else if (pObj instanceof Collection) {
  72. if (((Collection) pObj).size() == 0) { return true; }
  73. }
  74. else if (pObj instanceof Map) {
  75. if (((Map) pObj).size() == 0) { return true; }
  76. }
  77. return false;
  78. }
  79. /**
  80. * 判断对象是否为NotEmpty(!null或元素>0)<br>
  81. * 实用于对如下对象做判断:String Collection及其子类 Map及其子类
  82. *
  83. * @param pObj
  84. * 待检查对象
  85. * @return boolean 返回的布尔值
  86. */
  87. @SuppressWarnings("rawtypes")
  88. public static boolean isNotEmpty(Object pObj) {
  89. if (pObj == null) return false;
  90. if (pObj == "") return false;
  91. if (pObj instanceof String) {
  92. if (((String) pObj).length() == 0) { return false; }
  93. }
  94. else if (pObj instanceof Collection) {
  95. if (((Collection) pObj).size() == 0) { return false; }
  96. }
  97. else if (pObj instanceof Map) {
  98. if (((Map) pObj).size() == 0) { return false; }
  99. }
  100. return true;
  101. }
  102. public static String getUUID() {
  103. return UUID.randomUUID().toString().replace("-", "");
  104. }
  105. public static String parseByte2HexStr(byte buf[]) {
  106. StringBuffer sb = new StringBuffer();
  107. for (int i = 0; i < buf.length; i++) {
  108. String hex = Integer.toHexString(buf[i] & 0xFF);
  109. if (hex.length() == 1) {
  110. hex = '0' + hex;
  111. }
  112. sb.append(hex.toUpperCase());
  113. }
  114. return sb.toString();
  115. }
  116. /**
  117. * 把16进制字符串转换成字节数组
  118. *
  119. * @param hexString
  120. * @return byte[]
  121. */
  122. public static byte[] hexStringToByte(String hex) {
  123. int len = (hex.length() / 2);
  124. byte[] result = new byte[len];
  125. char[] achar = hex.toCharArray();
  126. for (int i = 0; i < len; i++) {
  127. int pos = i * 2;
  128. result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
  129. }
  130. return result;
  131. }
  132. private static int toByte(char c) {
  133. byte b = (byte) "0123456789ABCDEF".indexOf(c);
  134. return b;
  135. }
  136. }