From 0d7caf1781d3e4ace0b187eb9638eeb73a528506 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Tue, 7 Jan 2020 17:49:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=96=B9=E6=B3=95=EF=BC=8C=E6=96=B0=E5=A2=9E=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0Excel=E6=96=87=E4=BB=B6=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=B8=AD=E6=96=87=E6=8B=BC?= =?UTF-8?q?=E9=9F=B3=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cm/common/aspect/ApiLogAspect.java | 5 ++ .../plugin/service/file/IFileService.java | 15 ++++ .../service/file/impl/FileServiceImpl.java | 31 ++++++- .../com/cm/common/base/AbstractService.java | 17 ++++ .../com/cm/common/enums/UploadTypeEnum.java | 2 +- .../java/com/cm/common/utils/WStringUtil.java | 80 ++++++++++++++++++- pom.xml | 21 ++++- 7 files changed, 166 insertions(+), 5 deletions(-) diff --git a/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiLogAspect.java b/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiLogAspect.java index b6e41ea..9a7b7e6 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiLogAspect.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/aspect/ApiLogAspect.java @@ -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_"); } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java index 9e6f8ee..89dd3eb 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java @@ -150,5 +150,20 @@ public interface IFileService { */ FilePO getFile(Map params) throws SearchException; + /** + * 保存导入Excel异常文件信息 + * + * @param fileName + * @param uploadPath + * @param fileSize + * @param params + */ + void saveUploadErrorExcelFileInfo(String fileName, String uploadPath, long fileSize, Map params); + /** + * 获取导入Excel异常文件路径 + * + * @return + */ + String getUploadExcelErrorPath(); } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java index 96cc0f1..2fed262 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java @@ -93,7 +93,6 @@ public class FileServiceImpl extends AbstractService implements IFileService { * @throws SystemException */ private void uploadFile(String token, MultipartFile uploadFile, UploadTypeEnum uploadTypeEnum, Map 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 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 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("文件格式不支持上传"); diff --git a/cloud-common/src/main/java/com/cm/common/base/AbstractService.java b/cloud-common/src/main/java/com/cm/common/base/AbstractService.java index 5ab8e72..cc009cb 100644 --- a/cloud-common/src/main/java/com/cm/common/base/AbstractService.java +++ b/cloud-common/src/main/java/com/cm/common/base/AbstractService.java @@ -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> simpleExcelHeader(String... names) { + List> listHeaders = new ArrayList<>(); + for (String name : names) { + List listHeader = new ArrayList<>(); + listHeader.add(name); + listHeaders.add(listHeader); + } + return listHeaders; + } } diff --git a/cloud-common/src/main/java/com/cm/common/enums/UploadTypeEnum.java b/cloud-common/src/main/java/com/cm/common/enums/UploadTypeEnum.java index 54fe82a..5fc7fc3 100644 --- a/cloud-common/src/main/java/com/cm/common/enums/UploadTypeEnum.java +++ b/cloud-common/src/main/java/com/cm/common/enums/UploadTypeEnum.java @@ -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) { diff --git a/cloud-common/src/main/java/com/cm/common/utils/WStringUtil.java b/cloud-common/src/main/java/com/cm/common/utils/WStringUtil.java index 262bb97..70e0e79 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/WStringUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/WStringUtil.java @@ -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,9 +174,9 @@ public class WStringUtil { if (letter.length() == 0) { continue; } - if(i == 0) { + if (i == 0) { sb.append(letter); - } else { + } else { int firstLetter = letter.charAt(0); firstLetter -= 32; sb.append((char) firstLetter).append(letter.substring(1)); @@ -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(); + } + } diff --git a/pom.xml b/pom.xml index e4c9378..a47f556 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,7 @@ Greenwich.RELEASE 1.0.31 1.2.24 + 2.0.5 5.1.1 4.5.6 2.9.4 @@ -55,6 +56,7 @@ 2.9.0 2.5.0 3.3.3 + 2.5.1 @@ -102,12 +104,21 @@ - + com.alibaba fastjson ${fastjson.version} + + + + + com.alibaba + easyexcel + ${easyexcel.version} + + @@ -317,6 +328,14 @@ ${zxing.version} + + + + com.belerweb + pinyin4j + ${pingyin4j.version} + +