package com.ruoyi.base.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.*; import java.util.stream.Collectors; /** * Created by IntelliJ IDEA. * User: andy.qu * Date: 2019/11/14 */ public class CommonUtils { //转变的依赖字符 public static final char UNDERLINE = '_'; /** * 文件下载 * * @param file * @param res * @throws IOException */ public static void download(File file, HttpServletResponse res) throws IOException { // 发送给客户端的数据 OutputStream outputStream = res.getOutputStream(); byte[] buff = new byte[1024]; BufferedInputStream bis = null; // 读取filename bis = new BufferedInputStream(new FileInputStream(file)); int i = bis.read(buff); while (i != -1) { outputStream.write(buff, 0, buff.length); outputStream.flush(); i = bis.read(buff); } } /** * 文件下载 * * @param inputStream * @param res * @throws IOException */ public static void download(InputStream inputStream, HttpServletResponse res) throws IOException { // 发送给客户端的数据 OutputStream outputStream = res.getOutputStream(); byte[] buff = new byte[1024]; BufferedInputStream bis = null; // 读取filename bis = new BufferedInputStream(inputStream); int i = bis.read(buff); while (i != -1) { outputStream.write(buff, 0, buff.length); outputStream.flush(); i = bis.read(buff); } } /** * 对象字符串转换null判断 * * @return */ public static String objectStringConvert(Object object) { return object == null ? "" : object.toString(); } public static String getString(String string) { if (string == null) { return ""; } else { return string; } } /** * 检查是否为Int * * @param value 值 * @param fildName 提示字段名称 * @return */ public static void checkInt(String value, String fildName) { try { Integer i = Integer.parseInt(value); } catch (Exception e) { throw new RuntimeException(fildName + "必须是整数"); } } /** * 对象转map * * @param obj * @return */ public static Map objToMap(Object obj) { if (obj == null) { return null; } JSONObject jsonObj = (JSONObject) JSON.toJSON(obj); Map map = JSONObject.parseObject(jsonObj.toJSONString(), new TypeReference>() { }); return map; } /** * 将驼峰转换成"_"(userId:user_id) * * @param param * @return */ public static String camelToUnderline(String param) { if (param == null || "".equals(param.trim())) { return ""; } int len = param.length(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { char c = param.charAt(i); if (Character.isUpperCase(c)) { sb.append(UNDERLINE); sb.append(Character.toLowerCase(c)); } else { sb.append(c); } } return sb.toString(); } /** * 将"_"转成驼峰(user_id:userId) * * @param param * @return */ public static String underlineToCamel(String param) { if (param == null || "".equals(param.trim())) { return ""; } int len = param.length(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { char c = param.charAt(i); if (c == UNDERLINE) { if (++i < len) { sb.append(Character.toUpperCase(param.charAt(i))); } } else { sb.append(c); } } return sb.toString(); } public static String lpad(String s, int n, String replace) { while (s.length() < n) { s = replace + s; } return s; } public static void main(String[] args) { //String pwd = RandomUtil.randomNumbers(6); //System.out.println(DigestUtils.sha256Hex("583817")); List> arr = new ArrayList<>(); Map map1 = new HashMap<>(); map1.put("key", "123"); map1.put("value", "1"); arr.add(map1); List map = arr.stream().map(e -> e.put("key", "")).collect(Collectors.toList()); System.out.println(JSON.toJSONString(map)); //System.out.println(StringUtils.join(arr,",")); } }