字符判断常用方法工具类

字符判断常用方法工具类

今天我再次奢侈一把,给大家更新一篇日常开发中常用的于字符串的一些操作以及获取文件上传的类型,如doc,pdf等…

上代码

package com.lxito.common.util;
import cn.hutool.core.text.StrBuilder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 字符使用、操作 工具类
 * @Author gxh
 * @Data 2021/8/25
 */
public final class GStringDecideUtil {

    /***
     * 私有构造 禁止外界创建对象
     */
    private GStringDecideUtil() {}


    /**
     * 判断文件是否为 pdf
     * @param fileName 文件名
     * @return boolean  T是 | F否
     */
    public static boolean isPdf(String fileName){
        boolean isNull = nonEmpty(fileName);
        if (isNull){
            String reg = ".+(.PDF|.pdf)$";
            Matcher matcher = Pattern.compile(reg).matcher(fileName);
            if (!matcher.find()){
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 判断文件是否为 doc
     * @param fileName 文件名
     * @return boolean  T是 | F否
     */
    public static boolean isDoc(String fileName){
        boolean isNull = nonEmpty(fileName);
        if (isNull){
            String reg = ".+(.DOC|.doc)$";
            Matcher matcher = Pattern.compile(reg).matcher(fileName);
            if (!matcher.find()){
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 判断文件是否为 docx
     * @param fileName 文件名
     * @return boolean  T是 | F否
     */
    public static boolean isDocx(String fileName){
        boolean isNull = nonEmpty(fileName);
        if (isNull){
            String reg = ".+(.DOCX|.docx)$";
            Matcher matcher = Pattern.compile(reg).matcher(fileName);
            if (!matcher.find()){
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 判断文件是否是 excel
     * @param fileName 文件名
     * @return boolean  T是 | F否
     */
    public static boolean isExcel(String fileName){
        boolean isNull = nonEmpty(fileName);
        if (isNull){
            String reg = ".+(.XLS|.xls|.XLSX|.xlsx)$";
            Matcher matcher = Pattern.compile(reg).matcher(fileName);
            if (!matcher.find()){
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     *  Dx | 大写
     * 判断字符串是否为全部大写
     * @param var 判断什么传入什么 字符串
     * @return boolean
     */
    public static boolean isDx(String var) {
        if (null == var || "".equals(var) || var.length() == 0){
            return false;
        }
        for (int i=0,len = var.length();i<len;i++) {
            char c = var.charAt(i);
            if (Character.isLowerCase(c)) {
                return false;
            }
        }
        return true;
    }

    /****
     * Email | 邮箱
     * 判断是否为合法 邮箱
     * @param email
     * @return
     */
    public static boolean isEmail(String email){
        Pattern emailPattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        Matcher matcher = emailPattern.matcher(email);
        if (matcher.find()){
            return true;
        }else {
            return false;
        }
    }

    /**
     * Legal | 合法的
     * 检查字符串是否包含 非法字符  如: 空格  回车  空字符串
     *                 如果包含以上  自动剔除符号,返回新的字符串
     *                 可以追加 if(){}
     *                 可以移除 if(){}
     * @param str 被检查的字符串
     * @return 剔除符号后 新的字符串
     */
    public static String isLegal(String str){
        if (str.contains("\t")){
            str = str.replace("\t","");
        }
        if (str.contains("\n")){
            str = str.replace("\n","");
        }
        if (str.contains("\r")){
            str = str.replace("\r","");
        }
        if (str.contains(" ")){
            str = str.replace(" ","");
        }
        if (str.contains(".")){
            str = str.replace(".","");
        }
        if (str.contains("\u0000")){
            str = str.replace("\u0000","");
        }
        return str;
    }


    /**
     * 字符拼接(StringBuffer)    线程安全
     * @return
     */
    public static String strBfSpl(String...str){
        Integer size = str.length;
        if (null == size || 0 == size){
            return "";
        }
        if (1 == size){
            return str[0];
        }

        StringBuffer sbf = new StringBuffer();
        for (int i=0;i<str.length;i++){
            sbf.append(str[i]);
        }
        return sbf.toString();
    }


    /**
     * 字符拼接(StrBuilder)    线程不安全
     * @return
     */
    public static String strBdSpl(String...str){
        Integer size = str.length;
        if (null == size || 0 == size){
            return "";
        }
        if (1 == size){
            return str[0];
        }
        StrBuilder sb = new StrBuilder();
        for (int i=0;i<str.length;i++){
            sb.append(str[i]);
        }
        return sb.toString();
    }


    /**
     * 本类使用判断 字符串 是否为空
     * @param str
     * @return
     */
    private static boolean nonEmpty(String str){
        if (null == str || "".equals(str) || str.length() == 0){
            return false;
        }
        return true;
    }

}

File工具类
1.MultipartFile转File

public static File multipartFileToFile(MultipartFile file) throws IOException {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2.File转MultipartFile

   public static MultipartFile fileToMultipart(File file) {
        //无参构造,文件大小最大值便是默认值10kb,超过10Kb便会通过生成临时文件转化不占用内存
        FileItemFactory factory = new DiskFileItemFactory();
        FileItem item = factory.createItem("textField", "text/plain", true, file.getName());
        int bytesRead;
        byte[] buffer = new byte[1024];
        try (FileInputStream fis = new FileInputStream(file);
             OutputStream os = item.getOutputStream()) {
            while ((bytesRead = fis.read(buffer, 0, 1024)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            System.out.println("文件转化失败, fileUrl: " + file.getName());
            System.out.println("e: " + e);
            return null;
        } finally {
            //操作完删除缓存区文件
            file.delete();
        }
        return new CommonsMultipartFile(item);
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白的小,小白的白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值