Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of Java tool class library Hutool

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the functions of Java tool class library Hutool", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the functions of Hutool, the Java tool library?"

Brief introduction

Hutool homonym "confused", implying the pursuit of "everything is confused, there is no loss, no gain" realm.

Hutool is a Java toolkit and just a toolkit that helps us simplify every line of code, reduce every method, and make the Java language "sweet".

Hutool was originally a collation of the "util" package in my project, then gradually accumulated and added more non-business-related functions, and extensively learned the essence of other open source projects, and finally formed a rich open source toolset after self-collation and modification.

Function

A Java basic utility class that encapsulates JDK methods such as file, stream, encryption and decryption, transcoding, regularization, threading, XML, etc., to form various Util utility classes, and provides the following components:

Hutool-aop JDK dynamic proxy encapsulation, provides non-IOC aspect support hutool-bloomFilter Bloom filtering, provides some Hash algorithm Bloom filtering hutool-cache cache hutool-core core, including Bean operations, dates, various Util and other hutool-cron timing task modules, provides Crontab expression-like timing tasks hutool-crypto encryption and decryption module hutool-db JDBC encapsulated data operation Based on ActiveRecord idea hutool-dfa multi-keyword search hutool-extra expansion module based on DFA model, encapsulation of third-party encapsulation (template engine, mail, etc.) hutool-http based on HttpUrlConnection Http client encapsulation hutool-log automatic identification log implementation of log facade hutool-script script execution encapsulation For example, Javascripthutool-setting more powerful Setting configuration file and Properties encapsulation hutool-system system parameter call encapsulation (JVM information, etc.) hutool-json JSON implementation of hutool-captcha image verification code to achieve simple testing

These two days to use Hutool to replace some of the code in Halo, I have to say, it is very smooth to use, the following is a brief introduction to some of the Hutool tool classes I used.

It is easy to use, only need to introduce the configuration.

Cn.hutool hutool-all 5.3.9SecureUtil (encryption and decryption tool)

It is mainly used when logging in and when changing the password, because the password in the database is encrypted by md5, so you need to encrypt it first and then query the database when you log in. If you use Hutool, you only need to call the md5 method in SecureUtil.

User = userService.userLoginByName (loginName,SecureUtil.md5 (loginPwd)); HtmlUtil (HTML utility class)

This utility class is more powerful, but the one I use most in Halo is HtmlUtil.encode, which can convert some characters to safe characters to prevent xss injection and SQL injection, such as the following comment submission.

Comment.setCommentAuthor (HtmlUtil.encode (comment.getCommentAuthor ()

This is to prevent a little villain from deliberately writing some executable js code and then submitting comments, which will be executed in the background panel, which is dangerous. You can use the encode method to convert the tag to, so that the js code will not be executed.

In addition, HtmlUtil also provides the following methods, which you can try if you are interested.

HtmlUtil.restoreEscaped restores escaped HTML special characters HTML characters in escaped text to safe characters HtmlUtil.cleanHtmlTag clears all HTML tags HtmlUtil.removeHtmlTag clears specified HTML tags and content surrounded by tags HtmlUtil.unwrapHtmlTag clears specified HTML tags, excluding content HtmlUtil.removeHtmlAttr removes attributes in HTML tags HtmlUtil.removeAllHtmlAttr removes all attributes of specified tags HtmlUtil.filter filters HTML text, prevents XSS attacks on CronUtil (scheduled task)

This tool is even more powerful, it doesn't need a framework like quartz to do timing tasks, and CronUtil doesn't need any other dependencies, just build a configuration file under resources, and then start scheduled tasks when the program starts, such as Halo's scheduled backup function (backed up at 1: 00 a. M. every day).

Cron.setting:

Cc.ryanc.halo.web.controller.admin.BackupController.backupResources = 0 01 * *?

Cc.ryanc.halo.web.controller.admin.BackupController.backupDatabase = 0 01 * *?

Cc.ryanc.halo.web.controller.admin.BackupController.backupPosts = 0 01 * *?

@ Override

Public void onApplicationEvent (ContextRefreshedEvent event) {

This.loadActiveTheme ()

This.loadOptions ()

This.loadFiles ()

This.loadThemes ()

/ / start a scheduled task

CronUtil.start ()

Log.info ("scheduled task started successfully!")

} at this point, I believe you have a deeper understanding of "what are the functions of the Java tool class library Hutool?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report