| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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 |
| | | */ |
| | |
| | | userInf.setUName("未登录的用户账号"); |
| | | userInf.setUId(0L); |
| | | userInf.setURole(0); |
| | | userInf.setUpassword("123456"); |
| | | }else{ |
| | | userInf=(UserInf) session.getAttribute("user"); |
| | | } |
| | |
| | | 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); |
| | |
| | | * @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 { |
| | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | 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; |
| | | } |
| | |
| | | Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]"); |
| | | Matcher matcher = pattern.matcher(fileName); |
| | | fileName = matcher.replaceAll(""); |
| | | return fileName; |
| | | 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); |
| | | } |
| | | |
| | | } |