Introduce a super powerful domestic Java tool — Hutool. Hutool is a Java toolkit class library that encapsulates JDK methods such as files, streams, encryption and decryption, transcoding, re, threads, XML, etc., forming various Util utility classes. Suitable for many projects as well as Web development, and has no coupling with other frameworks.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
1. Hutool time tool — DateUtil
1.1 Introduction to Time and date
DateUtil
Provides a series of static methods for date-time operationsDateTime
Provides encapsulation of date-time objects similar to those in Joda-time, inheriting DateFastDateFormat
Provides thread-safe formatting and Date string parsing support for Date objects. This object does not need to be sensed in practice, and the related operations are encapsulated in the related methods of DateUtil and DateTime.DateBetween
Classes that evaluate two time intervals are used by constructing new objects, and the related operations are encapsulated in the related methods of DateUtil and DateTime.TimeInterval
A simple timer class used to calculate the execution time of a piece of code. It provides calculations of the elapsed time in milliseconds, seconds, minutes, hours, days, weeks, etc. The static construction of the object is encapsulated in DateUtil.DatePattern
Provides common date formatting modes, including String and FastDateFormat.
The DateUtil, DateTime, and DatePattern classes are the main classes you need to know to handle most date and time operations.
1.2 Time tool class DateUtil
DateUtil is all about static methods. Here are some simple ones;
now():String // Current date time YYYY-MM-DD hh: MM :ss
today():String // Today date YYYY-MM-DD
date():DateTime
/* The current DateTime object (equivalent to new DateTime() or new Date()), in addition to providing an overloaded method that passes the argument of type long, is given a Unix timestamp and returns the time of the timestamp. * /
lastWeek():DateTime // Last week today (7 days before)
lastMonth():DateTime // Last month today (the month before that)
nextWeek():DateTime // Next week today (7 days later)
nextMonth():DateTime // Next month today (the month after that)
yesterday():DateTime // At the same time yesterday
tomorrow():DateTime // At the same time tomorrow
currentSeconds():long / / the number of milliseconds
thisYear():int / / year
thisMonth():int // month (starting from 0)
thisWeekOfMonth():int // Weekly this month (starting from 1)
thisWeekOfYear():int // Weekly of current year (starting from 1)
thisDayOfMonth():int // Day of the month (starting from 1)
thisDayOfWeek():int // Day of the week (starting from 1)
thisHour(boolean is24HourClock):int // The current hour
thisMinute():int / / the current points
thisSecond():int / / the second
Copy the code
1.3 Conversion between dates and strings
1.3.1 Parsing date Strings
Add some fixed-format strings –>Date objects:
Yyyy-mm-dd hh: MM: SS YYYY-MM-DD hh: MM: SS YYYY-MM-DD hh: MM: SS YYYY-MM-DD hh: MM: SS YYYY-MM-DD hh: MM: SS YYYY-MM-DD hh: MM parseDateTime parseDate ParseTime DateUtil.parse() DateUtil.parse(String,String)// Convert Date to a Date object of the specified format
Copy the code
1.3.2 Formatting the Date
To formatDateTime output, Hutool provides several methods: dateutil. formatDateTime(Date Date):String// The format string YYYY-MM-DD hh: MM :ss is returned
DateUtil.formatDate(Date date):String // The format string yyyY-MM-DD is returned
DateUtil.formatTime(Date date):String // The format string hh:mm:ss will be returned
DateUtil.format(Date,String):String // Returns a string of the specified format
Copy the code
1.4 Start and End time
Hutool can easily get the start and end of a day/month/year:
BeginOfDay (Date):Date Start time endOfDay(Date):Date end time beginOfMonth(Date):Date
endOfMonth(Date):Date
beginOfYear(Date):Date
endOfYear(Date):Date getBeginTimeOfDay Obtains the start time of the specified day. The start time is 00:00. GetEndTimeOfDay Obtains the end time of the specified day.Copy the code
To offset a specified date, use offsiteDay, offsiteWeek, and offsiteMonth to obtain the offset day, week, and month. The offset is positive to the future and negative to the past. If the above does not satisfy the offset requirements, use offsiteDate offset to set the amount, where the calendarField parameters are the granularity of the offset (hour, day, month, etc.) using the constants in the Calendar class.
1.5 Time Interval
1Dateutil. between(Date begin,Date end,DateUnit):longTo calculate the interval, the method takes three parameters, the first two are the start time and the end time, and the third parameter is the enumeration of DateUnit, which represents the difference in units. The value of DateUnit can be dateUnit. DAY (DAY), dateUnit. HOUR (HOUR), dateUnit. WEEK (WEEK), dateUnit. SECOND (SECOND),DateUnit.2The formatBetween method can also convert the difference to a specified format, such as XX days, XX hours, XX minutes, and XX seconds. Dateutil. formatBetween(Date begin,Date end,Level):String. The first two parameters are the start and end times, and the third parameter is the precision, such as level. SECOND. The format is XX days, XX hours, XX minutes, and XX seconds.3The.diff method returns the time difference between two dates. The diffField parameter defines the units of this difference, which are defined in DateUtil constants, for example dateutil. SECOND_MS indicates the number of seconds between two dates.Copy the code
1.6 Calculating age
With Hutool you can calculate your age instantly. What do you need to worry about? No, whatever Hutool says. We can use the following method:
age(Date begin,Date end):intAge of birth and death (String birthday):intCalculate the age of the current Date (Date birthday):intCalculates the age to the current dateCopy the code
1.7 Determine leap years
Use dateutil. isLeapYear(in year): Boolean to check whether it is a leap year.
1.8 a DateTime
Now that you know DateUtil, it should be easy to look at DateTime, because most of the methods in DateTime are similar to DateUtil. The DateTime class inherits from java.util.Date, which is a complete alternative to the use of Date and has other useful methods!
Let’s start with the DateTime constructor:
Method 1: Usenew
DateTime dt=new DateTime(); // Create the current moment
DateTime dt=new DateTime(Date date); // Use the Date object construct
DateTime dt=new DateTime(long timeMillis); // Construct with millisecondsDateTime dt= datetime.of (); DateTime dt= datetime.now ();// Create the current momentThere are two useful DateTime methods: before(Date when):booleanAnd after (the Date the when) :boolean, they can tell the order of time.Copy the code
1.9 DatePattern
Finally, take a look at the DatePattern class, which provides some specified date and time formats (all strings), including locale representations:
Format template: datepattern.chinese_date_pattern// YYYY MM month DD date
DatePattern.NORM_DATE_PATTERN //yyyy-MM-dd
DatePattern.NORM_TIME_PATTERN //HH:mm:ss
DatePattern.NORM_DATETIME_PATTERN //yyyy-MM-dd HH:mm:ss
DatePattern.UTC_PATTERN //yyyy-MM-dd'T'HH:mm:ss'Z'
DatePattern.PURE_DATE_PATTERN //yyyyMMdd
DatePattern.PURE_TIME_PATTERN //HHmmss
DatePattern.PURE_DATETIME_PATTERN //yyyyMMddHHmmss
Copy the code
Lu Xiaolei, "A Genius pig on Planet 612" Source: https://blog.csdn.net/tianc_pig/article/details/87826810 https://my.oschina.net/looly/blog/268552 CSDN oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2. Hutool string tool — StrUtil
String instruments refers to cn. Hutool. Core. Util. StrUtil classes, including a variety of String method encapsulation and provides other convenient and practical method. Methods in StrUtil are static methods.
2.1 Check whether multiple Strings are empty
Null has two meanings: null or “” (an empty string), and invisible strings, such as whitespace (” “). Hutool provides different solutions for both cases. Similar to StringUtil in the Apache Commons Lang, StrUtil is used instead of StringUtil because it is shorter, and Str is a shorthand THAT I think is well established. Common methods, such as isBlank, isNotBlank, isEmpty, isNotEmpty, etc., will not be introduced.
The common methods are as follows:
isBlank(CharSequence arg0):boolean // Check whether the string is null or "" or invisible string
isEmpty(charSequence arg0):boolean // Check whether the string is null or ""HasBlank (CharSequence... arg0):boolean // Check whether there are null or "" or invisible strings in multiple stringsHasEmpty (CharSequence... arg0):boolean // Check whether there is null or "" in multiple strings.The hasEmpty() method is often used when logging in to forms.Copy the code
2.2 sub way
The sub() method is equivalent to the subString() method; There are three arguments: the first is the truncated string, and the last two are the beginning and end positions. One of the biggest features of the sub() method is that it has a very high fault tolerance, and -1 represents the last character, -2 represents the penultimate character, and so on. It can also be reversed.
String testStr="my name is smartPig";
String result01=StrUtil.sub(testStr, 0.4); //my n
/* it's 4 minus 0, but it's actually 0 minus 4*/
String result02=StrUtil.sub(testStr, 4.0); //my n
String result03=StrUtil.sub(testStr, -1.3); //name is smartPi
String result04=StrUtil.sub(testStr, -4.3); //name is smar
Copy the code
2.3 the format method
The format method is similar to the one in PreparedStatement in JDBC. Placeholders, you can use "{}" as placeholders in strings. String testStr="{} name is smart{}";
String result=StrUtil.format(testStr, "my"."Pig"); //my for the first {}, Pig for the second {}
My name is smartPig
Copy the code
2.4 Remove prefixes and suffixes
Hutool can remove string prefixes/suffixes, which is great for removing file suffixes. The relevant methods are as follows:
removeSuffix(CharSequence str, CharSequence suffix) // remove the suffix
removeSuffixIgnoreCase(CharSequence str, CharSequence suffix) // Remove suffixes and ignore case
removePrefix(CharSequence str, CharSequence suffix) // Remove the prefix
removePrefixIgnoreCase(CharSequence str, CharSequence suffix) // Remove prefix, ignore case
Copy the code
2.5 String Constants
Hutool defines some character constants that can be used flexibly. Some constants are as follows:Copy the code
StrUtil.DOT / / points.
StrUtil.DOUBLE_DOT / / two point..
StrUtil.UNDERLINE // Underline _
StrUtil.EMPTY / / rung _
StrUtil.BACKSLASH // backslash \
StrUtil.DASHED / / en -
StrUtil.BRACKET_END // right center extension]
StrUtil.BRACKET_START // Open brackets [
StrUtil.COLON / / the colon:
StrUtil.COMMA / / a comma,
StrUtil.DELIM_END // Close curly brace}
StrUtil.DELIM_START // Open curly brace {
Copy the code
2.6 String Reverse Order
Hutool makes it easy to reverse string order. You can use the strutil.reverse (String STR):String method.Copy the code
2.7 Encode and decode methods
GetByte (String charsetName) (String charsetName) (String charsetName) (String charsetName) (String charsetName) (String charsetName) (String charsetName) (String charsetName) (String charsetName) (String charsetName) (String charsetName) Package a try to throw a runtime exception, province I must in processing the disgusting UnsupportedEncodingException business code.Copy the code
Lu Xiaolei, "A Genius pig on Planet 612" Source: https://blog.csdn.net/tianc_pig/article/details/87944463 https://my.oschina.net/looly/blog/262775 CSDN oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
3. Hutool — RandomUtil
The main methods of random tools are as follows:
RandomUtil. RandomInt gets a random number in the specified range RandomUtil. RandomEle gets an element in the list RandomUtil. RandomString gets a randomString (only numbers and characters) RandomUtil. RandomNumbers gets a string containing only numbers. RandomNumber gets a randomNumber RandomUtil. RandomChar gets a random characterCopy the code
Each of these methods has some overloaded methods, as shown in the following table:
randomInt():int // Get a random integer
randomInt(int limit):int // Get a random integer
randomInt(int min,int max):int // Get a random integer n(min<=n< Max)
randomChar():char // Get a random character
randomChar(Strint str):char // Gets a random character from the specified string
randomNumber():int // Get a random number
randomNumbers(int length):String // Get a numeric string of length
randomString(int length):String // Get a string of length containing only numbers and characters
randomString(String str,int length):String // Generate a random string of the specified length from STR (all characters come from STR)
randomEle(T array[]):T // Get a random number from the array
randomEle(List<T> list):T // Get a random item from the List
randomEle(List<T> list,int limit):T // Get a random item from the List before the limit (limit starts at 1)
Copy the code
The author: "612 star" a genius pig text links: https://blog.csdn.net/tianc_pig/article/details/88034976 source: CSDNCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
4. Hutool’s re tool — ReUtil
4.1 Checking whether a match is detected
Use isMatch(String regex,CharSequence Content): Boolean The first argument is the regex, and the second argument is the matched content.Copy the code
4.2 Deleting matched Content
delFirst(String regex,CharSequence content):String // Delete the first matching string, return the deleted string
delAll(String regex,CharSequence content):String // Delete all matching strings and return the deleted string
Copy the code
4.3 Get the matching content
FindAll (String regex, CharSequence Content, int group): List<String> to get all matchesCopy the code
4.4 Replacing matched Content
Use replaceAll(CharSequence Content, String regex, String replacementTemplate):String can replace matched content.Copy the code
4.4 Automatically Escaping Special Characters
Escape (CharSequence arg0):String can be used to escape special characters in regular expressions to escape characters.Copy the code
The author: "612 star" a genius pig text links: https://blog.csdn.net/tianc_pig/article/details/88134882 source: CSDNCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
5. Hutool’s pagination tool — PageUtil
Static method of PageUtilCopy the code
transToStartEnd(int.int) :int[] // Convert the page count and page capacity to the starting position in the data table
/* The first argument is the number of pages, the second argument is the capacity of each page, and the return value is an array of length 2, representing the start and end positions respectively. * /
@param pageNum Number of pages (starting from 1) @param pageSize Capacity per page int startEnd[]= pageutil. transToStartEnd(pageNum, pageSize); * /
totalPage(int.int) :int // Get the number of pages from the total number of records and the page size
The first argument is the total number of records, the second argument is the capacity per page, and the return value is the total number of pages.
/* @param totalPage @param pageSize int totalPage = pageutil. totalPage(totalCount, pageSize); * /Rainbow paging algorithm rainbow(int.int) :int[]
// The first parameter indicates the current page number, and the second parameter indicates the total number of pages, in which case all pages are displayed at once by default
// (this method is personally not useful)
rainbow(int.int.int) :int[]
// The first parameter indicates the current page number, the second parameter indicates the total number of pages, and the third parameter indicates the maximum number of pages displayed at a time
Copy the code
Rainbow paging algorithm is used in the case of “previous page” 1, 2, 3, 4, 5 “Next page” often displayed on a web page. Generally, the page number displayed is dynamically updated according to the current page number, because the number of page numbers displayed each time is fixed.
/** * Test paging rainbow algorithm *@paramCurPage Number of current pages *@paramTotalPage totalPage */
public static String rainbowPage(int curPage,int totalPage) {
int rainbow[]=PageUtil.rainbow(curPage,totalPage);
return Arrays.toString(rainbow);
}
/** * Test paging rainbow algorithm *@paramCurPage Number of current pages *@paramTotalPage totalPage *@paramSize Number of pages displayed at a time */
public static String rainbowPage(int curPage,int totalPage,int size) {
int rainbow[]=PageUtil.rainbow(curPage,totalPage,size);
return Arrays.toString(rainbow);
}
Copy the code
MyBatis provides a pagination tool and supports plug-in pagination
The author: "612 star" a genius pig text links: https://blog.csdn.net/tianc_pig/article/details/88628323 source: CSDNCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Collection tool — CollectionUtil
Mainly added to the array, collection class operations.
6.1 the join method
To convert a collection to a string, this method is quite common, and is the antithesis of strutil.split. This method takes arguments that support a collection of objects of various types, and finally calls its toString() method when each object is connected. As follows:
String[] col= new String[]{a,b,c,d,e};
String str = CollectionUtil.join(col, "#"); //str -> a#b#c#d#e
Copy the code
6.2 sortPageAll, sortPageAll2 methods
A given set is placed into a List, sorted by the given Comparator, and paged to fetch the data. This approach is very similar to sorting paging after multiple table queries in a database, and that’s why it exists. SortPageAll2 and sortPageAll use the same method and result, the difference is that sortPageAll2 uses BoundedPriorityQueue to store the combined list. We don’t know which one is better, so we keep them. As follows:
Comparator<Integer> comparator = new Comparator<Integer>(){ / / Integer comparator
@Override
public int compare(Integer o1, Integer o2) {
returno1.compareTo(o2); }};/ / new three lists, CollectionUtil newArrayList method said new ArrayList and populate the elements
List<Integer> list1 = CollectionUtil.newArrayList(1.2.3);
List<Integer> list2 = CollectionUtil.newArrayList(4.5.6);
List<Integer> list3 = CollectionUtil.newArrayList(7.8.9);
Select * from list1,list2,list3; select * from list1,list2,list3;
@SuppressWarnings("unchecked")
List<Integer> result = CollectionUtil.sortPageAll(0.2, comparator, list1, list2, list3);
System.out.println(result); / / output [1, 2]
Copy the code
6.3 sortEntrySetToList method
It mainly sorts Entry
according to the Value of Value, which has great limitations
6.4 popPart method
This method passes in a stack object and pops a specified number of elements, meaning the pop() method, which is removed from the original stack.
6.5 newHashMap, newHashSet, and newArrayList methods
Create a new data structure. The type of the data structure element depends on the type of your variable, as follows:
HashMap<String, String> map = CollectionUtil.newHashMap();
HashSet<String> set = CollectionUtil.newHashSet();
ArrayList<String> list = CollectionUtil.newArrayList();
Copy the code
6.6 append method
Add () adds an element to the end of a given array. This method exists only to add a small number of elements, because system. arrayCopy is used internally and copies the array every time it is called. This method is also intended for use in cases where only arrays are available, eliminating the need to convert to List, add elements, and then to Array.
6.7 the resize method
Resize the data, truncate it if it is smaller, or leave the extra space empty if it is larger. (It seems that List will use a similar method when expanding)
6.8 addAll method
Combine multiple data into an array
6.9 range method
This method comes from a Python syntactic sugar that generates an arithmetic sequence (list) given a start and end and a step.
int[] a1 = CollectionUtil.range(6); / /,1,2,3,4,5 [0]
int[] a2 = CollectionUtil.range(4.7); / / (4 and 6)
int[] a3 = CollectionUtil.range(4.9.2); / /,6,8 [4]
Copy the code
6.10 sub way
Section of the collection, other types of collections will be converted to a List, enclosed List. The subList method, automatic correction cross-border problems, completely avoid IndexOutOfBoundsException anomalies.
6.11 isEmpty and isNotEmpty methods
Determines whether a collection is empty (including null and collections with no elements).
6.12 zip method
This method is also derived from a Python syntactic sugar: given two sets of objects, the elements of the two sets correspond to each other to form a Map. This method also has an overloaded method that can pass characters, and then given a delimiter, the string is split into a list.
String[] keys = new String[]{"a"."b"."c"};
Integer[] values = new Integer[]{1.2.3};
Map<String, Integer> map = CollectionUtil.zip(keys,values);
System.out.println(map); // {b=2, c=3, a=1}
String a = "a,b,c";
String b = "1, 2, 3";
Map<String, String> map2 = CollectionUtil.zip(a,b, ",");
System.out.println(map2); // {b=2, c=3, a=1}
Copy the code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/262786 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
7. Processing tools like Hutool — ClassUtil
The most useful method in this class is the scanPackage method, which scans all classes in the classpath. This is one of Spring’s features and is the basis for class scanning in the Hulu framework
7.1 scanPackage method
The only argument to this method is the name of the package, and the result is the package and all classes under the subpackage. The method is simple to use, but the process is more complicated. The package scan first calls the getClassPaths method to get the ClassPath, and then scans the ClassPath or, if it is a directory, the class files in the directory, or the JAR files. If it is a JAR package, get the class name directly from the JAR package. The obvious purpose of this method is to find all classes, in Spring for dependency injection, and in Hulu for Action classes. Of course, you can also pass a ClassFilter object to filter unwanted classes.
7.2 getMethods method
This method is the same as the Class object’s ·getMethods· method, but only returns the name of the method (string). Encapsulation is very simple.
7.3 getClassPaths method
This method gets the currentThread’s ClassPath, centered on a call to thread.currentthread ().getcontextclassloader ().getresources.
7.4 getJavaClassPaths method
This method is used to get the ClassPath defined by Java’s system variables.
7.5 the parse method
This method encapsulates the casting method. First, the cast method of the object itself is called. If it fails, the cast method attempts whether the object is a primitive type (int,long,double,float, etc.). Save us the tedious of trying many times without knowing the type.
7.6 parseBasic method
This method is called by the parse method and is specifically used to convert character set strings to objects of primitive types (Integer,Double, and so on). This is a one-stop-shop conversion method. The JDK’s method names are awkward. For example, if you want to convert to Long, you need to call long. parseLong. I don’t get it. That’s where this comes in.
7.7 castToPrimitive method
This is an awkward way to change a class such as Integer to int.class, but it doesn’t seem to have much use, so if you can use it, that’s great.
7.8 getClassLoader and getContextClassLoader methods
The latter simply gets the ClassLoader of the current thread, while the former gets the ClassLoader of the class ClassUtil if it fails.
7.9 newInstance method
Instantiate an object that encapsulates the class.forname (clazz).newinstance () method.
7.10 cloneObj method
Clone an object. For some objects that do not implement the Cloneable interface, it is difficult to clone them. For example, when encapsulating the Redis client, it is almost impossible to clone the configuration object, so we wrote this method, which uses ObjectOutputStream to copy the object stream.
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/268087 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
8. File tools like Hutool — FileUtil
8.1 introduction
In Java tools, file manipulation is also used quite frequently. However, Java operations on files are complicated because they involve streams, and various streams are confusing. Therefore, most projects have a FileUtil class in util package, I think. This class is a summary of many FileUtil.
8.2 Linux Command Mapping method
These methods are named after Linux commands to help users familiar with Linux. For example:
Ls returns a list of all file objects in a given directory. The path can be a relative ClassPath path or an absolute path, not a path in the compressed package. ListFileNames returns all file names in the specified directory. Touch creates the file, if the given path does not have a parent directory, also creates the file. Del Deletes a file or directory that contains nested directories or files. Mkdir Creates a directory. The parent directory is not automatically created. CreateTempFile creates a temporary file that will be deleted when the program is finished running. Copy Copies a file or directory. The target file object can be a directory. The original file name is automatically used. Move Copy a file or directory before deleting the original file or directory. IsExist Whether the file or directory exists.Copy the code
8.3 Common Methods
GetAbsolutePath retrieves the absolute path, if the path is already absolute, returns the original path, otherwise gets its absolute position based on the relative position of the ClassPath or given class close For objects with the Closeable interface. There will be no debugging information when closing the error. This method is also used frequently, for example to close file streams and so on. Equals compares whether two files are the sameCopy the code
8.4 File Reading and Writing
8.4.1 write files
GetBufferedWriter gets a cached write object, which can write strings, etc. GetPrintWriter wrapper for getBufferedWriter, which can have println and other methods written in line. GetOutputStream will write out the stream object of the file. WriteString writes the string directly to the file, overwriting the previous content. AppendString Appends a string to text. Writappendlines overrides lists of write and append text, with each element one line. WriteBytes Write section code. WriteStream Writes the contents of a stream to a file.Copy the code
8.4.2 read file
GetReader gets the cached Reader object. ReadLines reads the data in the file line by line, returning a List, each element a single line of text. Load reads data from a file in accordance with the given ReaderHandler object. ReaderHandler is an excuse to operate on the Reader object. This method exists to prevent the user from manually calling the close method. ReadString reads all the text in the file directly. ReadBytes Reads the bytecodeCopy the code
8.4.3 other
Whether the isModifed file has been modified requires passing a timestamp to compare the last modified time. GetExtension gets the extension of the file.Copy the code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/288525 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
9. Hutool’s BoundedPriorityQueue -BoundedPriorityQueue
9.1 origin
For example, I have a user table that is hashed to different database instances based on the user name. I want to find the top five users. How do I do that? Here’s how I did it:
Find the 5 most popular items on each database instance and sort the 5 items on each database instance in order of popularity, and finally take out the top 5 itemsCopy the code
This process may seem simple, but it takes a lot of code to write on your application server. Query N lists, add them to a new list, sort them, and fetch the first 5. This process is cumbersome in code and involves multiple lists, which is a waste of space. So BoundedPriorityQueue comes into being.
/** * bounded priority queue */
public class BoundedPriorityQueueDemo {
public static void main(String[] args) {
// Initialize the queue, set the size of the queue to 5 (only 5 elements),
// The element type is INTEGER and the default comparator is used to sort the element from smallest to largest inside the queue
BoundedPriorityQueue<Integer> queue = new BoundedPriorityQueue<Integer>(5);
// Initialize the queue with a custom comparator
queue = new BoundedPriorityQueue<>(5.new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
returno1.compareTo(o2); }});// Define 6 elements. When the elements are added to the queue, they are sorted from smallest to largest.
// When the sixth element is added, the end of the queue (the largest element) is discarded
int[] array = new int[] {5.7.9.2.3.8};
for (int i : array) {
queue.offer(i);
}
// Queue can be converted to ListArrayList<Integer> list = queue.toList(); System.out.println(queue); }} Principle: set the capacity of the queue, and then add or offer all data in (both methods are the same), will get the first5The data.Copy the code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/389940 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
10. Hutool scheduling task tool -CronUtil
10.1 origin
The most commonly used framework for timed tasks in Java is Quartz, but this framework is too large, doesn’t require that many things, and is complex to use. A new framework was sought instead. As anyone who has used Linux crontab knows, the timing expression can be used to define the time and frequency of scheduled tasks flexibly (Linux crontab is accurate to minutes, while Quaeta is accurate to seconds, but accurate to minutes is enough, and accurate to seconds can be solved by using Timer). Then there is the Crontab configuration file, which clearly lists the scheduled tasks. (Remember when Spring integrated Quartz, XML was a mess). So I found a lightweight scheduling framework called Cron4J
10.2 packaging
To hide what’s inside the framework, it’s encapsulated, which means you put scheduling in a configuration file and start it up (much like The Crontab on Linux).
10.3 Configuration Files
For Maven project, the first in the SRC/main/resources/config to devolve into cron4j. The setting file (default is the path of this file), and then in the file into the timing rules, the rules are as follows:
[Package name] TestJob = */10 * * * * TestJob2 = */10 * * * * TestJob is a class that implements the Runnable interface, adding logic to the start() method.Copy the code
Expressions about timing task, it with Linux crontab expressions of the same, specific please see here: www.cnblogs.com/peida/archi…
10.4 Starting or disabling a Scheduled Task
Call cronutil.start () to start the scheduled task service, and crontabutil.stop () to stop the scheduled task service. To dynamically add scheduled tasks, use the cronutil.schedule (String schedulingPattern, Runnable task) method (scheduled tasks added using this method will not be written to the configuration file).
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/379677 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Hutool type conversion class -Convert
11.1 Type conversion class Convert
Java development will face a variety of conversion problems, especially from the command line user parameters, parameters obtained from HttpRequest, etc., these parameters are a variety of types, how to convert them? The common approach is to first complete the String and then call the xxx.parsexxx method. At the risk of conversion failure, you have to add a try catch layer. This small procedure mixing would be very ugly and bloated in business code, so this type conversion task is encapsulated in the Conver class.
1. The toStr, toInt, toLong, toDouble, and toBool methods basically replace the XX.parsexxx method in the JDK. The first argument is the converted value of type Object, and the second argument is the default value. These methods do not throw an exception when the conversion fails or if the value provided is NULL, only the default value is returned. All the returned types use the wrapped class to facilitate the case where NULL is needed. 2. ToSBC and toDBC are very useful in the unification of many texts. They are mainly used to convert punctuation marks to full Angle and half Angle.Copy the code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/270829 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
12. Hutool Singleton pool — Singleton
12.1 the reason
There are two ways to use singletons:
Add a static method getInstance() to the object to get it. This way you can refer to [go] thread-safe singleton [https://my.oschina.net/looly/blog/152865] this blog, can be divided into the hungry and satisfy the han mode. Use a container like Spring to manage objects in a unified way. Spring can also be configured to determine lazy or hungry modeCopy the code
To be clear, I prefer the second option, but Spring is more about injecting rather than taking, so I want to do the Singleton class, maintain a pool of Singleton objects, and just take them when you use them, using lazy mode here. I just want to change the way singletons are managed. I want to manage singletons with a container tool rather than a large framework, which can greatly reduce the complexity of using singletons.
12.2 the use of
import com.xiaoleilu.hutool.Singleton;
/** * Single example */
public class SingletonDemo {
/** * animal interface */
public static interface Animal{
public void say(a);
}
/** * the dog implements */
public static class Dog implements Animal{
@Override
public void say(a) {
System.out.println("Wang wang"); }}/** * the cat implements */
public static class Cat implements Animal{
@Override
public void say(a) {
System.out.println("Meow"); }}public static void main(String[] args) {
Animal dog = Singleton.get(Dog.class);
Animal cat = Singleton.get(Cat.class);
// The Singleton object is the same object each time unless singleton.destroy () or remove is called
System.out.println(dog == Singleton.get(Dog.class)); //true
System.out.println(cat == Singleton.get(Cat.class)); //true
dog.say(); / / wang wang
cat.say(); / / meow meow}}Copy the code
12.2 summarize
The implementation is very simple. A HashMap is used as a singleton pool to instantiate objects through newInstance() (no constructor with arguments is supported). Both fetching and creating objects are thread-safe (there may be bottlenecks when the number of singletons is very large and the singletons are very time-consuming to instantiate). Considering that double-checked locks are made during GET, synchronized is directly added as a modifiers, which is welcome to give suggestions here.
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/284922 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
13. Hutool Properties instead of — Setting
13.1 introduction
There are many limitations to the Properties file read by JDK. First, iso8859-1 encoding makes it impossible to add Chinese values and annotations (using the Japanese plug-in in Eclipse can read and write, but it is difficult to read on the server). Furthermore, it does not support variable grouping and other functions, hence the Setting class.
13.2 origin
Configuration file using variable has a long history, the demand is accomplished in the Spring class is used for the ApplicationContext. Use the Properties in the XML file variable substitution. I first learned about grouping in the /etc/rsyncd.conf configuration file of Linux rsync, and found it particularly useful.
These two functions were later discovered in the Props of JoDD. The extension class of its configuration file is very powerful and even supports multi-line functions. Originally, I wanted to use it directly to avoid repeating the wheel, but I found that many features were completely useless and there was no convenient function needed, so I built the wheel Setting.
The configuration file format is example.setting
<! -- lang: shell --># -------------------------------------------------------------
# ----- Setting File with UTF8-----
#----- Database configuration file -----
# -------------------------------------------------------------
#The parentheses represent a group to which all the attributes below belong, which is called Demo, or no group at all
[demo]
#Custom data source Settings file, which is valid for the current group and is used to configure separate database connection pool parameters for the current group.
#If not, use the global configuration
ds.setting.path = config/other.setting
#Database driver name, if not specified, is automatically determined based on the URL
driver = com.mysql.jdbc.Driver
#JDBC URL, required
url = jdbc:mysql://fedora.vmware:3306/extractor
#The user name is mandatory
user = root${driver}
#The password is mandatory. If the password is empty, enter pass =
pass = 123456
Copy the code
The configuration file can be placed anywhere. The Setting class provides several ways to read the configuration file in the constructor, which will be described later. Now let’s talk about the specific format of the configuration file, Setting configuration file is similar to the Properties file, the rules are as follows:
-
Comments start with # and only single-line comments are supported. Empty lines and key-value pairs that are not properly recognized are also ignored and can be used as comments, but explicitly specifying comments is recommended.
-
Key/value pairs are represented by key = value. Key and value are read with whitespace trimmed, so don’t worry about whitespace.
-
The contents are grouped in brackets (for example, [demo] in the configuration file). The lines below the brackets are all grouped in brackets. The contents that are not grouped in brackets are grouped in blank characters, that is, []. If a key is name and the group is group, the key after the group is added is equivalent to group.name.
-
Variables are supported. The default variable is named{driver} will be replaced with com.mysql.jdbc.driver. For performance purposes, the constructor of Setting specifies whether variable substitution is enabled.
<! -- lang: java -->import java.io.IOException;
import com.xiaoleilu.hutool.CharsetUtil;
import com.xiaoleilu.hutool.FileUtil;
import com.xiaoleilu.hutool.Setting;
/** * Setting example class */
public class SettingDemo {
public static void main(String[] args) throws IOException {
/ / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the initialization
// Read xxx. setting in classpath without variable
Setting setting = new Setting("XXX.setting");
// Read xxx. setting in config directory of classpath without variable
setting = new Setting("config/XXX.setting");
/home/looly/ xxx. setting
// The second parameter is the self-defined encoding, please keep consistent with the encoding of the Setting file
// The third argument is whether to use variables. If true, each key in the configuration file can be referenced by value in subsequent entries.
setting = new Setting(FileUtil.touch("/home/looly/XXX.setting"), CharsetUtil.UTF_8, true);
// Read xxx. setting in the same package as settingDemo.class
setting = new Setting("XXX.setting", SettingDemo.class, CharsetUtil.UTF_8, true);
/ / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
// Get the key of name
setting.getString("name");
// Get the value of key name under group
setting.getString("name"."group1");
// Returns the default value when the value obtained is null (or a whitespace character, including multiple Spaces)
setting.getStringWithDefault("name"."Default value");
// Complete method with key, grouping, and default values
setting.getStringWithDefault("name"."group1"."Default value");
// If you want to get another type of value, you can call the corresponding getXXX method with similar arguments
// This method generates a debug log when the value corresponding to the key does not exist
setting.getWithLog("name");
setting.getWithLog("name"."group1");
// To re-read the configuration file, you can enable a timer to call this method to periodically update the configuration
setting.reload();
// When a new key-value pair is added through code, the store call is saved to the file, but overwrites the original file and loses the comment
setting.setSetting("name1"."value");
setting.store("/home/looly/XXX.setting");
// Get all the grouping names
setting.getGroups();
// Map key-value to object by calling the corresponding setXX method of the object
//setting.toObject();
// Sets the regular expression for the variable name.
// The variable name of the Setting is replaced by the re lookup. If the variable name in the Setting conflicts with other variables,
// You can change the way variables are defined
Group 1 matches the name of the key
setting.setVarRegex("\ \ $\ \ {(. *?) \ \}"); }}Copy the code
Simple encapsulation of Properties (available as of Version 2.0.0)
I can’t do anything about the widespread use of Properties. Sometimes it is not easy to read and write Properties files easily, so I made a simple encapsulation of Properties and provided a convenient construction method (consistent with the Setting). The Properties class Props is an extension of the Properties class, so it is compatible with the Properties class. Interested can look at the cn. Hutool. Setting. The dialect. The Props
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/302407 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
14. Hutool – crypto modules
14.1 Using Hutool to Implement AES and DES Encryption and decryption
14.1.1 introduction
Both AES and DES are symmetric encryption algorithms. Data sender processes the plaintext (original data) and encryption key together with a special encryption algorithm to make the data into complex encrypted ciphertext and send it. After receiving the ciphertext, the recipient needs to decrypt the ciphertext using the used encryption key and the inverse algorithm of the same algorithm to restore the ciphertext to readable text if it wants to read the original text. In symmetric encryption algorithm, only one key is used. Both sender and receiver use this key to encrypt and decrypt data, which requires that the decryptor must know the encryption key in advance.
In the Java world, AES and DES encryption and decryption require Cipher objects to construct encryption and decryption systems. Hutool rewraps this object to simplify the encryption and decryption process.
14.1.2 introduced Hutool
<dependency> <groupId>cn. Hutool </groupId> <artifactId>hutool-all</artifactId> <version>4.1.2</version> </dependency>Copy the code
14.1.3 use
** AES encryption decryption String Content ="Test" in Chinese; byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded(); AES AES = SecureUtil. AES (key); // Build byte[] encrypt = aes.encrypt(content); // build byte[] encrypt = aes.encrypt(content); Byte [] decrypt = AES.decrypt (encrypt); byte[] decrypt = AES.decrypt (encrypt); // encryptHex = aes.encrypThex (content); // Encrypt to hexadecimal String decryptStr = AES.decryptstr (encryptHex); ** DES encryption and decryption. The usage of DES is basically the same as that of AES. String Content ="Test" in Chinese; byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.DES.getValue()).getEncoded(); DES DES = SecureUtil. DES (key); // Build byte[] encrypt = des.encrypt(content); // build byte[] encrypt = des.encrypt(content); Byte [] decrypt = des.decrypt(encrypt); byte[] decrypt = des.decrypt(encrypt); String encryptHex = des.encryptHex(content); // Encrypt to hexadecimal and decrypt to the original String String decryptStr = des.decryptstr (encryptHex);Copy the code
14.1.4 more
All symmetric encryption algorithms supported by JDK are encapsulated in Hutool as SymmetricCrypto class. AES and DES classes are simplified representations of this class. You can use the same method to encrypt and decrypt strings or objects by instantiating this class and passing in the corresponding algorithmic enumeration.
Hutool supports the following symmetric encryption algorithms:
AES
ARCFOUR
Blowfish
DES
DESede
RC2
PBEWithMD5AndDES
PBEWithSHA1AndDESede
PBEWithSHA1AndRC2_40
Copy the code
These enumerations are all enumerated in the SymmetricAlgorithm
Symmetric encryption objects are also very simple to use: String Content ="Test" in Chinese;
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();// Randomly generate the key
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);/ / build
byte[] encrypt = aes.encrypt(content);/ / encryption
byte[] decrypt = aes.decrypt(encrypt);/ / decryption
String encryptHex = aes.encryptHex(content);// Encrypt to hexadecimal representation
String decryptStr = aes.decryptStr(encryptHex);// Decrypt to a string
Copy the code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/1504160 oschinaCopy the code
14.2 Using Hutool to Process Asymmetric Encryption, such as RSA
14.2.1 introduction
Hutool is a domestic open source Java tool set, aimed at simplifying the tedious process of Java development, Hutool- Crypto module is for JDK encryption and decryption to do a great simplification. This paper mainly introduces the use of Hutool- Crypto to simplify asymmetric encryption and decryption. For asymmetric encryption, the most commonly used are RSA and DSA. In Hutool, asymmetrical Crypto object is used to encrypt and decrypt. Asymmetric encryption has two concepts: public key and private key. The private key is owned by itself and cannot be given to others, while the public key is made public. Depending on the application, we can choose to use different key encryption:
Signature: Use the private key to encrypt and the public key to decrypt. Used to allow all public key owners to verify the identity of the private key owner and to prevent content published by the private key owner from being tampered with, but not to ensure that the content is not available to others. Encryption: Encrypt with the public key and decrypt with the private key. Used to publish information to the public key owner that can be tampered with but cannot be accessed by others.Copy the code
14.2.2 use
14.2.2.1 introduced Hutool
<dependency> <groupId>cn. Hutool </groupId> <artifactId>hutool-all</artifactId> <version>4.1.2</version> </dependency>Copy the code
In asymmetric encryption, we can use AsymmetricCrypto(AsymmetricAlgorithm algorithm) constructor to obtain its encryption and decryptor by passing in different algorithm enumeration. Of course, for convenience, we have built separate objects for the most common RSA and DSA algorithms: RSA and DSA.
14.2.2.2 Basic Usage
The following uses RSA as an example to describe how to use RSA encryption and decryption. When constructing an RSA object, you can pass in a public key or a private key. When using the no-parameter constructor, Hutool automatically generates a random public and private key pair:
RSA rsa = new RSA();
rsa.getPrivateKey()// Get the private key
rsa.getPrivateKeyBase64()
rsa.getPublicKey()// Get the public key
rsa.getPublicKeyBase64()
byte[] encrypt = rsa.encrypt(StrUtil.bytes("I'm a test AAAA.", CharsetUtil.CHARSET_UTF_8),
KeyType.PublicKey);// Public key encryption, private key decryption
byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
Assert.assertEquals("I'm a test AAAA.", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
// Private key encryption, public key decryption
byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("I'm a test AAAA.", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);
Assert.assertEquals("I'm a test AAAA.", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));
Copy the code
Encryption and decryption can be completely separated, and for RSA objects, the other argument can be null if only the public or private key is used
14.2.2.3 Generating a Key Pair By yourself
Sometimes want to self-help generate keys can: KeyPair pair. = SecureUtil generateKeyPair ("RSA");
pair.getPrivate();
pair.getPublic();
Copy the code
The self-generated key pair is in byte[] form, which we can convert to Base64 using the base64. encode method for easy storage as text. Of course, if you use RSA objects, you can also use encryptStr and decryptStr to encrypt and decrypt strings
14.2.3 case
Given the private key and ciphertext, how can I decrypt the ciphertext? String PRIVATE_KEY ="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIL7pbQ+5KKGYRhw7jE31hmA"
+ "f8Q60ybd+xZuRmuO5kOFBRqXGxKTQ9TfQI+aMW+0lw/kibKzaD/EKV91107xE384qOy6IcuBfaR5lv39OcoqNZ"
+ "5l+Dah5ABGnVkBP9fKOFhPgghBknTRo0/rZFGI6Q1UHXb+4atP++LNFlDymJcPAgMBAAECgYBammGb1alndta"
+ "xBmTtLLdveoBmp14p04D8mhkiC33iFKBcLUvvxGg2Vpuc+cbagyu/NZG+R/WDrlgEDUp6861M5BeFN0L9O4hz"
+ "GAEn8xyTE96f8sh4VlRmBOvVdwZqRO+ilkOM96+KL88A9RKdp8V2tna7TM6oI3LHDyf/JBoXaQJBAMcVN7fKlYP"
+ "Skzfh/yZzW2fmC0ZNg/qaW8Oa/wfDxlWjgnS0p/EKWZ8BxjR/d199L3i/KMaGdfpaWbYZLvYENqUCQQCobjsuCW"
+ "nlZhcWajjzpsSuy8/bICVEpUax1fUZ58Mq69CQXfaZemD9Ar4omzuEAAs2/uee3kt3AvCBaeq05NyjAkBme8SwB0iK"
+ "kLcaeGuJlq7CQIkjSrobIqUEf+CzVZPe+AorG+isS+Cw2w/2bHu+G0p5xSYvdH59P0+ZT0N+f9LFAkA6v3Ae56OrI"
+ "wfMhrJksfeKbIaMjNLS9b8JynIaXg9iCiyOHmgkMl5gAbPoH/ULXqSKwzBw5mJ2GW1gBlyaSfV3AkA/RJC+adIjsRGg"
+ "JOkiRjSmPpGv3FOhl9fsBPjupZBEIuoMWOC8GXK/73DHxwmfNmN7C9+sIi4RBcjEeQ5F5FHZ";
RSA rsa = new RSA(PRIVATE_KEY, null);
String a = "2707F9FD4288CEF302C972058712F24A5F3EC62C5A14AD2FC59DAB93503AA0FA17113A020EE4EA35EB53F"
+ "75F36564BA1DABAA20F3B90FD39315C30E68FE8A1803B36C29029B23EB612C06ACF3A34BE815074F5EB5AA3A"
+ "C0C8832EC42DA725B4E1C38EF4EA1B85904F8B10B2D62EA782B813229F9090E6F7394E42E6F44494BB8";
byte[] aByte = HexUtil.decodeHex(a);
byte[] decrypt = rsa.decrypt(aByte, KeyType.PrivateKey);
Assert.assertEquals("Tiger head in Hangzhou, look up to the sky, do not just farm.", StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8));
Copy the code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/1523094 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
15. Hutool – the core module
DynaBean, BeanDesc, beandEsc. PropDesc, BeanPath, BeanUtil
15.1 DynaBean: Dynamic Bean that encapsulates instance objects and makes reflection calls
15.2 BeanDesc: Bean information description as an alternative to BeanInfo
15.3 beandEsc. PropDesc: Property description
15.4 BeanPath: Bean path expression object used for property checking
15.5 BeanUtil: Utility class
The original author: Mr Huan dragon link: https://blog.csdn.net/sanjun333/article/details/90645420 source: CSDNCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
16. Hutool – HTTP modules
Use Hutool to crawl open source Information in China, demonstrating the HTTP request function of Hutool- HTTP.
16.1 Analysis Page
1. Open the osChina homepage, we find the most conspicuous open source information module, and then click “more” to open the “open source information” section.
16.2 Simulating Http Request Page crawling
Using Hutool-HTTP to accompany the ReUtil request and extract the page content is simple as follows: / / request list page String listContent = HttpUtil. Get (" http://www.oschina.net/action/ajax/get_more_news_list?newsType=&p=2 "); List<String> titles = reutil.findall ("<span class=\"text-ellipsis\">(.*?) </span>", listContent, 1); for (String title : titles) { Console.log(title); The first line requests the page content, and the second line relocates all header lines and extracts the header section. Here's the re part: the reutil. findAll method is used to findAll the content parts of the regular expression that match. The second argument, 1, extracts the content from the first parenthesis (grouping), and 0 extracts all the content that the regular expression matches. See the ReUtil section in the Core module for details on this method. <span class=\"text-ellipsis\">(.*?) </span> this re is the one we obtained from analyzing the source code of the page above, where (.*?) For what we need,. For any character, * for zero or more,? Shortest match, the whole re means. <span class= "text-ellipsis\">, </span> all the middle characters, the middle characters must be the shortest. ? </span> </span> </span> </span> </span> </span> </span> </span>Copy the code
16.2 conclusion
I have to say that fetching itself is not difficult, but working with Hutool makes it much easier and faster, and the challenge is to analyze the page and locate the content we need.
Real content scraping is divided into two parts:
- Find the list page (many sites don’t have a general list page)
- Request list page to get the details page address
- Request the detail page and use the re to match what we need
- Store or save content as a file
And in the process of fetching we will also encounter a variety of problems, including but not limited to:
- Sealing IP
- There are special requirements for request headers
- There are special requirements for cookies
- Verification code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/1575851 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
17. Hutool CaptchaUtil
Use Hutool [Generate and validate] graphic captcha
17.1 origin
With more and more comprehensive attack protection, graphic verification code is a simple and effective means of anti-attack and anti-capture, so it is more and more widely used. Hutool abstracts the implementation of captcha and provides several simple implementations of captcha, thus greatly reducing the cost of server-side development. Due to the huge demand for verification codes and the accumulation in my previous projects, I added verification code generation and verification functions in Hutool.
17.2 introduced Hutool
Learn more about Hutool at: http://hutool.cn/ <dependency> <groupId>com.xiaoleilu</groupId> <artifactId>hutool-all</artifactId> The < version > 3.2.3 < / version > < / dependency >Copy the code
17.3 introduction
The captcha function is in the com.hutool.captcha package. The core interface is ICaptcha, which defines the following methods:
GetCode Obtains the text content of the verification code. Verify Whether the verification code is correct. You are advised to ignore case and write the verification code to the target streamCopy the code
The write method has only one OutputStream, which the ICaptcha implementation class can wrap to write methods to files. AbstractCaptcha AbstractCaptcha is an ICaptcha abstract implementation class that implements captcha text generation, case-insensitive validation, write to streams and files, etc.
17.4 the implementation class
- LineCaptcha line segment interference verification code, the generated effect is roughly as follows:
// Define the length and width of the graphic captcha
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200.100);
//LineCaptcha lineCaptcha = new LineCaptcha(200, 100, 4, 150);
// Graphic captcha can be written to a file or stream
lineCaptcha.write("d:/line.png");
// Verify the validity of the graphic captcha, return Boolean
lineCaptcha.verify("1234");
Copy the code
- CircleCaptcha circle interference captcha
// Define the length, width, number of characters, and number of interference elements of the graphic captcha
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200.100.4.20);
//CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
// Graphic captcha can be written to a file or stream
captcha.write("d:/circle.png");
// Verify the validity of the graphic captcha, return Boolean
captcha.verify("1234");
Copy the code
- ShearCaptcha Distortion interference verification code
// Define the length, width, number of characters, and interference line width of the graphic captcha
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200.100.4.4);
//ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
// Graphic captcha can be written to a file or stream
captcha.write("d:/shear.png");
// Verify the validity of the graphic captcha, return Boolean
captcha.verify("1234");
Copy the code
- Write to browser (Servlet output)
ICaptcha captcha = ... ; captcha.write(response.getOutputStream());// The Servlet OutputStream must be closed by itself!
Copy the code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/1591079 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
18. Use Hutool to send daily reports
18.1 Used Modules and Tool Classes
Cron module, used to send mail regularly extra module, MailUtil module, used to send mail POI module, WordWriter, used to generate daily wordCopy the code
18.2 Project Structure
Demo projects can access code cloud addresses: https://gitee.com/loolly_admin/daily-work/SRC/main/Java cn hutool/example/dailyWork/DailyWorkGenerator. Java - daily generator, Java ---- Mail sending task, used to send mail dailyworkmain. Java ---- main program for scheduled tasks, Used to start scheduled tasks SRC /main/resources config/ cron.setting ---- Scheduled task configuration file mail.setting ---- Mailbox configuration fileCopy the code
18.3 Code implementation
1. Generate daily Word
/** Title font */
private static final Font TITLE_FONT = new Font("Black", Font.PLAIN, 22);
/** Body font */
private static final Font MAIN_FONT = new Font("宋体", Font.PLAIN, 14);
/** * generate daily **@returnDaily Word file */
public static File generate(a) {
// 1
File wordFile = FileUtil.file(StrUtil.format("Daily Work Report _{}.docx", DateUtil.today()));
if(FileUtil.exist(wordFile)) {
// If the file exists, delete it.
wordFile.delete();
}
// Generate and write word
Word07Writer writer = new Word07Writer(wordFile);
writer.addText(ParagraphAlignment.CENTER, TITLE_FONT, "Daily At Work");
writer.addText(MAIN_FONT, "");
writer.addText(MAIN_FONT, "Dear leader,);
writer.addText(MAIN_FONT, "Today I fished in the Hutool group and didn't do any work.");
writer.close();
return wordFile;
}
Copy the code
2. Send emails Configure the sender box information in mailer. Setting
From = Hutool<[email protected]> If you use foxMail, User = hutool # password pass = XXXX # sslEnable = true = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = / / today's date, format is similar to: 2019-06-20 String today = DateUtil.today(); / / report generation Word File dailyWorkDoc = DailyWorkGenerator. The generate (); SendHtml ("[email protected]", strutil. format("{} work daily ", today), "see attachment." , dailyWorkDoc); Staticlog.debug ("{} Workdaily sent to leader!" , today);Copy the code
3. Add the sent email as a scheduled task to the configuration file:
[cn hutool. Example. DailyWork] # 6 PM every day regularly send MailSendTask. Execute = 00 00 18 * * *Copy the code
4. Start the scheduled task
// Set second matching (used only when timed tasks are accurate to seconds)
CronUtil.setMatchSecond(true);
// Start a scheduled task to automatically load the content in the configuration file
CronUtil.start();
Copy the code
Results of 5.
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/3064203 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
19. Hutool – log module
19.1 Java Logging Framework Classification
19.1.1 Log Facade
* Slf4j stands for Simple Logging FacadeforJAVA, the real logging surface, only provides interface methods, when combined with a specific logging implementation, need to introduce the corresponding bridge package * common-logging Apache provides a Common logging interface, Common logging through the dynamic lookup mechanism, When the program is running to find out the real log library, their own very weak log implementation. * the difference: Common-logging dynamically finds logging implementations (the program finds logging implementations at runtime), whereas Slf4j is statically bound (the implementation is found at compile time). Dynamic binding relies on ClassLoader to find and load logging implementations. Therefore, using a separate ClassLoader like OSGI can cause unusable situations. Slf4j supports parameterizedlogString, which avoids having to be written before to reduce the performance cost of string concatenationif(logger.isdebugenable ()), you can now write: logger.debug(" current user is: {} ", user).Copy the code
19.1.2 Log implementation
-
Log4j Log4j is probably the best known logging framework in the Java world, supporting various levels of logging output to various destinations.
-
LogBack Log4j is another masterstroke from the authors of LogBack Log4j. It feels like the best logging framework to date, has always been used, the configuration file is concise, and the performance is good enough.
JDK Logging has been introduced since JDK1.4. If you Google the JDK Logging component, it is not as easy to use as Log4j and LogBack. There are no configuration files, and the Logging level is not easy to understand. The JDK design is not universally accepted, and the only advantage is that it does not introduce new JAR packages.
19.1.3 Why is there a facade
If you’re not a Java veteran, you might be confused, and you might ask why you need to keep up appearances. With a mobile phone, there is a mobile phone case, mobile phone film, frame is the same, the role of the facade is more or three words: decoupling. To put it bluntly, joining a project uses a logging framework. What if you want to change? It’s a nightmare to look for log changes line by line. If you want to use slF4J-API to change the logging framework, you can simply change the bridge package. It’s very convenient. To tell you the truth, Slf4j can be a standard Java logging, according to it to write basic can achieve all log implementation universal, but some people do not accept, also write the facade (yes, this person is me).
19.1.4 Facade of facade
If seen Netty source, recommend you take a look at io.net. Ty util. Internal. Logging in the packet content, will find Netty to log encapsulates another layer.
19.2 Hutool – log module
Either the Netty Logging module or the Hutool-log module, the idea is similar to Common Logging, do the dynamic log implementation search, and then find the corresponding log implementation to write logs, the core code is as follows:/** * determine the logging implementation *@returnLogging implementation class */
public static Class<? extends AbstractLog> detectLog(){
List<Class<? extends AbstractLog>> logClassList = Arrays.asList(
Slf4jLog.class,
Log4jLog.class,
Log4j2Log.class,
ApacheCommonsLog.class,
JdkLog.class
);
for (Class<? extends AbstractLog> clazz : logClassList) {
try {
clazz.getConstructor(Class.class).newInstance(LogFactory.class).info("Use Log Framework: [{}]", clazz.getSimpleName());
return clazz;
} catch (Error | Exception e) {
continue; }}return JdkLog.class;
}
Copy the code
Instantiate the corresponding Logging implementation sequentially. If the instantiation fails (usually ClassNotFoundException), the JAR does not exist, instantiate the next one, and by trial and error, eventually use JDK Logging if no Logging framework is introduced (which it will be). Of course, this approach has similar issues to common-logging, but it works fine without having to cross ClassLoader.
Some adaptations have been made to JDK Logging to correspond to Slf4j’s Logging level, which minimizes the differences between Logging frameworks. On the other hand, if you’ve seen this post, you’ve already seen my class name auto-recognition feature, which allows you to copy the class name without having to change that line of code in the log. Among all classes, the log initialization is only this:
Log log = LogFactory.get();
//======= implementation =======
/ * * *@returnGet the caller's log * Get the current class name by stack reference. * /
public static Log get(a) {
return getLog(new Exception().getStackTrace()[1].getClassName());
}
/** * Unified log interface */
public interface Log extends TraceLog.DebugLog.InfoLog.WarnLog.ErrorLogThis allows for a single use and flexible reference from each logging framework.Copy the code
Author: road small lei Source: the original links: https://my.oschina.net/looly/blog/543000 oschinaCopy the code
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =