Java常用的8个开源类库

一 点睛

1 IOUtils

org.apache.commons.io.IOUtils,操作 IO 流的工具类。

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

2 FileUtils

org.apache.commons.io.FileUtils,操作文件或者目录的工具类。

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

3 StringUtils

org.apache.commons.lang3.StringUtils,操作字符串的工具类,并且是 null 安全的。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>

4 FilenameUtils

org.apache.commons.io.FilenameUtils,操作文件名或者路径的工具类。

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

5 ArrayUtils

org.apache.commons.lang3.ArrayUtils,操作数组的工具类。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>

6 DigestUtils

org.apache.commons.codec.digest.DigestUtils,加密的工具类。

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.14</version>
</dependency>

7 StringEscapeUtils

org.apache.commons.text.StringEscapeUtils,字符串的转义和反转义工具类。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>

8 BeanUtils

org.springframework.beans.BeanUtils,操作JavaBean的工具类。

二 实战

/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: ToolUtils
 * Author:   cakin
 * Date:     2020/4/26
 * Description: 常用开源工具测试
 */
package toolutils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.io.IOException;
import java.io.File;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;

import static common.Constant.SPLIT_LINE;
import static common.Constant.NUM4;
import static common.Constant.NUM5;
import static common.Constant.NUM6;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;


/**
 * @ClassName: ToolUtils
 * @Description: 常用开源工具测试
 * @Date: 2020/4/26
 * @Author: cakin
 */
@Slf4j
public class ToolUtils {
    public static void main( String[] args ) {
        log.info(SPLIT_LINE);
        testIOUtils();
        log.info(SPLIT_LINE);
        testFileUtils();
        log.info(SPLIT_LINE);
        testStringUtils();
        log.info(SPLIT_LINE);
        testFilenameUtils();
        log.info(SPLIT_LINE);
        testArrayUtils();
        log.info(SPLIT_LINE);
        testDigestUtils();
        log.info(SPLIT_LINE);
        testStringEscapeUtils();
        log.info(SPLIT_LINE);
        testBeanUtils();
        log.info(SPLIT_LINE);
    }

    /**
     * 功能描述:IOUtils测试
     *
     * @author cakin
     * @date 2020/4/28
     */
    private static void testIOUtils() {
        String strSource = "D:/test/test/source.txt";
        File file = new File(strSource);

        String str = "D:/test/test/des.txt";
        String str1 = "D:/test/test/des1.txt";
        File saveFile = new File(str);
        File saveFile1 = new File(str1);
        OutputStream outputStream = null;
        OutputStream outputStream1 = null;
        InputStream inputStream = null;

        try {
            outputStream = new FileOutputStream(saveFile);
            outputStream1 = new FileOutputStream(saveFile1);
        } catch (FileNotFoundException e) {
            log.info("catch FileNotFoundException");
        }

        try {
            inputStream = new FileInputStream(file);
            byte[] result = IOUtils.toByteArray(inputStream); // 以byte[]形式获取输入流中的内容,输入流读完后,再读内容为空
            String resultString = new String(result, "UTF-8"); // 将byte数组转成字符
            log.info((resultString));

            /**
             * inputStream只能读一次,如果读多次,会异常,这里有两种解决方法
             * 1 屏蔽上面这段代码
             * 2 inputStream重写赋值
             * 这里采用方式2
             */
            inputStream = new FileInputStream(file); // inputStream 重新赋值
            byte[] intArray = new byte[inputStream.available()]; // 获取输入流的大小
            IOUtils.readFully(inputStream, intArray);
            String resultString1 = new String(intArray, "UTF-8");
            log.info(resultString1);

        } catch (IOException e) {
            log.info("catch IOException");
        }

        try {
            InputStream inputStream2 = new FileInputStream(file);
            IOUtils.copy(inputStream2, outputStream1); // 将字节从输入流赋值到输出流,拷贝完后,输入流清空
            // 从输入流中复制内容到输出流,超过2G
            IOUtils.copyLarge(inputStream2, outputStream1);
            IOUtils.write("是追加而不是覆盖", outputStream1, "UTF-8"); // 将字节或字符写入字节流中
            // 从输入流中一行一行读取,并安装指定的字符编码返回字符串列表
            InputStream inputStream1 = new FileInputStream(file);
            List<String> lines = IOUtils.readLines(new InputStreamReader(inputStream1, "utf-8"));
            for (String line : lines
            ) {
                log.info(line);
            }
        } catch (IOException e) {
            log.info("catch IOException");
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(outputStream);
        }
    }

    /**
     * 功能描述:FileUtils测试
     *
     * @author cakin
     * @date 2020/4/27
     */
    private static void testFileUtils() {
        String strDir = "D:/test/test";
        File file = new File(strDir);
        try {
            FileUtils.deleteDirectory(file); // 删除目录
        } catch (IOException e) {
            log.info("catch IOException");
        }

        String strFile = "D:/test/test/test.txt";
        File reportFile = new File(strFile);
        try {
            String fileAsString = FileUtils.readFileToString(reportFile); // 把文件内容读入到字符串
            log.info(fileAsString);
        } catch (IOException e) {
            log.info("catch FileNotFoundException");
        }

        String strFile1 = "D:/test/test/test1.txt";
        String strFile2 = "D:/test/test/test2.txt";
        File source = new File(strFile1);
        File dest = new File(strFile2);
        try {
            FileUtils.copyFile(source, dest); // 把文件复制到一个新的位置
        } catch (IOException e) {
            log.info("catch FileNotFoundException");
        }

        File file1 = new File("D:/test/test/test1.txt");
        String writeText = "test";
        try {
            FileUtils.writeStringToFile(file1, writeText, Charset.forName("UTF-8")); // 把字符串写入文件
            FileUtils.write(file1, "test2", "utf-8"); // 把字符或者字节写入到文件,覆盖写
        } catch (IOException e) {
            e.printStackTrace();
        }

        File dir = new File("D:/test/test");
        if (!dir.exists()) {
            try {
                FileUtils.forceMkdir(dir); // 强制创建任务必需但不存在的父目录
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        otherFileUtils();
        listDir();
    }

    // 列式目录
    private static void listDir() {
        String dirName = "D:/test/test";
        File root = new File(dirName);
        Collection<File> files = FileUtils.listFiles(root, //列出指定目录下的所有文件
                new RegexFileFilter(".*."),
                DirectoryFileFilter.DIRECTORY);
        for (Iterator<File> iterator = files.iterator(); iterator.hasNext(); ) {
            Object obj = iterator.next();
            if (obj instanceof File) {
                File file = (File) obj;
                log.info(file.getName());
            }
        }
    }

    private static void otherFileUtils() {
        File src = new File("D:/test/test1");
        File dest = new File("D:/test/test2");
        try {
            FileUtils.copyDirectory(src, dest); // 将目录下的所有目录以及文件复制到新的目录。
        } catch (IOException e) {
            log.info("catch FileNotFoundException");
        }

        try {
            FileUtils.forceDelete(dest); // 强制删除文件或者目录及其所有子目录和文件
        } catch (IOException e) {
            log.info("catch FileNotFoundException");
        }

        String strFileNotExit = "D:/test/test3/test.txt";
        File testFile = new File(strFileNotExit);
        FileUtils.deleteQuietly(testFile); // 删除文件,但不抛出异常
    }

    /**
     * 功能描述:测试 FilenameUtils
     *
     * @author cakin
     * @date 2020/4/27
     */
    private static void testStringUtils() {
        // isBlank(),检查字符是否为空字符串"",或者 null,或者空格。
        log.info("result is " + StringUtils.isBlank(null));
        log.info("result is " + StringUtils.isBlank(""));
        log.info("result is " + StringUtils.isBlank(" "));
        log.info("result is " + StringUtils.isBlank("王小二"));
        log.info("result is " + StringUtils.isBlank("  王小二  "));

        // isNotBlank(),与 isBlank () 检查的结果相反。

        // isEmpty(),检查字符是否为空字符串 "",或者 null;和 isBlank () 不同,不包括空格的检查。
        log.info("result is " + StringUtils.isEmpty(null));
        log.info("result is " + StringUtils.isEmpty(""));
        log.info("result is " + StringUtils.isEmpty(" "));
        log.info("result is " + StringUtils.isEmpty("王小二"));
        log.info("result is " + StringUtils.isEmpty("  王小二  "));

        // isNotEmpty(),与 isEmpty () 检查的结果相反。

        // join(),将多个元素连接成一个字符串。
        log.info("result is " + StringUtils.join(null));
        log.info("result is " + StringUtils.join("王", "二小"));

        // equals(),比较两个字符序列是否相等。
        log.info("result is " + StringUtils.equals(null, null));
        log.info("result is " + StringUtils.equals(null, "王小二"));
        log.info("result is " + StringUtils.equals("王小二", null));
        log.info("result is " + StringUtils.equals("王小二", "王小二"));
        log.info("result is " + StringUtils.equals("cmower", "CMOWER"));

        // split(),把字符串拆分为数组,拆分符为空白字符。
        log.info("result is " + StringUtils.split(null));
        log.info("result is " + StringUtils.split(""));
        log.info("result is " + StringUtils.split("王小二 沉默王三"));
        log.info("result is " + StringUtils.split("王小二  沉默王三"));
        log.info("result is " + StringUtils.split(" 王小二 "));

        // replace(),替换另一个字符串中所有出现的字符串。
        log.info(StringUtils.replace(null, "*", "*"));
        log.info(StringUtils.replace("", "*", "*"));
        log.info(StringUtils.replace("any", null, "*"));
        log.info(StringUtils.replace("any", "*", null));
        log.info(StringUtils.replace("any", "", "*"));
        log.info(StringUtils.replace("王小二", "二", null));
        log.info(StringUtils.replace("王小二", "二", ""));
        log.info(StringUtils.replace("王小二", "二", "三"));
    }

    /**
     * 功能描述:测试FilenameUtils
     *
     * @author cakin
     * @date 2020/4/27
     */
    private static void testFilenameUtils() {
        // getExtension(),获取文件的扩展名。
        log.info(FilenameUtils.getExtension("test.txt"));
        log.info(FilenameUtils.getExtension("a/b/test.jpg"));
        log.info(FilenameUtils.getExtension("a/test.txt/c"));
        log.info(FilenameUtils.getExtension("a/b/c"));

        // getBaseName(),获取单纯的文件名或者路径名,文件时去掉路径和扩展名;路径时去掉父级路径。
        log.info(FilenameUtils.getBaseName("a/b/test.txt"));
        log.info(FilenameUtils.getBaseName("test.txt"));
        log.info(FilenameUtils.getBaseName("a/b/c"));
        log.info(FilenameUtils.getBaseName("a/b/c/"));

        // getName(),如果是文件时,获取文件名和后缀,去掉路径;如果是路径时,去掉父级路径。
        log.info(FilenameUtils.getName("a/b/test.txt"));
        log.info(FilenameUtils.getName("test.txt"));
        log.info(FilenameUtils.getName("a/b/c"));
        log.info(FilenameUtils.getName("a/b/c/"));

        // concat(),将路径和文件名连接在一起。
        log.info(FilenameUtils.concat("/foo/", "bar"));
        log.info(FilenameUtils.concat("/foo", "bar"));
        log.info(FilenameUtils.concat("/foo", "/bar"));
        log.info(FilenameUtils.concat("/foo", "C:/bar"));
        log.info(FilenameUtils.concat("/foo", "C:bar"));
        log.info(FilenameUtils.concat("/foo/a/", "../bar"));
        log.info(FilenameUtils.concat("/foo/", " ../../bar"));
        log.info(FilenameUtils.concat("/foo/", "/bar"));
        log.info(FilenameUtils.concat("/foo/.. ", "/bar"));
        log.info(FilenameUtils.concat("/foo", " bar/c.txt"));
        log.info(FilenameUtils.concat("/foo/c.txt", "bar"));

        // FilenameUtils.wildcardMatch(),检查文件名是否匹配指定的格式。
        log.info("is match:" + FilenameUtils.wildcardMatch("c.txt", "*.txt"));
        log.info("is match:" + FilenameUtils.wildcardMatch("c.txt", "*.jpg"));
        log.info("is match:" + FilenameUtils.wildcardMatch("a/b/c.txt", "a/b/*"));
        log.info("is match:" + FilenameUtils.wildcardMatch("c.txt", "*.???"));
        log.info("is match:" + FilenameUtils.wildcardMatch("c.txt", "*.????"));

        // separatorsToUnix(),将所有分隔符转换为正斜杠的 Unix 分隔符。
        log.info(FilenameUtils.separatorsToUnix("my/unix/filename"));

        // getFullPath(),获取文件的完整路径。
        log.info(FilenameUtils.getFullPath("C:\\a\\b\\c.txt"));
        log.info(FilenameUtils.getFullPath("~/a/b/c.txt"));
        log.info(FilenameUtils.getFullPath("a.txt"));
    }

    /**
     * 功能描述:测试ArrayUtils
     *
     * @param
     * @return
     * @author cakin
     * @date 2020/4/26
     */
    private static void testArrayUtils() {
        testContains();
        testAddAll();
        testClone();
        testSubarray();
        testToObject();
        testOther();
    }

    /**
     * Contains
     * 如果某个数组包含某个值就返回true
     * 否则返回false
     */
    public static void testContains() {
        String[] array = {"1", "2", "3"};
        log.info("{}", ArrayUtils.contains(array, "2"));
        log.info("{}", ArrayUtils.contains(array, "4"));
    }

    /**
     * AddAll
     * 创建出一个新的数组,原数组的值不变
     */
    public static void testAddAll() {
        String[] array1 = {"1", "2", "3"};
        String[] array2 = {"4", "5", "6"};
        String[] array3 = ArrayUtils.addAll(array1, array2);
        log.info(Arrays.toString(array3));
    }

    /**
     * 浅拷贝一个数组
     */
    public static void testClone() {
        String[] array1 = {"1", "2", "3"};
        String[] clone = ArrayUtils.clone(array1);
        log.info(Arrays.toString(clone));
    }

    /**
     * 截取某个数组的值
     * 从startIndexInclusive
     * 到endIndexExclusive
     * 即[)
     */
    public static void testSubarray() {
        String[] array = {"4", "5", "6"};
        String[] subarray = ArrayUtils.subarray(array, 0, NUM6);
        log.info(Arrays.toString(subarray));
    }

    /**
     * 将基本类型数组变成
     * 对应的引用类型数组
     */
    public static void testToObject() {
        int[] array = {NUM4, NUM5, NUM6};
        Integer[] array1 = ArrayUtils.toObject(array);
        for (Integer integer : array1) {
            log.info("{}", integer);
        }
    }

    private static void testOther() {
        String[] strs = null;
        // 检查数组是否为 null 或者没有元素
        if (ArrayUtils.isEmpty(strs)) {
            log.info("strs is null");
        }

        // 在数组中添加一个新的元素,原数组不变。
        String[] strNews = ArrayUtils.add(strs, "how are you");
        log.info(Arrays.toString(strNews));

        // 找出指定数组的下标
        String[] num = {"12", "34", "56"};
        log.info("{}", ArrayUtils.indexOf(num, "56"));
    }


    /**
     * 功能描述:测试DigestUtils
     *
     * @author cakin
     * @date 2020/4/26
     */
    private static void testDigestUtils() {
        // 计算字符串的 MD5 摘要,并返回 32 位的十六进制字符。
        log.info(DigestUtils.md5Hex("王小二"));
        // 计算字符串的 MD5 摘要,并返回 16 个元素的字节数组。
        log.info(Arrays.toString(DigestUtils.md5("王小二")));
    }

    /**
     * 功能描述:StringEscapeUtils测试
     *
     * @author cakin
     * @date 2020/4/26
     * @description: 使用pom如下
     */
    private static void testStringEscapeUtils() {
        // 反转义 HTML
        log.info(StringEscapeUtils.unescapeHtml4("&lt;div&gt;&lt;/div&gt;"));
        // 转义 HTML
        log.info(StringEscapeUtils.escapeHtml4("<div></div>"));
        // 转义 Java
        log.info(StringEscapeUtils.escapeJava("王二小"));
        // 反转义 Java
        log.info(StringEscapeUtils.unescapeJava("\\u738B\\u4E8C\\u5C0F"));
    }

    /**
     * 功能描述:测试BeanUtils工具类
     *
     * @author cakin
     * @date 2020/4/26
     * @description: Spring框架自带
     */
    private static void testBeanUtils() {
        EUser user1 = new EUser(1, "cakin");
        EUser user2 = new EUser();

        BeanUtils.copyProperties(user1, user2);
        log.info("user2 is" + user2);
    }
}

三 测试

2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 52 - *************************************************************************
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 115 - catch IOException
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 132 - catch IOException
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 54 - *************************************************************************
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 151 - catch IOException
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 160 - catch FileNotFoundException
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 170 - catch FileNotFoundException
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 216 - catch FileNotFoundException
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 222 - catch FileNotFoundException
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 205 - des1.txt
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 205 - test1.txt
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 56 - *************************************************************************
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 238 - result is true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 239 - result is true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 240 - result is true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 241 - result is false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 242 - result is false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 247 - result is true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 248 - result is true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 249 - result is false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 250 - result is false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 251 - result is false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 256 - result is null
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 257 - result is 王二小
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 260 - result is true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 261 - result is false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 262 - result is false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 263 - result is true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 264 - result is false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 267 - result is null
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 268 - result is [Ljava.lang.String;@7e0babb1
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 269 - result is [Ljava.lang.String;@6debcae2
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 270 - result is [Ljava.lang.String;@5ba23b66
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 271 - result is [Ljava.lang.String;@2ff4f00f
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 274 - null
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 275 -
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 276 - any
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 277 - any
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 278 - any
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 279 - 王小二
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 280 - 王小
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 281 - 王小三
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 58 - *************************************************************************
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 292 - txt
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 293 - jpg
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 294 -
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 295 -
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 298 - test
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 299 - test
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 300 - c
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 301 -
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 304 - test.txt
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 305 - test.txt
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 306 - c
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 307 -
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 310 - \foo\bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 311 - \foo\bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 312 - \bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 313 - C:\bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 314 - C:bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 315 - \foo\bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 316 - \foo\bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 317 - \bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 318 - \bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 319 - \foo\ bar\c.txt
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 320 - \foo\c.txt\bar
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 323 - is match:true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 324 - is match:false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 325 - is match:true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 326 - is match:true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 327 - is match:false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 330 - my/unix/filename
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 333 - C:\a\b\
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 334 - ~/a/b/
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 335 -
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 60 - *************************************************************************
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 362 - true
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 363 - false
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 374 - [1, 2, 3, 4, 5, 6]
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 383 - [1, 2, 3]
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 395 - [4, 5, 6]
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 406 - 4
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 406 - 5
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 406 - 6
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 414 - strs is null
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 419 - [how are you]
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 423 - 2
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 62 - *************************************************************************
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 435 - 6a0d311e640d9b57c353e328a7fa3099
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 437 - [106, 13, 49, 30, 100, 13, -101, 87, -61, 83, -29, 40, -89, -6, 48, -103]
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 64 - *************************************************************************
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 449 - <div></div>
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 451 - &lt;div&gt;&lt;/div&gt;
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 453 - \u738B\u4E8C\u5C0F
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 455 - 王二小
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 66 - *************************************************************************
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 470 - user2 isEUser(id=1, username=cakin)
2020-04-28 19:24:09 [main] INFO  toolutils.ToolUtils 68 - *************************************************************************

Process finished with exit code 0

四 参考

https://blog.csdn.net/qing_gee/article/details/104387940

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中,可以使用 Apache HttpComponents 组件来解析请求头中的 IP 地址。具体方法是使用 HttpComponents 中的 HttpClient 类来获取请求头,再从中获取 IP 地址。代码示例如下: ``` import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class Main { public static void main(String[] args) throws IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpGet request = new HttpGet("http://www.example.com"); CloseableHttpResponse response = client.execute(request); String ip = response.getFirstHeader("X-Forwarded-For").getValue(); System.out.println("IP address: " + ip); response.close(); client.close(); } } ``` 这段代码假设请求头中的 IP 地址位于 "X-Forwarded-For" 头部。如果请求头中的 IP 地址位于其他地方,可以调整代码中的 ".getFirstHeader("X-Forwarded-For")" 为相应的请求头部。 ### 回答2: 要解析请求头中的IP地址,可以使用Java开源类库来帮助完成。 在Java中,我们可以使用Servlet API中的HttpServletRequest对象来获取请求信息,包括请求头。可以通过调用HttpServletRequest对象的getHeader方法,传入"X-Forwarded-For"参数,来获取代理服务器传递的客户端IP地址。 然而,并不是所有的代理服务器都会添加"X-Forwarded-For"头部信息。因此,还可以使用Java开源类库来解析不同的请求头。 常用Java开源类库包括: 1. Apache Commons IO:可以使用该类库中的IOUtils类来读取和分析请求头的内容,获取IP地址。 2. Servlets API:Java Servlet规范中提供了HttpServletRequest对象,可以直接用它的getHeader方法来获取请求头信息。 3. Spring Framework:Spring框架提供了很多有用的类和方法,可以帮助我们解析请求头,包括获取IP地址。 这些开源类库都可以通过Maven或Gradle等构建工具导入到项目中,以便使用其中的类和方法。根据具体的需求和项目情况,选择适合的开源类库使用即可。 总的来说,Java开源类库为我们解析请求头中的IP地址提供了方便和灵活的方式。我们可以根据不同的需求和情况,选择合适的类库来使用。 ### 回答3: 要解析请求头中的ip地址,可以使用Java开源类库中的servlet相关类。 首先,需要获取HttpServletRequest对象,它包含了请求头的信息。可以在Servlet中通过参数或者ServletContext获取HttpServletRequest对象。 然后,通过HttpServletRequest对象的getHeader方法获得指定名称的请求头的值。IP地址一般存储在请求头中的"X-Forwarded-For"或者"X-Real-IP"字段中。 可以这样获取IP地址: ```java HttpServletRequest request = ...; // 获取HttpServletRequest对象 String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } ``` 上述代码首先尝试从"X-Forwarded-For"字段获取IP地址,如果获取不到,再尝试从"X-Real-IP"字段获取IP地址,最后从request.getRemoteAddr()获取IP地址。 这样就可以通过Java开源类库解析请求头中的IP地址了。注意,通过上述方式获取的IP地址可能是中间代理服务器的IP地址,而非真正的客户端IP地址,所以需要根据具体的环境和需求进行适配和判断。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值