whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/util/ActionUtil.java
@@ -5,6 +5,7 @@
import com.google.gson.JsonSyntaxException;
import com.whyc.pojo.UserInf;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.misc.BASE64Decoder;
@@ -18,27 +19,35 @@
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ActionUtil{
public class ActionUtil {
   public static String time_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
   public static String time_yyyyMMdd = "yyyy-MM-dd";
   public static String time_yyyyMMdd_HH_mm_ss = "yyyy-MM-dd_HH_mm_ss";
   public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   public static SimpleDateFormat sdfwithOutday = new SimpleDateFormat("yyyy_MM");
   public static SimpleDateFormat sdfwithday = new SimpleDateFormat("yyyy-MM-dd");
   public static SimpleDateFormat sdfwithtime = new SimpleDateFormat("HH:mm:ss");
   public static SimpleDateFormat sdfwithtime_yyyyMMdd_HH_mm_ss = new SimpleDateFormat(time_yyyyMMdd_HH_mm_ss);
   public static Object objeNull = null;
   /*
    * 获取HttpServletRequest
    */
   public static HttpServletRequest getRequest(){
   public static HttpServletRequest getRequest() {
      ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
      HttpServletRequest request = requestAttributes.getRequest();
      return request;
   }
   /*
    * 获取HttpServletResponse
    */
@@ -198,6 +207,7 @@
         userInf.setUName("未登录的用户账号");
         userInf.setUId(0L);
         userInf.setURole(0);
         userInf.setUpassword("123456");
       }else{
         userInf=(UserInf) session.getAttribute("user");
       }
@@ -416,10 +426,17 @@
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }finally {
         if(inputStream!=null){
            try {
               inputStream.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
@@ -435,8 +452,10 @@
     * @return
    */
    public static boolean generateImage(String imgStr, String path) {
       if (imgStr == null) {
         return false;
       boolean bl=false;
      OutputStream out=null;
      if (imgStr == null) {
          bl=false;
      }
       BASE64Decoder decoder = new BASE64Decoder();
       try {
@@ -448,14 +467,22 @@
                b[i] += 256;
             }
          }
          OutputStream out = new FileOutputStream(path);
          out = new FileOutputStream(path);
          out.write(b);
          out.flush();
          out.close();
          return true;
          bl=true;
       } catch (Exception e) {
          return false;
       }
          bl=false;
       }finally {
         if(out!=null){
            try {
               out.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
       return bl;
    }
    
    /**
@@ -573,10 +600,98 @@
   public static String getLang() {
      Locale locale = Locale.getDefault();//对Locale类实例化定义
      String lang = locale.getLanguage();
      //String lang = "zh";
      String str = (String) ActionUtil.getSession().getAttribute("lang");
      if (str != null) {
         lang = str;
      }
      return lang;
   }
   //获取当前系统的语言环境
   public static String getLang4Socket(HttpSession httpSession) {
      Locale locale = Locale.getDefault();//对Locale类实例化定义
      String lang = locale.getLanguage();
      //String lang = "zh";
      String str = (String) httpSession.getAttribute("lang");
      if (str != null) {
         lang = str;
      }
      return lang;
   }
   /**
    * 输入验证:路径遍历,防止恶意符号影响文件体系
    * 过滤掉特殊字符 ”/\" : | * ? < >”
    */
   public static String filterFileName(String fileName){
      Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]");
      Matcher matcher = pattern.matcher(fileName);
      fileName = matcher.replaceAll("");
      return FilenameUtils.getName(fileName);
   }
   /**
    * 输入验证:路径遍历,防止恶意符号影响文件体系
    * 过滤掉特殊字符 ”/\" : | * ? < >”
    */
   public static boolean filterPwd(String pwd){
      String regex="^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+\\-])[a-zA-Z\\d!@#$%^&*()_+\\-]{8,}$";
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(pwd);
      return matcher.matches();
   }
   /**
    * 将时间字符串(格式HH:mm:ss)转换为秒数
    *
    * @param timeStr 时间字符串
    * @return 转换后的秒数
    */
   public static int convertToSeconds(String timeStr) {
      // 使用冒号(:)分割时间字符串
      String[] parts = timeStr.split(":");
      // 验证时间字符串是否有效(假设总是包含三个部分)
      if (parts.length != 3) {
         throw new IllegalArgumentException("时间字符串格式不正确,应为HH:mm:ss");
      }
      // 解析小时、分钟和秒
      int hours = Integer.parseInt(parts[0]);
      int minutes = Integer.parseInt(parts[1]);
      int seconds = Integer.parseInt(parts[2]);
      // 转换为总秒数
      // 注意:1小时 = 3600秒,1分钟 = 60秒
      int totalSeconds = hours * 3600 + minutes * 60 + seconds;
      return totalSeconds;
   }
   public static String parseDurationDirectly(String timeStr) {
      // 获取当前时间
      LocalDateTime now = LocalDateTime.now();
      String[] parts = timeStr.split(":");
      if (parts.length != 3) {
         throw new IllegalArgumentException("时间长度字符串格式不正确");
      }
      int hours = Integer.parseInt(parts[0]);
      int minutes = Integer.parseInt(parts[1]);
      int seconds = Integer.parseInt(parts[2]);
      Duration timeToAdd = Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds);
      // 将时间长度加到当前时间上
      LocalDateTime updatedTime = now.plus(timeToAdd);
      return  updatedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
   }
   public static void main(String[] args) {
      //System.out.println(filterPwd("Aa@123456"));
      // 时间长度字符串
      String timeToAddStr = "00:00:34";
      String time =parseDurationDirectly(timeToAddStr);
      System.out.println(time);
   }
}