admin
2025-02-20 f537abe9f3646c739beaf15076246a2f71a347e9
src/main/java/com/yeshi/buwan/util/StringUtil.java
@@ -1,30 +1,17 @@
package com.yeshi.buwan.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.gson.*;
import javax.persistence.Entity;
import javax.servlet.http.HttpServletRequest;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.lang.reflect.Type;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @author Administrator
@@ -65,6 +52,18 @@
        return st;
    }
    public static String join(Collection srcList, String seperate) {
        String st = "";
        for (Iterator<String> its = srcList.iterator(); its.hasNext(); ) {
            st += its.next() + seperate;
        }
        if (st.endsWith(seperate)) {
            st = st.substring(0, st.length() - seperate.length());
        }
        return st;
    }
    /**
     * 随机码生成
     *
@@ -86,9 +85,13 @@
     * @return
     */
    public static String getVerifyCode() {
        return getNumberVerifyCode(6);
    }
    public static String getNumberVerifyCode(int number) {
        String sts = "0123456789";
        String code = "";
        for (int i = 0; i < 6; i++) {
        for (int i = 0; i < number; i++) {
            int p = (int) (Math.random() * 10);
            code += sts.charAt(p);
        }
@@ -172,7 +175,16 @@
            return o.toString();
        } else {
            Gson gson = new GsonBuilder().enableComplexMapKeySerialization().excludeFieldsWithoutExposeAnnotation()
                    .setDateFormat(DateFormat.LONG).setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)// 会把字段首字母大写
                    .setDateFormat(DateFormat.LONG).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                        @Override
                        public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
                            if (value == null) {
                                return new JsonPrimitive("0");
                            } else {
                                return new JsonPrimitive(value.getTime());
                            }
                        }
                    }).setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)// 会把字段首字母大写
                    .setPrettyPrinting().setVersion(1.0).create();
            String st = gson.toJson(o);
            return Utils.JsonFilter(st);
@@ -257,7 +269,7 @@
            e.printStackTrace();
        }
        if (b != null) {
            s = new BASE64Encoder().encode(b);
            s = Base64.getEncoder().encodeToString(b);
        }
        return s;
    }
@@ -266,19 +278,17 @@
        String s = null;
        if (b != null) {
            s = new BASE64Encoder().encode(b);
            s = Base64.getEncoder().encodeToString(b);
        }
        return s;
    }
    // 解密
    public static String getFromBase64(String s) {
        byte[] b = null;
        String result = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = Base64.getDecoder().decode(s);
            try {
                b = decoder.decodeBuffer(s);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
@@ -288,17 +298,7 @@
    }
    public static byte[] getFromBase64Byte(String s) {
        byte[] b = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                b = decoder.decodeBuffer(s);
                return b;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
        return Base64.getDecoder().decode(s);
    }
    public static String getNumberFromString(String st) {
@@ -341,10 +341,9 @@
            }
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            byte[] b = Base64.getDecoder().decode(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
@@ -356,6 +355,7 @@
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }