123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 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<String, String> objToMap(Object obj) {
- if (obj == null) {
- return null;
- }
- JSONObject jsonObj = (JSONObject) JSON.toJSON(obj);
- Map<String, String> map = JSONObject.parseObject(jsonObj.toJSONString(), new TypeReference<Map<String, String>>() {
- });
- 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<Map<String, Object>> arr = new ArrayList<>();
- Map<String, Object> map1 = new HashMap<>();
- map1.put("key", "123");
- map1.put("value", "1");
- arr.add(map1);
- List<Object> map = arr.stream().map(e -> e.put("key", "")).collect(Collectors.toList());
- System.out.println(JSON.toJSONString(map));
- //System.out.println(StringUtils.join(arr,","));
- }
- }
|