调整文件上传方法,新增上传Excel文件异常处理,添加中文拼音转换

This commit is contained in:
wenc000 2020-01-07 17:49:04 +08:00
parent 23b9b21904
commit 0d7caf1781
7 changed files with 166 additions and 5 deletions

View File

@ -17,6 +17,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.InputStreamSource;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@ -126,6 +127,10 @@ public class ApiLogAspect {
if (arg instanceof WebStatFilter.StatHttpServletResponseWrapper) {
continue;
}
// 文件过滤
if (arg instanceof InputStreamSource) {
continue;
}
if (paramValue.length() > 0) {
paramValue.append("_wg_");
}

View File

@ -150,5 +150,20 @@ public interface IFileService {
*/
FilePO getFile(Map<String, Object> params) throws SearchException;
/**
* 保存导入Excel异常文件信息
*
* @param fileName
* @param uploadPath
* @param fileSize
* @param params
*/
void saveUploadErrorExcelFileInfo(String fileName, String uploadPath, long fileSize, Map<String, Object> params);
/**
* 获取导入Excel异常文件路径
*
* @return
*/
String getUploadExcelErrorPath();
}

View File

@ -93,7 +93,6 @@ public class FileServiceImpl extends AbstractService implements IFileService {
* @throws SystemException
*/
private void uploadFile(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map<String, Object> params) throws SystemException {
String baseUploadPath = fileProperties.getUploadPath();
if (StringUtils.isBlank(baseUploadPath)) {
throw new SystemException("上传路径未配置");
@ -110,6 +109,34 @@ public class FileServiceImpl extends AbstractService implements IFileService {
if (!uploadFile(uploadFile, uploadPath, uploadFileName)) {
throw new SaveException("文件上传失败");
}
saveUploadFileInfo(token, fileName, uploadPath, uploadFileName, fileType, fileSize, params);
}
@Override
public void saveUploadErrorExcelFileInfo(String fileName, String uploadPath, long fileSize, Map<String, Object> params) {
saveUploadFileInfo(null, fileName, uploadPath, fileName, "xls", fileSize, params);
}
@Override
public String getUploadExcelErrorPath() {
String baseUploadPath = fileProperties.getUploadPath();
if (StringUtils.isBlank(baseUploadPath)) {
throw new SystemException("上传路径未配置");
}
return getUploadPath(baseUploadPath, UploadTypeEnum.ERROR_EXCEL, null);
}
/**
* 保存文件信息
*
* @param token
* @param fileName
* @param uploadPath
* @param uploadFileName
* @param fileType
* @param fileSize
*/
private void saveUploadFileInfo(String token, String fileName, String uploadPath, String uploadFileName, String fileType, long fileSize, Map<String, Object> params) {
String fixPath = uploadPath.replace(fileProperties.getUploadPath(), "");
if ("\\".equals(File.separator)) {
fixPath = fixPath.replace("\\", "/");
@ -389,6 +416,8 @@ public class FileServiceImpl extends AbstractService implements IFileService {
throw new FileException("音频格式不支持上传");
}
filePath.append("audios");
} else if (uploadTypeEnum.getValue() == UploadTypeEnum.ERROR_EXCEL.getValue()) {
filePath.append("errorexcels");
} else {
if (hasFileType && !isTypeCorrect(getFileTypes(), fileType)) {
throw new FileException("文件格式不支持上传");

View File

@ -18,6 +18,7 @@ import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -219,4 +220,20 @@ public abstract class AbstractService {
throw new SearchException(resultObj.getString("msg"));
}
}
/**
* 简单Excel的Header
*
* @param names
* @return
*/
protected List<List<String>> simpleExcelHeader(String... names) {
List<List<String>> listHeaders = new ArrayList<>();
for (String name : names) {
List<String> listHeader = new ArrayList<>();
listHeader.add(name);
listHeaders.add(listHeader);
}
return listHeaders;
}
}

View File

@ -9,7 +9,7 @@ package com.cm.common.enums;
**/
public enum UploadTypeEnum {
FILE(1), IMAGE(2), VIDEO(3), AUDIO(4);
FILE(1), IMAGE(2), VIDEO(3), AUDIO(4), ERROR_EXCEL(5);
private int value;
UploadTypeEnum(int value) {

View File

@ -1,6 +1,13 @@
package com.cm.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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -167,7 +174,7 @@ public class WStringUtil {
if (letter.length() == 0) {
continue;
}
if(i == 0) {
if (i == 0) {
sb.append(letter);
} else {
int firstLetter = letter.charAt(0);
@ -206,4 +213,73 @@ public class WStringUtil {
return result;
}
/**
* 获取拼音
*
* @param src
* @return
*/
public static String getPingYin(String src) {
char[] t1 = null;
t1 = src.toCharArray();
String[] t2 = new String[t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
String t4 = "";
int t0 = t1.length;
try {
for (int i = 0; i < t0; i++) {
// 判断是否为汉字字符
if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4 += t2[0];
} else {
t4 += java.lang.Character.toString(t1[i]);
}
}
return t4;
} catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4;
}
/**
* 获取中文首字母
*
* @param str
* @return
*/
public static String getPinYinHeadChar(String str) {
String convert = "";
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert += pinyinArray[0].charAt(0);
} else {
convert += word;
}
}
return convert;
}
/**
* 汉字转ASCII码
*
* @param cnStr
* @return
*/
public static String getCnASCII(String cnStr) {
StringBuffer strBuf = new StringBuffer();
byte[] bGBK = cnStr.getBytes();
for (int i = 0; i < bGBK.length; i++) {
// System.out.println(Integer.toHexString(bGBK[i]&0xff));
strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
}
return strBuf.toString();
}
}

21
pom.xml
View File

@ -26,6 +26,7 @@
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<durid.version>1.0.31</durid.version>
<fastjson.version>1.2.24</fastjson.version>
<easyexcel.version>2.0.5</easyexcel.version>
<pagehelper.version>5.1.1</pagehelper.version>
<httpclient.version>4.5.6</httpclient.version>
<jodaTime.version>2.9.4</jodaTime.version>
@ -55,6 +56,7 @@
<jedis.version>2.9.0</jedis.version>
<swagger.version>2.5.0</swagger.version>
<zxing.version>3.3.3</zxing.version>
<pingyin4j.version>2.5.1</pingyin4j.version>
</properties>
<dependencies>
@ -102,12 +104,21 @@
</dependency>
<!-- Spring end -->
<!-- fastjson -->
<!-- fastjson start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- fastjson end -->
<!-- easyexcel start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
<!-- easyexcel end -->
<!-- MySQL start -->
<dependency>
@ -317,6 +328,14 @@
<version>${zxing.version}</version>
</dependency>
<!-- zxing end -->
<!-- pingyin start -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>${pingyin4j.version}</version>
</dependency>
<!-- pingyin end -->
</dependencies>
<dependencyManagement>