[关闭]
@Cesar 2016-05-30T05:30:27.000000Z 字数 1650 阅读 2163

Spring中好用的工具类

Spring 学习

请求工具类

地址:org.springframework.web.bind.ServletRequestUtils

  1. //取请求参数的整数值:
  2. public static Integer getIntParameter(ServletRequest request, String name)
  3. public static int getIntParameter(ServletRequest request, String name, int defaultVal) -->单个值
  4. public static int[] getIntParameters(ServletRequest request, String name) -->数组

还有譬如long、float、double、boolean、String的相关处理方法。

字符串工具类

地址:org.springframework.util.StringUtils

  1. //首字母大写:
  2. public static String capitalize(String str)
  3. //首字母小写:
  4. public static String uncapitalize(String str)
  5. //判断字符串是否为null或empty:
  6. public static boolean hasLength(String str)
  7. //判断字符串是否为非空白字符串(即至少包含一个非空格的字符串):
  8. public static boolean hasText(String str)
  9. //获取文件名:
  10. public static String getFilename(String path) //如e.g. "mypath/myfile.txt" -> "myfile.txt"
  11. //获取文件扩展名:
  12. public static String getFilenameExtension(String path) //如"mypath/myfile.txt" -> "txt"

还有譬如数组转集合、集合转数组、路径处理、字符串分离成数组、数组或集合合并为字符串、数组合并、向数组添加元素等。

对象序列化与反序列化

地址:org.springframework.util.SerializationUtils

  1. public static byte[] serialize(Object object)
  2. public static Object deserialize(byte[] bytes)

数字处理

地址:org.springframework.util.NumberUtils
字符串转换为Number并格式化,包括具体的Number实现类,如Long、Integer、Double,字符串支持16进制字符串,并且会自动去除字符串中的空格:

  1. public static <T extends Number> T parseNumber(String text, Class<T> targetClass)
  2. public static <T extends Number> T parseNumber(String text, Class<T> targetClass, NumberFormat numberFormat)

各种Number中的转换,如Long专为Integer,自动处理数字溢出(抛出异常):

  1. public static <T extends Number> T convertNumberToTargetClass(Number number, Class<T> targetClass)

文件复制

地址:org.springframework.util.FileCopyUtils
流与流之间、流到字符串、字节数组到流等的复制

目录复制

地址:org.springframework.util.FileSystemUtils
递归复制、删除一个目录

MD5加密

地址:org.springframework.util.DigestUtils
字节数组的MD5加密

  1. public static String md5DigestAsHex(byte[] bytes)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注