From 7c42f2789a5a869dc6ec2583ad09a641db20565f Mon Sep 17 00:00:00 2001 From: wujingtao Date: Sat, 7 Aug 2021 17:14:05 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=A4=96=E9=83=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/constant/DefaultConstant.java | 4 + .../formdata/service/FormdataService.java | 4 +- .../service/impl/FormdataServiceImpl.java | 129 +++--------------- .../security/config/SecurityConfig.java | 6 +- .../uploadnew/httpRequest/HttpClient.java | 55 -------- .../uploadnew/httpRequest/SSLClient.java | 42 ------ .../uploadnew/listener/EasyExcelListener.java | 1 - .../rest/TbUploadFileNewController.java | 79 +++++------ .../modules/uploadnew/service/dto/OosDTO.java | 2 +- .../service/impl/BuildPathUtils.java | 94 +++++++++++++ .../mapstruct/TbUploadFileNewMapper.java | 36 ++--- .../uploadnew/task/TransFormDataTask.java | 63 ++++++++- .../src/main/resources/config/application.yml | 12 +- 13 files changed, 246 insertions(+), 281 deletions(-) delete mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/HttpClient.java delete mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/SSLClient.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/BuildPathUtils.java diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/constant/DefaultConstant.java b/eladmin-system/src/main/java/me/zhengjie/modules/constant/DefaultConstant.java index 757d7c7..20693ab 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/constant/DefaultConstant.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/constant/DefaultConstant.java @@ -24,6 +24,10 @@ public class DefaultConstant { */ public static final int TWO_NUMBER = 2; + /** + * 3 + */ + public static final int THREE_NUMBER = 3; /** * 5 diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/FormdataService.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/FormdataService.java index 03039df..f89e029 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/FormdataService.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/FormdataService.java @@ -96,7 +96,7 @@ public interface FormdataService { * @param file 需要进行处理的文件对象 * @return 是否处理成功 */ - boolean parseFileAndSaveToDB(MultipartFile file); + List parseFileAndSaveToDB(MultipartFile file); /** * 保存音频文件在本地 @@ -111,5 +111,5 @@ public interface FormdataService { * * @param file */ - void uploadOOS(MultipartFile file); + void uploadOOS(MultipartFile file, List list); } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java index b2c8d36..fc4bbff 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java @@ -15,22 +15,12 @@ */ package me.zhengjie.modules.formdata.service.impl; -import cn.hutool.core.date.DateUnit; -import cn.hutool.core.date.DateUtil; -import cn.hutool.core.util.IdUtil; -import cn.hutool.system.OsInfo; -import cn.hutool.system.SystemUtil; -import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcelFactory; -import com.alibaba.excel.ExcelReader; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; -import com.aliyun.oss.model.PutObjectRequest; -import lombok.Data; +import com.aliyun.oss.model.PutObjectResult; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import me.zhengjie.common.http.CommonResponse; -import me.zhengjie.common.http.ResponseCode; import me.zhengjie.modules.formdata.domain.Formdata; import me.zhengjie.modules.formdata.repository.FormdataRepository; import me.zhengjie.modules.formdata.service.FormdataService; @@ -42,6 +32,7 @@ import me.zhengjie.modules.uploadnew.service.dto.ExcelDataDTO; import me.zhengjie.modules.uploadnew.service.dto.OosDTO; import me.zhengjie.modules.uploadnew.service.dto.WavDTO; import me.zhengjie.modules.uploadnew.task.TransFormDataTask; +import me.zhengjie.modules.uploadnew.service.impl.BuildPathUtils; import me.zhengjie.utils.FileUtil; import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.QueryHelp; @@ -58,7 +49,6 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.nio.charset.Charset; -import java.text.SimpleDateFormat; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -80,26 +70,18 @@ public class FormdataServiceImpl implements FormdataService { private final FormdataMapper formdataMapper; @Autowired private final TransFormDataTask transFormDataTask; - /** - * 远程上传临时存放文件地址 - linux环境 - */ - @Value("${remote.link.file-base-path-linux}") - private String remoteLinkFileBasePathLinux; - /** - * 远程上传临时存放文件地址 - linux环境 - */ - @Value("${remote.link.file-base-path-windows}") - private String remoteLinkFileBasePathWindows; - - /** - * 远程上传临时存放文件地址 - linux环境 - */ - @Value("${remote.link.file-base-path-mac}") - private String remoteLinkFileBasePathMac; - - private List list = new ArrayList<>(); + @Autowired + private BuildPathUtils buildPathUtils; + @Value("${upload.accessKeyId}") + private String accessKeyId; + @Value("${upload.accessKeySecret}") + private String accessKeySecret; + @Value("${upload.bucketName}") + private String bucketName; + @Value("${upload.endpoint}") + private String endpoint; @Override public Map queryAll(FormdataQueryCriteria criteria, Pageable pageable) { @@ -166,7 +148,7 @@ public class FormdataServiceImpl implements FormdataService { * @return 是否操作成功 */ @Override - public boolean parseFileAndSaveToDB(MultipartFile file) { + public List parseFileAndSaveToDB(MultipartFile file) { List formData; // 1. todo 先进行解析 excel 文件 try { @@ -177,15 +159,11 @@ public class FormdataServiceImpl implements FormdataService { formData = listener.getData(); // 2. todo 把解析后的结果进行入库 formdataRepository.saveAll(formData); - list = formdataMapper.toDto(formData); -// - + return formdataMapper.toDto(formData); } catch (Exception e) { log.error("解析失败"); - e.printStackTrace(); - return false; + throw new RuntimeException(""); } - return true; } /** @@ -200,8 +178,7 @@ public class FormdataServiceImpl implements FormdataService { //文件名 String fileName = file.getOriginalFilename(); //文件保存地址 - String path = buildFilePath(fileName); - + String path = buildPathUtils.buildFilePath(); OutputStream os = null; InputStream inputStream = null; try { @@ -244,24 +221,20 @@ public class FormdataServiceImpl implements FormdataService { * @return */ @Override - public void uploadOOS(MultipartFile file) { - String accessKeyId = "LTAI5t8agDMumxcjAVuuwqPY"; - String accessKeySecret = "i93lzrZwb2BUgtB9r0y0LT2Dt7tNbP"; - String bucketName = "hjrecording"; - String endpoint = "oss-cn-shanghai.aliyuncs.com"; + public void uploadOOS(MultipartFile file, List list) { String fileName = file.getOriginalFilename(); String subFixFile = StringUtils.substringAfterLast(fileName, SPLIT_FILE_SYMBOL); - OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); List wavList = zipUtil(file); for (WavDTO wavDTO : wavList) { for (FormdataDto info : list) { if (wavDTO.getName().equals(info.getPhone())) { - //上传oos - String newFileName = buildFileOosPath(subFixFile); - ossClient.putObject(bucketName, newFileName, wavDTO.getLine()); + //上传路径 + String newFileName = buildPathUtils.buildFileOosPath(subFixFile); + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); + PutObjectResult putObjectResult = ossClient.putObject(bucketName, newFileName, wavDTO.getLine()); ossClient.shutdown(); OosDTO oosDTO = toBean(info, newFileName); //发送下游 @@ -272,7 +245,6 @@ public class FormdataServiceImpl implements FormdataService { } - private List zipUtil(MultipartFile file) { List wavDTOS = new ArrayList<>(); String zipFileName = null; @@ -299,7 +271,7 @@ public class FormdataServiceImpl implements FormdataService { private OosDTO toBean(FormdataDto info, String newFileName) { OosDTO oosDTO = new OosDTO(); - oosDTO.setId(info.getSourceId()); + oosDTO.setId(info.getSourceId().longValue()); oosDTO.setAge(info.getAge()); oosDTO.setCity(info.getCity()); oosDTO.setName(info.getName()); @@ -308,61 +280,4 @@ public class FormdataServiceImpl implements FormdataService { oosDTO.setVoiceaddr(newFileName); return oosDTO; } - - /** - * 构建文件上传保存路径 - */ - private String buildFilePath(String fileName) { - // 获取环境配置信息 - OsInfo osInfo = SystemUtil.getOsInfo(); - // 定义的时间格式 - String timeFormate = DateUtil.today() ; - - String dirPath; - if (osInfo.isWindows()) { - dirPath = remoteLinkFileBasePathWindows + timeFormate; - FileUtil.mkdir(new File(dirPath)); - // 构建存储文件 - return dirPath; - } else if (osInfo.isLinux()) { - dirPath = remoteLinkFileBasePathLinux + timeFormate; - FileUtil.mkdir(new File(dirPath)); - // 构建存储文件 - return dirPath; - } else if (osInfo.isMac()) { - dirPath = remoteLinkFileBasePathMac + timeFormate; - FileUtil.mkdir(new File(dirPath)); - // 构建存储文件 - return dirPath; - } else { - return ""; - } - } - - /** - * 构建文件上传保存路径 - */ - private String buildFileOosPath(String subFixFile) { - OsInfo osInfo = SystemUtil.getOsInfo(); - String timeFormate = DateUtil.date() + "" + (Math.random() * 9 + 1) * 10000 + "." + subFixFile; - String dirPath; - if (osInfo.isWindows()) { - dirPath = remoteLinkFileBasePathWindows + timeFormate; - FileUtil.mkdir(new File(dirPath)); - // 构建存储文件 - return dirPath; - } else if (osInfo.isLinux()) { - dirPath = remoteLinkFileBasePathLinux + timeFormate; - FileUtil.mkdir(new File(dirPath)); - // 构建存储文件 - return dirPath; - } else if (osInfo.isMac()) { - dirPath = remoteLinkFileBasePathMac + timeFormate; - FileUtil.mkdir(new File(dirPath)); - // 构建存储文件 - return dirPath; - } else { - return ""; - } - } } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/config/SecurityConfig.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/config/SecurityConfig.java index eb16fb4..fba604f 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/config/SecurityConfig.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/config/SecurityConfig.java @@ -135,9 +135,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { // 所有类型的接口都放行 .antMatchers(anonymousUrls.get(RequestMethodEnum.ALL.getType()).toArray(new String[0])).permitAll() // 所有请求都需要认证 -// .anyRequest().authenticated() - .antMatchers().permitAll() - .anyRequest().permitAll() + .anyRequest().authenticated() +// .antMatchers().permitAll() +// .anyRequest().permitAll() .and().apply(securityConfigurerAdapter()); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/HttpClient.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/HttpClient.java deleted file mode 100644 index 314f44f..0000000 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/HttpClient.java +++ /dev/null @@ -1,55 +0,0 @@ -package me.zhengjie.modules.uploadnew.httpRequest; - -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.util.EntityUtils; - -import java.util.Map; - -/** - * @author wjt - * @date 2021/8/6 - */ -public class HttpClient { - /** - * token渠道代号为2个: - * 自营渠道:b1540b934765484d9364792914be0027 - * 收量渠道:d2170b4d14c44f75aec196f2c8b911fc - */ - private static final String URL = "https://app.hjdata.com/api/dymaticform/channel/save?formId=1&token=xxdfdfgdgd"; - - public static String doPost(String map, String charset) { - org.apache.http.client.HttpClient httpClient = null; - HttpPost httpPost = null; - String result = null; - try { - httpClient = new SSLClient(); - httpPost = new HttpPost(URL); - //设置参数 - httpPost.addHeader("Accept", "application/json"); - httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); - StringEntity stringEntity = new StringEntity(map); - stringEntity.setContentEncoding("UTF-8"); - stringEntity.setContentType("application/json"); - httpPost.setEntity(stringEntity); - HttpResponse response = httpClient.execute(httpPost); - if (response != null) { - HttpEntity resEntity = response.getEntity(); - if (resEntity != null) { - result = EntityUtils.toString(resEntity, charset); - } - } - } catch (Exception ex) { - ex.printStackTrace(); - } - JSONObject json = JSONUtil.parseObj(result); - if (Integer.parseInt(json.get("code").toString()) == 200) { - - } - return result; - } -} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/SSLClient.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/SSLClient.java deleted file mode 100644 index e006bd1..0000000 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/SSLClient.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.zhengjie.modules.uploadnew.httpRequest; - -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -import org.apache.http.conn.ClientConnectionManager; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.impl.client.DefaultHttpClient; - -//用于进行Https请求的HttpClient -public class SSLClient extends DefaultHttpClient { - public SSLClient() throws Exception { - super(); - SSLContext ctx = SSLContext.getInstance("TLS"); - X509TrustManager tm = new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, - String authType) throws CertificateException { - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, - String authType) throws CertificateException { - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; - } - }; - ctx.init(null, new TrustManager[]{tm}, null); - SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); - ClientConnectionManager ccm = this.getConnectionManager(); - SchemeRegistry sr = ccm.getSchemeRegistry(); - sr.register(new Scheme("https", 443, ssf)); - } -} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/listener/EasyExcelListener.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/listener/EasyExcelListener.java index 4bc9a69..e4139a6 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/listener/EasyExcelListener.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/listener/EasyExcelListener.java @@ -1,7 +1,6 @@ package me.zhengjie.modules.uploadnew.listener; import cn.hutool.core.date.DateUtil; -import cn.hutool.core.lang.ObjectId; import cn.hutool.core.util.IdUtil; import cn.hutool.json.JSONUtil; import com.alibaba.excel.context.AnalysisContext; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java index cbfb5e5..ad8f62d 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java @@ -30,17 +30,15 @@ import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.constant.DefaultConstant; import me.zhengjie.modules.formdata.service.FormdataService; import me.zhengjie.modules.formdata.service.dto.FormdataDto; -import me.zhengjie.modules.formdata.service.mapstruct.FormdataMapper; import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; import me.zhengjie.modules.uploadnew.redis.RedisParentDao; import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria; -import me.zhengjie.modules.uploadnew.service.mapstruct.TbUploadFileNewMapper; import me.zhengjie.modules.uploadnew.task.SaveToFileNewTask; -import me.zhengjie.modules.uploadnew.task.TransFormDataTask; import me.zhengjie.utils.SecurityUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.data.redis.core.RedisTemplate; @@ -64,7 +62,6 @@ import static me.zhengjie.modules.constant.FileConstant.*; import static me.zhengjie.modules.upload.consts.UploadFileConst.FILE_PATH_SPLIT; import static me.zhengjie.modules.uploadnew.consts.SysConstNew.*; -//import org.springframework.util.StringUtils; /** * @author weizhongxi @@ -78,6 +75,7 @@ import static me.zhengjie.modules.uploadnew.consts.SysConstNew.*; @Slf4j public class TbUploadFileNewController { + private TbUploadFileNewService tbUploadFileNewService; private SaveToFileNewTask saveToFileNewTask; @@ -85,9 +83,6 @@ public class TbUploadFileNewController { private RedisTemplate redisTemplate; private FormdataService formdataService; - @Autowired - private TbUploadFileNewMapper tbUploadFileNewMapper; - @Autowired public TbUploadFileNewController(TbUploadFileNewService tbUploadFileNewService, SaveToFileNewTask saveToFileNewTask, RedisTemplate redisTemplate, FormdataService formdataService) { @@ -177,16 +172,43 @@ public class TbUploadFileNewController { log.error("保存失败"); } int finishTag = 0; + List list = new ArrayList<>(); for (MultipartFile file : files) { String originalFilename = file.getOriginalFilename(); - if (StringUtils.isNotBlank(originalFilename)) { String subOriginalFilename = StringUtils.substringAfterLast(originalFilename, SPLIT_FILE_SYMBOL); - finishTag = handleEachUploadFile(file, subOriginalFilename, finishTag, resourcesDto); + if (XLSX_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { + // TODO: 2021/8/5 0005 解析 入库 + list = formdataService.parseFileAndSaveToDB(file); + if (CollectionUtil.isNotEmpty(list)) { + finishTag += 1; + } + } + if (RAR_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename) + || ZIP_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { + // TODO: 2021/8/5 0005 以 压缩文件结尾的格式 则直接进行保存文件到指定文件夹目录下 + String localSavePath = formdataService.parseMediaFileToLocal(file); + if (StringUtils.isNotBlank(localSavePath)) { + // todo 记录保存位置 + resourcesDto.setLocalSavePath(localSavePath); + resourcesDto.setFileFormat(subOriginalFilename); + finishTag += 1; + } + } +// finishTag = handleEachUploadFile(file, subOriginalFilename, finishTag, resourcesDto, list); } } + for (MultipartFile file : files) { + String subOriginalFilename = StringUtils.substringAfterLast(file.getOriginalFilename(), SPLIT_FILE_SYMBOL); + if (CollectionUtil.isNotEmpty(list)) { + if (RAR_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename) || ZIP_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { + // 3. todo 上传oos 并 异步发送给 下游接口 + formdataService.uploadOOS(file, list); + } + } + } // 确保所有文件都经过了响应的处理 if (finishTag != files.length) { // todo 上传完成更新文件上传记录 @@ -197,45 +219,14 @@ public class TbUploadFileNewController { // todo 上传完成更新文件上传记录 resourcesDto.setUploadTag(DefaultConstant.TWO_NUMBER); resourcesDto.setGmtModified(DateUtil.date().toTimestamp()); - tbUploadFileNewService.create(tbUploadFileNewMapper.toEntity(resourcesDto)); - return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); - - } - - /** - * 经过 finishTag 个步骤进行处理,然后返回处理后的步骤计数 - * - * @param subOriginalFilename 文件后缀名 - * @return 处理后的步骤计数 - */ - private int handleEachUploadFile(MultipartFile file, String subOriginalFilename, int finishTag, TbUploadFileNewDto resourcesDto) { + TbUploadFileNew tbUploadFileNew = new TbUploadFileNew(); + BeanUtils.copyProperties(resourcesDto, tbUploadFileNew); + tbUploadFileNewService.create(tbUploadFileNew); + return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); - if (XLSX_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { - // TODO: 2021/8/5 0005 解析 入库 - if (formdataService.parseFileAndSaveToDB(file)) { - finishTag += 1; - } - } - - if (RAR_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename) - || ZIP_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { - - // TODO: 2021/8/5 0005 以 压缩文件结尾的格式 则直接进行保存文件到指定文件夹目录下 - String localSavePath = formdataService.parseMediaFileToLocal(file); - if (StringUtils.isNotBlank(localSavePath)) { - // todo 记录保存位置 - resourcesDto.setLocalSavePath(localSavePath); - resourcesDto.setFileFormat(subOriginalFilename); - finishTag += 1; - } - // 3. todo 上传oos 并 异步发送给 下游接口 - formdataService.uploadOOS(file); - } - return finishTag; } - /** * 文件上传接口 * diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/OosDTO.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/OosDTO.java index b6e0790..2e20730 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/OosDTO.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/OosDTO.java @@ -8,7 +8,7 @@ import lombok.Data; */ @Data public class OosDTO { - private Integer id; + private Long id; private String name; private Integer age; private String province; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/BuildPathUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/BuildPathUtils.java new file mode 100644 index 0000000..c98fc84 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/BuildPathUtils.java @@ -0,0 +1,94 @@ +package me.zhengjie.modules.uploadnew.service.impl; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.system.OsInfo; +import cn.hutool.system.SystemUtil; +import me.zhengjie.utils.FileUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.File; + +/** + * @author wjt + * @date 2021/8/6 + */ +@Service +public class BuildPathUtils { + + /** + * 远程上传临时存放文件地址 - linux环境 + */ + @Value("${remote.link.file-base-path-linux}") + private String fileBasePathLinux; + + /** + * 远程上传临时存放文件地址 - linux环境 + */ + @Value("${remote.link.file-base-path-windows}") + private String fileBasePathWindows; + + /** + * 远程上传临时存放文件地址 - linux环境 + */ + @Value("${remote.link.file-base-path-mac}") + private String fileBasePathMac; + + /** + * 构建文件上传保存路径 + */ + public String buildFilePath() { + // 获取环境配置信息 + OsInfo osInfo = SystemUtil.getOsInfo(); + // 定义的时间格式 + String timeFormate = DateUtil.today(); + + String dirPath; + if (osInfo.isWindows()) { + dirPath = fileBasePathWindows + timeFormate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else if (osInfo.isLinux()) { + dirPath = fileBasePathLinux + timeFormate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else if (osInfo.isMac()) { + + dirPath = fileBasePathMac + timeFormate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else { + return ""; + } + } + + /** + * 构建文件上传保存路径 + */ + public String buildFileOosPath(String subFixFile) { + OsInfo osInfo = SystemUtil.getOsInfo(); + String timeFormate = DateUtil.now() + "" + (Math.random() * 9 + 1) * 10000 + "." + subFixFile; + String dirPath; + if (osInfo.isWindows()) { + dirPath = fileBasePathWindows + timeFormate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else if (osInfo.isLinux()) { + dirPath = fileBasePathLinux + timeFormate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else if (osInfo.isMac()) { + dirPath = fileBasePathMac + timeFormate; + FileUtil.mkdir(new File(dirPath)); + // 构建存储文件 + return dirPath; + } else { + return ""; + } + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/mapstruct/TbUploadFileNewMapper.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/mapstruct/TbUploadFileNewMapper.java index 2c11a58..5338155 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/mapstruct/TbUploadFileNewMapper.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/mapstruct/TbUploadFileNewMapper.java @@ -1,18 +1,18 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package me.zhengjie.modules.uploadnew.service.mapstruct; import me.zhengjie.base.BaseMapper; @@ -22,10 +22,10 @@ import org.mapstruct.Mapper; import org.mapstruct.ReportingPolicy; /** -* @website https://el-admin.vip -* @author weizhongxi -* @date 2021-04-14 -**/ + * @author weizhongxi + * @website https://el-admin.vip + * @date 2021-04-14 + **/ @Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) public interface TbUploadFileNewMapper extends BaseMapper { diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java index 3c2efc4..825ed1a 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java @@ -1,25 +1,41 @@ package me.zhengjie.modules.uploadnew.task; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import lombok.extern.slf4j.Slf4j; -import me.zhengjie.modules.formdata.service.dto.FormdataDto; -import me.zhengjie.modules.uploadnew.httpRequest.HttpClient; +import me.zhengjie.exception.BadRequestException; +import me.zhengjie.modules.constant.DefaultConstant; import me.zhengjie.modules.uploadnew.service.dto.OosDTO; import me.zhengjie.utils.ConvertUtil; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.time.ZoneOffset; -import java.util.List; + @Component @Scope("prototype") @Slf4j public class TransFormDataTask { + @Value("${upload.url}") + private String url; +// private static String url = "https://app.hjdata.com/api/dymaticform/channel/save?"; + /** + * token渠道代号为2个: + * 自营渠道:b1540b934765484d9364792914be0027 + * 收量渠道:d2170b4d14c44f75aec196f2c8b911fc + */ + private final static String TOKEN = "d2170b4d14c44f75aec196f2c8b911fc"; + /** + * @param oosDTO + */ @Async(value = "SendBigDataTaskExecutor") public void doRunTask(OosDTO oosDTO) { Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); @@ -30,7 +46,44 @@ public class TransFormDataTask { } private void runTask(OosDTO oosDTO) { -// String jsonStr = JSONUtil.toJsonStr(oosDTO); -// HttpClient.doPost(jsonStr, "utf-8"); + String newUrl = url + "formId=3" + "&token=" + TOKEN; + String jsonStr = JSONUtil.toJsonStr(oosDTO); + int count = 0; + while (count < DefaultConstant.THREE_NUMBER) { + // 调用HTTP请求发送数据 + HttpResponse httpResponse = sendPostReq(jsonStr, newUrl); + JSONObject object = JSONUtil.parseObj(httpResponse.body()); + if (httpResponse.isOk() && object.get("retcode").equals(DefaultConstant.ZERO_NUMBER)) { + log.info("========== [SaveToFileTask|batchSendToEncrypt request success, response is {} ] ==========", object); + break; + } else { + count++; + try { + // 重新发送前休眠3秒 + Thread.sleep(3_0000); + } catch (InterruptedException e) { + log.error("SaveToFileTask|batchSendToEncrypt sleep ERROR. message is", e.getMessage(), e); + throw new BadRequestException("请求异常!!!"); + } + log.error("========== [SaveToFileTask|batchSendToEncrypt request fail, response is {} ] ==========", httpResponse.body()); + } + } + if (count >= DefaultConstant.THREE_NUMBER) { + log.error("========== [SaveToFileTask|batchSendToEncrypt update send status fail, url is {} ] ==========", newUrl); + } + } + + /** + * 调用HTTP请求发送Post请求 + * + * @param json 请求的body内容 + * @return 返回请求结果 + */ + private HttpResponse sendPostReq(String json, String newUrl) { + return HttpRequest + .post(newUrl) + .header("Content-Type", "application/json;charset=utf-8") + .body(json) + .execute(); } } diff --git a/eladmin-system/src/main/resources/config/application.yml b/eladmin-system/src/main/resources/config/application.yml index 91494c9..e4206c1 100644 --- a/eladmin-system/src/main/resources/config/application.yml +++ b/eladmin-system/src/main/resources/config/application.yml @@ -57,11 +57,17 @@ rsa: remote: link: address: 'http://47.110.11.213:8000/api/temp/file/download' - file-base-path-linux: /home/eladmin/file/temp/ + file-base-path-linux: ~/home/eladmin/file/temp/ file-base-path-windows: C:\eladmin\file\temp\ - file-base-path-mac: /Users/wujingtao/file/eladmin/temp/ + file-base-path-mac: ~/file/eladmin/temp/ # 发送加密家口需要的一些参数 inter: address: https://bd.hzdaba.cn:8085/v3/Accounts/yuyoukeji/BigData/EncryptTel appid: yuyoukeji - tk: e3fb7090b4babe44a5113601487af433 \ No newline at end of file + tk: e3fb7090b4babe44a5113601487af433 + +upload: + accessKeyId: LTAI5t8agDMumxcjAVuuwqPY + accessKeySecret: i93lzrZwb2BUgtB9r0y0LT2Dt7tNbP + bucketName: hjrecording + endpoint: oss-cn-shanghai.aliyuncs.com \ No newline at end of file From abce30cf27c29d1ad050c13efecb2c64be6b0994 Mon Sep 17 00:00:00 2001 From: wujingtao Date: Sat, 7 Aug 2021 17:36:35 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8A=A0=E5=85=A5yml=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/formdata/service/impl/FormdataServiceImpl.java | 2 -- eladmin-system/src/main/resources/config/application.yml | 5 +++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java index fc4bbff..e7e1e02 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java @@ -225,8 +225,6 @@ public class FormdataServiceImpl implements FormdataService { String fileName = file.getOriginalFilename(); String subFixFile = StringUtils.substringAfterLast(fileName, SPLIT_FILE_SYMBOL); - - List wavList = zipUtil(file); for (WavDTO wavDTO : wavList) { for (FormdataDto info : list) { diff --git a/eladmin-system/src/main/resources/config/application.yml b/eladmin-system/src/main/resources/config/application.yml index e4206c1..dc604c7 100644 --- a/eladmin-system/src/main/resources/config/application.yml +++ b/eladmin-system/src/main/resources/config/application.yml @@ -25,7 +25,7 @@ spring: #数据库索引 database: 0 host: 118.178.137.129 -# port: 6379 + # port: 6379 password: #连接超时时间 timeout: 5000 @@ -70,4 +70,5 @@ upload: accessKeyId: LTAI5t8agDMumxcjAVuuwqPY accessKeySecret: i93lzrZwb2BUgtB9r0y0LT2Dt7tNbP bucketName: hjrecording - endpoint: oss-cn-shanghai.aliyuncs.com \ No newline at end of file + endpoint: oss-cn-shanghai.aliyuncs.com + url: https://app.hjdata.com/api/dymaticform/channel/save? \ No newline at end of file From 3f7fb9b2d717d28b0c03d837e6207f7725585237 Mon Sep 17 00:00:00 2001 From: wujingtao Date: Sat, 7 Aug 2021 17:44:36 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/uploadnew/rest/TbUploadFileNewController.java | 4 ++-- eladmin-system/src/main/resources/config/application.yml | 2 +- eladmin-system/src/main/resources/logback.xml | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java index ad8f62d..ce8b0d4 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java @@ -144,9 +144,9 @@ public class TbUploadFileNewController { */ @Log("新的成单数据及录音文件导入接口") @ApiOperation("新的成单数据及录音文件导入接口") - @AnonymousAccess +// @AnonymousAccess @PostMapping("/newFileUpload") -// @PreAuthorize("@el.check('tbUploadFileNew:list')") // 这个换下相关按钮的权限控制 + @PreAuthorize("@el.check('tbUploadFileNew:list')") // 这个换下相关按钮的权限控制 // @ResponseBody public ResponseEntity newFileUpload(@RequestParam("file") MultipartFile[] files, @RequestParam(value = "taskName") String taskName) { diff --git a/eladmin-system/src/main/resources/config/application.yml b/eladmin-system/src/main/resources/config/application.yml index dc604c7..bf23194 100644 --- a/eladmin-system/src/main/resources/config/application.yml +++ b/eladmin-system/src/main/resources/config/application.yml @@ -5,7 +5,7 @@ spring: freemarker: check-template-location: false profiles: - active: dev + active: prod jackson: time-zone: GMT+8 data: diff --git a/eladmin-system/src/main/resources/logback.xml b/eladmin-system/src/main/resources/logback.xml index 40d230d..4fd9306 100644 --- a/eladmin-system/src/main/resources/logback.xml +++ b/eladmin-system/src/main/resources/logback.xml @@ -77,7 +77,7 @@ - - + + \ No newline at end of file