whyclxw
2024-04-28 b25b814a949d2065a61fd3167206ffb51a57af65
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;
@@ -21,24 +22,29 @@
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
    */
@@ -416,10 +422,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 +448,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 +463,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,9 +596,23 @@
   public static String getLang() {
      Locale locale = Locale.getDefault();//对Locale类实例化定义
      String lang = locale.getLanguage();
      if (ActionUtil.getSession().getAttribute("lang") != null) {
         lang = (String) ActionUtil.getSession().getAttribute("lang");
      //String lang = "zh";
      String str = (String) ActionUtil.getSession().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);
   }
}