!288 特殊符号工具类

Merge pull request !288 from dazer007/v5-dev
This commit is contained in:
Looly 2021-03-26 20:52:37 +08:00 committed by Gitee
commit 6a8e5d8b6c
3 changed files with 184 additions and 43 deletions

View File

@ -0,0 +1,100 @@
package cn.hutool.core.text;
import java.math.BigInteger;
/**
* 符号工具类
* @author dazer & neusoft
* @date 2021/3/26 12:21
* 别名Symbol or special signal or Special symbols
* 说明获取常见的特殊符号带圈数字
*
* {@link UnicodeUtil}
* @link 百度百科 https://baike.baidu.com/item/%E7%89%B9%E6%AE%8A%E5%AD%97%E7%AC%A6/112715?fr=aladdin
* @link 360百科 https://baike.so.com/doc/5408938-5646935.html
* @link 百科 https://www.baike.com/wikiid/3469869303298461399?prd=home_search&search_id=5bm572esa2k000&view_id=1takcxx7kjc000
* @link coolsymbol https://coolsymbol.com/
* @link 维基百科wikipedia https://en.wikipedia.org/wiki/List_of_Unicode_characters#Unicode_symbols
*
* @since 5.6.2
*/
public class SpecialSymbolUtil {
public static final String UNICODE_START_CHAR = "\\u";
private SpecialSymbolUtil(){}
/**
* 获取带圈数字 /封闭式字母数字 从1-20,超过1-20报错
*
* 0 1 2 3 4 5 6 7 8 9 A B C D E F
* U+246x
* U+247x
* U+248x
* U+249x
* U+24Ax
* U+24Bx
* U+24Cx
* U+24Dx
* U+24Ex
* U+24Fx
* @link Unicode_symbols https://en.wikipedia.org/wiki/List_of_Unicode_characters#Unicode_symbols
* @link Enclosed Alphanumerics https://en.wikipedia.org/wiki/Enclosed_Alphanumerics
*
* <code>
* System.out.println(enclosedAlphanumericsStyle1ByInt(1));
* System.out.println(enclosedAlphanumericsStyle1ByInt(2));
* System.out.println(enclosedAlphanumericsStyle1ByInt(3));
* System.out.println(enclosedAlphanumericsStyle1ByInt(4));
* System.out.println(enclosedAlphanumericsStyle1ByInt(14));
* System.out.println(enclosedAlphanumericsStyle1ByInt(18));
* System.out.println(enclosedAlphanumericsStyle1ByInt(19));
* System.out.println(enclosedAlphanumericsStyle1ByInt(20));
* </code>
*
* @param number 十进制数字从1-->1011--->20
* @return
*/
public static String enclosedNumericsByInt(int number) {
if (number <= 0 || number > 20) {
throw new IllegalArgumentException("number取值范围是[1-20]的正整数,包含1和20");
}
String start = "";
String unicodeStart = UnicodeUtil.toUnicode(start).substring(UNICODE_START_CHAR.length(),
UnicodeUtil.toUnicode(start).length() - 1);
// begin: U+246x的24
String beginHex = unicodeStart.substring(0, 2);
// U+246x的6
String middleHex = number >= 17 ? "7" : "6";
// A
String endHex = Integer.toHexString(number >= 17 ? number - 17 : number - 1);
// U + 24 + 60
String unicodeStr = UNICODE_START_CHAR + beginHex + middleHex + endHex;
return UnicodeUtil.toString(unicodeStr);
}
/**
* 获取带圈字母 /封闭式字母 从a-z or A-Z
* 根据字符 获取
* <code>
* System.out.println(encloseAlphabetByChar( 'A'));
* System.out.println(encloseAlphabetByChar( 'a'));
* System.out.println(encloseAlphabetByChar( 'z'));
* System.out.println(encloseAlphabetByChar( 'Z'))
* </code>
* @author dazer
* @param letter 字母不区分大小写'a''b''c''d'...'x''y''z'; 'A''B'...'Z'
* @date 2021/3/26 18:10
*/
public static String encloseAlphabetByChar(char letter) {
if (!(letter >= 'a' && letter <= 'z' || letter >= 'A' && letter <= 'Z')) {
throw new IllegalArgumentException("number取值范围是[a-z]、[A-Z]的字符");
}
// \u24b6
String start = "";
String hexStr = UnicodeUtil.toUnicode(start).substring(UNICODE_START_CHAR.length());
int difference = letter >= 'a' && letter <= 'z' ? (letter - (int)'a') : (letter - (int)'A');
String hex = new BigInteger(hexStr, 16).add(new BigInteger(String.valueOf(difference), 10)).toString(16);
//
String unicodeStr = UNICODE_START_CHAR + hex;
return UnicodeUtil.toString(unicodeStr);
}
}

View File

@ -549,6 +549,17 @@ public class ServletUtil {
* @param response 响应对象{@link HttpServletResponse}
* @param in 需要返回客户端的内容
* @param contentType 返回的类型
*
* 1application/pdf
* 2application/vnd.ms-excel
* 3application/msword
* 4application/vnd.ms-powerpoint
* docxxlsx 这种 office 2007 格式 设置 MIME;网页里面docx 文件是没问题但是下载下来了之后就变成doc格式了
* https://blog.csdn.net/cyh2260629/article/details/73824760
* 5MIME_EXCELX_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
* 6MIME_PPTX_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
* 7MIME_WORDX_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
* 8MIME_STREAM_TYPE = "application/octet-stream;charset=utf-8"; #原始字节流
* @param fileName 文件名
* @since 4.1.15
*/

View File

@ -0,0 +1,30 @@
package cn.hutool.extra.servlet;
import org.junit.Test;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
/**
* ServletUtil工具类测试
* @author dazer
* @date 2021/3/24 15:02
* @see ServletUtil
*/
public class ServletUtilTest {
@Test
public void writeTest() {
HttpServletResponse response = null;
byte[] bytes = new String("地球是我们共同的家园,需要大家珍惜.").getBytes(StandardCharsets.UTF_8);
//下载文件
// 这里没法直接测试直接写到这里方便调用
if (response != null) {
String fileName = "签名文件.pdf";
String contentType = "application/pdf";// application/octet-streamimage/jpegimage/gif
response.setCharacterEncoding(StandardCharsets.UTF_8.name()); // 必须设置否则乱码; 但是 safari乱码
ServletUtil.write(response, new ByteArrayInputStream(bytes), contentType, fileName);
}
}
}