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

How to use the Java tool class library hutool

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use Java tool class library hutool". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Brief introduction

Hutool is a self-coined word of Hu + tool. The former pays tribute to my "ex-company", while the latter means tool, homonym "muddle-headed", implying the pursuit of the realm of "everything is confused, no loss, no gain".

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, providing section support under non-IOC

Hutool-bloomFilter Bloom filtering, which provides some Hash algorithm Bloom filtering

Hutool-cache caching

Hutool-core core, including Bean operations, dates, various Util, etc.

Hutool-cron timing task module, which provides timing tasks for Crontab-like expressions

Hutool-crypto encryption and decryption module

Data operation after hutool-db JDBC encapsulation, based on ActiveRecord idea

Hutool-dfa Multi-keyword search based on DFA Model

Hutool-extra extension module, encapsulation for third parties (template engine, mail, etc.)

Http client Encapsulation of hutool-http based on HttpUrlConnection

Hutool-log automatically identifies the log facade implemented by the log

Hutool-script script execution wrapper, such as Javascript

Hutool-setting more powerful Setting configuration files and Properties encapsulation

Hutool-system system parameter call encapsulation (JVM information, etc.)

Hutool-json JSON implementation

Implementation of hutool-captcha Picture CAPTCHA

Simple test

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.

SecureUtil (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 HtmlUtil.encode escaped text are safe characters

HtmlUtil.cleanHtmlTag clears all HTML tags

HtmlUtil.removeHtmlTag clears the specified HTML tags and content surrounded by tags

HtmlUtil.unwrapHtmlTag clears the specified HTML tag, excluding content

HtmlUtil.removeHtmlAttr removes attributes from HTML tags

HtmlUtil.removeAllHtmlAttr removes all attributes from the specified tag

HtmlUtil.filter filters HTML text to prevent XSS attacks

CronUtil (scheduled tasks)

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 scheduled task CronUtil.start (); log.info ("scheduled task started successfully!") ;} "how to use the Java tool class library hutool" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report