中文文件名转拼音
This commit is contained in:
@@ -0,0 +1,299 @@
|
||||
package com.accompany.common.utils;
|
||||
|
||||
import net.sourceforge.pinyin4j.PinyinHelper;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
||||
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* @author QZB
|
||||
* @create 2019-12-18 17:25
|
||||
* PinYin4j工具类
|
||||
*/
|
||||
public class PinYin4JUtil {
|
||||
/**
|
||||
* 将字符串转换成拼音数组
|
||||
*
|
||||
* @param src 字符串
|
||||
* @return
|
||||
*/
|
||||
public static String[] stringToPinyin(String src) {
|
||||
return stringToPinyin(src, false, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转换成拼音数组
|
||||
*
|
||||
* @param src 字符串
|
||||
* @param separator 多音字拼音之间的分隔符
|
||||
* @return
|
||||
*/
|
||||
public static String[] stringToPinyin(String src, String separator) {
|
||||
return stringToPinyin(src, true, separator);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转换成拼音数组
|
||||
*
|
||||
* @param src 字符串
|
||||
* @param isPolyphone 是否查出多音字的所有拼音
|
||||
* @param separator 多音字拼音之间的分隔符
|
||||
* @return
|
||||
*/
|
||||
public static String[] stringToPinyin(String src, boolean isPolyphone, String separator) {
|
||||
// 判断字符串是否为空
|
||||
if (StringUtils.isBlank(src)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
char[] srcChar = src.toCharArray();
|
||||
int srcCount = srcChar.length;
|
||||
String[] srcStr = new String[srcCount];
|
||||
|
||||
for (int i = 0; i < srcCount; i++) {
|
||||
srcStr[i] = charToPinyin(srcChar[i], isPolyphone, separator);
|
||||
}
|
||||
return srcStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将单个字符转换成拼音
|
||||
*
|
||||
* @param src 被转换的字符
|
||||
* @param isPolyphone 是否查出多音字的所有拼音
|
||||
* @param separator 多音字拼音之间的分隔符
|
||||
* @return
|
||||
*/
|
||||
public static String charToPinyin(char src, boolean isPolyphone, String separator) {
|
||||
// 创建汉语拼音处理类
|
||||
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
|
||||
// 输出设置,大小写,音标方式
|
||||
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
||||
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||
StringBuilder tempPinyin = new StringBuilder();
|
||||
// 如果是中文
|
||||
if (src > 128) {
|
||||
try {
|
||||
// 转换得出结果
|
||||
String[] strs = PinyinHelper.toHanyuPinyinStringArray(src, defaultFormat);
|
||||
|
||||
// 是否查出多音字,默认是查出多音字的第一个字符
|
||||
if (isPolyphone && null != separator) {
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
tempPinyin.append(strs[i]);
|
||||
if (strs.length != (i + 1)) {
|
||||
// 多音字之间用特殊符号间隔起来
|
||||
tempPinyin.append(separator);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tempPinyin.append(strs[0]);
|
||||
}
|
||||
|
||||
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
tempPinyin.append(src);
|
||||
}
|
||||
|
||||
return tempPinyin.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将汉字转换成拼音
|
||||
*
|
||||
* @param hanzi
|
||||
* @return
|
||||
*/
|
||||
public static String hanziToPinyin(String hanzi) {
|
||||
return hanziToPinyin(hanzi, " ");
|
||||
}
|
||||
|
||||
/**
|
||||
* 将汉字转换成拼音
|
||||
*
|
||||
* @param hanzi 汉字
|
||||
* @param separator 分隔符
|
||||
* @return
|
||||
*/
|
||||
public static String hanziToPinyin(String hanzi, String separator) {
|
||||
// 创建汉语拼音处理类
|
||||
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
|
||||
// 输出设置,大小写,音标方式
|
||||
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
||||
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||
String pinyingStr = "";
|
||||
try {
|
||||
pinyingStr = PinyinHelper.toHanYuPinyinString(hanzi, defaultFormat, separator, true);
|
||||
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return pinyingStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串数组转换成字符串
|
||||
*
|
||||
* @param str
|
||||
* @param separator 各个字符串之间的分隔符
|
||||
* @return
|
||||
*/
|
||||
public static String stringArrayToString(String[] str, String separator) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < str.length; i++) {
|
||||
sb.append(str[i]);
|
||||
if (str.length != (i + 1)) {
|
||||
sb.append(separator);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的将各个字符数组之间连接起来
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String stringArrayToString(String[] str) {
|
||||
return stringArrayToString(str, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符数组转换成字符串
|
||||
*
|
||||
* @param ch 字符数组
|
||||
* @param separator 各个字符串之间的分隔符
|
||||
* @return
|
||||
*/
|
||||
public static String charArrayToString(char[] ch, String separator) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < ch.length; i++) {
|
||||
sb.append(ch[i]);
|
||||
if (ch.length != (i + 1)) {
|
||||
sb.append(separator);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符数组转换成字符串
|
||||
*
|
||||
* @param ch 字符数组
|
||||
* @return
|
||||
*/
|
||||
public static String charArrayToString(char[] ch) {
|
||||
return charArrayToString(ch, " ");
|
||||
}
|
||||
|
||||
/**
|
||||
* 取汉字的首字母
|
||||
*
|
||||
* @param src
|
||||
* @param isCapital 是否是大写
|
||||
* @return
|
||||
*/
|
||||
public static char[] getHeadByChar(char src, boolean isCapital) {
|
||||
// 如果不是汉字直接返回
|
||||
if (src <= 128) {
|
||||
return new char[]{src};
|
||||
}
|
||||
// 获取所有的拼音
|
||||
String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(src);
|
||||
// 创建返回对象
|
||||
int pinyinStrSize = pinyinStr.length;
|
||||
char[] headChars = new char[pinyinStrSize];
|
||||
int i = 0;
|
||||
// 截取首字符
|
||||
for (String s : pinyinStr) {
|
||||
char headChar = s.charAt(0);
|
||||
// 首字母是否大写,默认是小写
|
||||
if (isCapital) {
|
||||
headChars[i] = Character.toUpperCase(headChar);
|
||||
} else {
|
||||
headChars[i] = headChar;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return headChars;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取汉字的首字母(默认是大写)
|
||||
*
|
||||
* @param src
|
||||
* @return
|
||||
*/
|
||||
public static char[] getHeadByChar(char src) {
|
||||
return getHeadByChar(src, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找字符串首字母
|
||||
*
|
||||
* @param src
|
||||
* @return
|
||||
*/
|
||||
public static String[] getHeadByString(String src) {
|
||||
return getHeadByString(src, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找字符串首字母
|
||||
*
|
||||
* @param src
|
||||
* @param isCapital 是否大写
|
||||
* @return
|
||||
*/
|
||||
public static String[] getHeadByString(String src, boolean isCapital) {
|
||||
return getHeadByString(src, isCapital, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找字符串首字母
|
||||
*
|
||||
* @param src 汉字字符串
|
||||
* @param isCapital 是否大写
|
||||
* @param separator 分隔符
|
||||
* @return
|
||||
*/
|
||||
public static String[] getHeadByString(String src, boolean isCapital, String separator) {
|
||||
char[] chars = src.toCharArray();
|
||||
String[] headString = new String[chars.length];
|
||||
int i = 0;
|
||||
for (char ch : chars) {
|
||||
char[] chs = getHeadByChar(ch, isCapital);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (null != separator) {
|
||||
int j = 1;
|
||||
for (char ch1 : chs) {
|
||||
sb.append(ch1);
|
||||
if (j != chs.length) {
|
||||
sb.append(separator);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
} else {
|
||||
sb.append(chs[0]);
|
||||
}
|
||||
headString[i] = sb.toString();
|
||||
i++;
|
||||
}
|
||||
return headString;
|
||||
}
|
||||
|
||||
public static boolean isChinese(String str) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
char c = str.charAt(i);
|
||||
if (c >= '\u4e00' && c <= '\u9fa5') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -2,12 +2,14 @@ package com.accompany.common.tencent.cos;
|
||||
|
||||
import com.accompany.common.config.TencentCosConfig;
|
||||
import com.accompany.common.status.BusiStatus;
|
||||
import com.accompany.common.utils.PinYin4JUtil;
|
||||
import com.accompany.core.exception.ServiceException;
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.model.PutObjectResult;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sourceforge.pinyin4j.PinyinHelper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -20,15 +22,20 @@ public class TencentCosUploadService {
|
||||
|
||||
@Autowired
|
||||
private TencentCosConfig config;
|
||||
|
||||
@Autowired
|
||||
private COSClient client;
|
||||
|
||||
@SneakyThrows
|
||||
public String uploadByStream(InputStream is, String fileName) {
|
||||
if (StringUtils.isBlank(fileName)){
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
throw new ServiceException(BusiStatus.PARAMERROR);
|
||||
}
|
||||
|
||||
if (PinYin4JUtil.isChinese(fileName)) {
|
||||
String pinyinName = PinYin4JUtil.hanziToPinyin(fileName);
|
||||
log.info("fileName : {}, pinyinName : {}", fileName, pinyinName);
|
||||
fileName = pinyinName;
|
||||
}
|
||||
PutObjectRequest request = new PutObjectRequest(config.getBucket(), fileName, is, null);
|
||||
PutObjectResult result = client.putObject(request);
|
||||
log.info("[腾讯云cos] {} 上传成功, requestId {}", fileName, result.getRequestId());
|
||||
|
@@ -101,6 +101,7 @@
|
||||
<zxing.version>3.2.0</zxing.version>
|
||||
<bitwalker.version>1.20</bitwalker.version>
|
||||
<sa-token.version>1.37.0</sa-token.version>
|
||||
<pinyin4j.version>2.5.1</pinyin4j.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -549,6 +550,11 @@
|
||||
<version>${tencentcloud-cos-sdk-java.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.belerweb</groupId>
|
||||
<artifactId>pinyin4j</artifactId>
|
||||
<version>${pinyin4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -941,6 +947,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.belerweb</groupId>
|
||||
<artifactId>pinyin4j</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
Reference in New Issue
Block a user