CommonUtils.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.ruoyi.base.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.TypeReference;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.*;
  7. import java.util.*;
  8. import java.util.stream.Collectors;
  9. /**
  10. * Created by IntelliJ IDEA.
  11. * User: andy.qu
  12. * Date: 2019/11/14
  13. */
  14. public class CommonUtils {
  15. //转变的依赖字符
  16. public static final char UNDERLINE = '_';
  17. /**
  18. * 文件下载
  19. *
  20. * @param file
  21. * @param res
  22. * @throws IOException
  23. */
  24. public static void download(File file, HttpServletResponse res) throws IOException {
  25. // 发送给客户端的数据
  26. OutputStream outputStream = res.getOutputStream();
  27. byte[] buff = new byte[1024];
  28. BufferedInputStream bis = null;
  29. // 读取filename
  30. bis = new BufferedInputStream(new FileInputStream(file));
  31. int i = bis.read(buff);
  32. while (i != -1) {
  33. outputStream.write(buff, 0, buff.length);
  34. outputStream.flush();
  35. i = bis.read(buff);
  36. }
  37. }
  38. /**
  39. * 文件下载
  40. *
  41. * @param inputStream
  42. * @param res
  43. * @throws IOException
  44. */
  45. public static void download(InputStream inputStream, HttpServletResponse res) throws IOException {
  46. // 发送给客户端的数据
  47. OutputStream outputStream = res.getOutputStream();
  48. byte[] buff = new byte[1024];
  49. BufferedInputStream bis = null;
  50. // 读取filename
  51. bis = new BufferedInputStream(inputStream);
  52. int i = bis.read(buff);
  53. while (i != -1) {
  54. outputStream.write(buff, 0, buff.length);
  55. outputStream.flush();
  56. i = bis.read(buff);
  57. }
  58. }
  59. /**
  60. * 对象字符串转换null判断
  61. *
  62. * @return
  63. */
  64. public static String objectStringConvert(Object object) {
  65. return object == null ? "" : object.toString();
  66. }
  67. public static String getString(String string) {
  68. if (string == null) {
  69. return "";
  70. } else {
  71. return string;
  72. }
  73. }
  74. /**
  75. * 检查是否为Int
  76. *
  77. * @param value 值
  78. * @param fildName 提示字段名称
  79. * @return
  80. */
  81. public static void checkInt(String value, String fildName) {
  82. try {
  83. Integer i = Integer.parseInt(value);
  84. } catch (Exception e) {
  85. throw new RuntimeException(fildName + "必须是整数");
  86. }
  87. }
  88. /**
  89. * 对象转map
  90. *
  91. * @param obj
  92. * @return
  93. */
  94. public static Map<String, String> objToMap(Object obj) {
  95. if (obj == null) {
  96. return null;
  97. }
  98. JSONObject jsonObj = (JSONObject) JSON.toJSON(obj);
  99. Map<String, String> map = JSONObject.parseObject(jsonObj.toJSONString(), new TypeReference<Map<String, String>>() {
  100. });
  101. return map;
  102. }
  103. /**
  104. * 将驼峰转换成"_"(userId:user_id)
  105. *
  106. * @param param
  107. * @return
  108. */
  109. public static String camelToUnderline(String param) {
  110. if (param == null || "".equals(param.trim())) {
  111. return "";
  112. }
  113. int len = param.length();
  114. StringBuilder sb = new StringBuilder(len);
  115. for (int i = 0; i < len; i++) {
  116. char c = param.charAt(i);
  117. if (Character.isUpperCase(c)) {
  118. sb.append(UNDERLINE);
  119. sb.append(Character.toLowerCase(c));
  120. } else {
  121. sb.append(c);
  122. }
  123. }
  124. return sb.toString();
  125. }
  126. /**
  127. * 将"_"转成驼峰(user_id:userId)
  128. *
  129. * @param param
  130. * @return
  131. */
  132. public static String underlineToCamel(String param) {
  133. if (param == null || "".equals(param.trim())) {
  134. return "";
  135. }
  136. int len = param.length();
  137. StringBuilder sb = new StringBuilder(len);
  138. for (int i = 0; i < len; i++) {
  139. char c = param.charAt(i);
  140. if (c == UNDERLINE) {
  141. if (++i < len) {
  142. sb.append(Character.toUpperCase(param.charAt(i)));
  143. }
  144. } else {
  145. sb.append(c);
  146. }
  147. }
  148. return sb.toString();
  149. }
  150. public static String lpad(String s, int n, String replace) {
  151. while (s.length() < n) {
  152. s = replace + s;
  153. }
  154. return s;
  155. }
  156. public static void main(String[] args) {
  157. //String pwd = RandomUtil.randomNumbers(6);
  158. //System.out.println(DigestUtils.sha256Hex("583817"));
  159. List<Map<String, Object>> arr = new ArrayList<>();
  160. Map<String, Object> map1 = new HashMap<>();
  161. map1.put("key", "123");
  162. map1.put("value", "1");
  163. arr.add(map1);
  164. List<Object> map = arr.stream().map(e -> e.put("key", "")).collect(Collectors.toList());
  165. System.out.println(JSON.toJSONString(map));
  166. //System.out.println(StringUtils.join(arr,","));
  167. }
  168. }