Java 程序员都应该去使用一下这款强大的国产工具类库

时间:2022-06-19
本文章向大家介绍Java 程序员都应该去使用一下这款强大的国产工具类库,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

技术文章第一时间送达!

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>4.2.1</version>
</dependency>
public class Demo {

   public static void main(String[] args) {
       File file = new File("D:\face.jpg");

       // 第一种方式:自定义构建表单
       HttpRequest request = HttpRequest
               .post("http://ip:port/xxxx")
               .form("file", file)
               .form("fileType", "jpg");
       HttpResponse response = request.execute();
       System.out.println(response.body());

       // 第二种方式:使用统一表单,Http模块会自动识别参数类型,并完成上传
       HashMap<String, Object> paramMap = new HashMap<>();
       paramMap.put("author", "倪升武");
       paramMap.put("wechat", "程序员私房菜");
       String result = HttpUtil.post("http://ip:port/xxxx", paramMap);
       System.out.println(result);
   }
}
comment.setContent(HtmlUtil.encode(content));

它会转义文本中的HTML字符为安全的字符,这样比较安全。另外,HtmlUtil 还提供了以下方法,有兴趣的朋友可以去试一下。

CronUtil(定时任务)

/**
* 项目初始化配置
* @author shengwu ni
* @date 2018-12-06
*/
@Component
public class InitConfig {

   private static final Logger LOGGER = LoggerFactory.getLogger(InitConfig.class);

   @PostConstruct
   public void initTimer() {
       LOGGER.info("项目启动,开启Hutool定时任务……");
       CronUtil.setMatchSecond(true);
       CronUtil.start();
   }
}

zEND