In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Java class library Hutool and API how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java class library Hutool and API how to use the article will have a harvest, let's take a look.
Preface
Hutool is a small and comprehensive Java tool class library, encapsulated by static methods, reduce the learning cost of related API, improve work efficiency, make Java have functional language-like elegance, so that Java language can also be "sweet".
The tools and methods in Hutool come from the meticulous craftsmanship of each user. It covers all aspects of the underlying code of Java development. It is not only a sharp tool for solving small problems in large-scale project development, but also an efficient role in small projects.
Hutool is a friendly alternative to the "util" package in the project. It saves developers time to encapsulate common classes and tools in the project, makes developers focus on business, and minimizes the bug caused by imperfect encapsulation.
Some common tool classes in HuTool
We introduce this tool class is very simple, directly in the Mavan project to introduce dependency, if you are a beginner, go to Maven Repository to download jar package can also be used.
Cn.hutool hutool-all 5.3.5
Through Hutool, you can reduce the cost of code search and avoid bug caused by uneven code on the network.
Date related API
We know that the use of API on dates in Java has always been tedious, from Date to Calendar, are not so developer-friendly, and it is easy to produce BUG, so Hutool starts with dates, providing wrappers for Date and Calendar objects in JDK.
For example, SimpleDateFormat (yyyy-MM-dd HH:mm:ss) is generally used to format the current Date date, but the SimpleDateFormat class is thread-unsafe, while Hutool provides a FastDateFormat class that provides thread-safe formatting and date string parsing support for Date objects. This object does not need to be perceived in actual use, and the relevant operations have been encapsulated in the relevant methods of DateUtil and DateTime.
There are other dates API as follows:
DateUtil provides a series of static methods for date-time operations
DateTim provides wrappers similar to date-time objects in Joda-Time, inherits from the Date class, and provides richer object methods.
DateBetween calculates classes with two time intervals, and in addition to constructing new objects, related operations are also encapsulated in the relevant methods of DateUtil and DateTime.
TimeInterval is a simple timer class, which is often used to calculate the execution time of a piece of code, including milliseconds, seconds, minutes, hours, days, weeks, and so on. The static construction of the object has been encapsulated in DateUtil.
DatePattern provides common date formatting modes, including String type and FastDateFormat type.
Random tool
Random tools should be the most commonly used class libraries in our development, and Hutool also encapsulates the commonly used API.
RandomUtil.randomInt gets random numbers in a specified range
RandomUtil.randomBytes random bytes
RandomUtil.randomEl randomly gets the elements in the list
RandomUtil.randomEleSet randomly gets a certain amount of non-repeating elements in the list and returns Set
RandomUtil.randomString gets a random string (only numbers and characters)
RandomUtil.randomNumbers gets a string that contains only numbers
RandomUtil.randomUUID random UUID
RandomUtil.weightRandom weight random generator, pass in the object with weight, and then get the object randomly according to the weight
Picture tool
Encapsulate the image processing in awt, including zooming, cropping, turning to black and white, adding watermark and so on.
Color to black and white / / black and white conversion ImgUtil.gray (FileUtil.file ("d:/logo.png"), FileUtil.file ("d:/result.png")) Add text watermark ImgUtil.pressText (/ / FileUtil.file ("e:/pic/face.jpg"), / / FileUtil.file ("e:/pic/test2_result.png"), / / "copyright", Color.WHITE, / / text new Font ("boldface", Font.BOLD, 100), / / font 0, / / x coordinate correction. By default in the middle, the offset is relative to the intermediate offset 0, / / y coordinate correction value. The default is in the middle, and the offset is relative to the intermediate offset 0.8f// transparency: alpha must be a floating point number within the range [0.0,1.0] (including boundary values); encryption and decryption tool
There are generally three types of encryption:
Symmetric encryption (symmetric), such as AES, DES, etc.
Asymmetric encryption (asymmetric), such as RSA, DSA, etc.
Summary encryption (digest), such as MD5, SHA-1, SHA-256, HMAC, etc.
Hutool provides corresponding encapsulation methods for the above encryption.
Bloom filter
The Bloom filter can be used to retrieve whether an element is in a collection. Its advantage is that the space efficiency and query time are far higher than the general algorithm, and the disadvantage is that it has a certain error recognition rate and deletion difficulties. It is generally used to solve the cache penetration problem of NoSQL.
/ / initialize BitMapBloomFilter filter = new BitMapBloomFilter (10); filter.add ("123"); filter.add (" abc "); filter.add (" ddd "); / / find the filter.contains (" abc ") mail tool
Sending mail in Java mainly depends on javax.mail package, while native API is cumbersome to use. Hutool still depends on javax.mail package on the basis of supporting mail delivery.
Javax.mail mail 1.4.7
Complete configuration of the mail server:
# SMTP address of mail server, optional, default is smtp.host = SMTP port of smtp.yeah.net# mail server, optional, default is 25port = 2 "sender (must be correct, otherwise sending fails) from = hutool@yeah.net# username, default is sender mailbox prefix user = hutool# password (note that some mailboxes need to set authorization codes separately for SMTP service, see related help for details) pass = q1w2e3
To send an email:
Send a plain text message. The last parameter is optional whether to add multiple attachments:
/ / plain text MailUtil.send ("hutool@foxmail.com", "Test", "message from Hutool Test", false)
Send a message in HTML format with an attachment. The last parameter is optional whether to add multiple attachments:
/ MailUtil.send with HTML format ("hutool@foxmail.com", "test", "message from Hutool test", true, FileUtil.file ("d:/aaa.xml"))
Group email, optional HTML or plain text, optional multiple attachments:
/ ArrayList tos = CollUtil.newArrayList ("person1@bbb.com", "person2@bbb.com", "person3@bbb.com", "person4@bbb.com"); MailUtil.send (tos, "Test", "email from Hutool Mass Test", false); HTML tool
For example, clear the specified tag of HTML text in a rich text editor, or keep only plain text.
HtmlUtil.cleanHtmlTag
Clear all HTM L tags, but retain the contents of the tags.
String str = "pre dfdsfdsfdsfBBBB"; / / result: pre dfdsfdsfdsfBBBBString result = HtmlUtil.cleanHtmlTag (str); HtmlUtil.removeHtmlTag
Clears the specified HTML tag and the content surrounded by the tag
String str = "pre
"; / / result: preString result = HtmlUtil.removeHtmlTag (str," img "); filter HTML text to prevent XSS attacks
Needless to say, this effect prevents users from writing HTML tags in the input text to cause attacks on the background.
String html = ""; / / the result is: "" String filter = HtmlUtil.filter (html); JSON tool
Since there are Fastjson tools that are open source and being maintained all the time, I won't go into details about the JSON tools in Hutool here, but use them in a similar way.
This is the end of the article on "Java's class library Hutool and how to use API". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Hutool of Java and how to use API". If you want to learn more, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.