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 f89e029..365acd1 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 @@ -105,11 +105,4 @@ public interface FormdataService { * @return */ String parseMediaFileToLocal(MultipartFile file); - - /** - * 上传oos - * - * @param 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 c66071f..a4b210b 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 @@ -16,8 +16,6 @@ package me.zhengjie.modules.formdata.service.impl; import com.alibaba.excel.EasyExcelFactory; -import com.aliyun.oss.OSS; -import com.aliyun.oss.OSSClientBuilder; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import me.zhengjie.modules.formdata.domain.Formdata; @@ -28,17 +26,13 @@ import me.zhengjie.modules.formdata.service.dto.FormdataQueryCriteria; import me.zhengjie.modules.formdata.service.mapstruct.FormdataMapper; import me.zhengjie.modules.uploadnew.listener.EasyExcelListener; 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.service.impl.BuildPathUtils; import me.zhengjie.modules.uploadnew.task.TransFormDataTask; import me.zhengjie.utils.FileUtil; import me.zhengjie.utils.PageUtil; import me.zhengjie.utils.QueryHelp; import me.zhengjie.utils.ValidationUtil; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -47,17 +41,11 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; -import java.nio.charset.Charset; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import static java.util.stream.Collectors.toMap; -import static me.zhengjie.modules.constant.FileConstant.SPLIT_FILE_SYMBOL; /** * @author x @@ -72,20 +60,10 @@ public class FormdataServiceImpl implements FormdataService { private final FormdataRepository formdataRepository; private final FormdataMapper formdataMapper; - @Autowired - private final TransFormDataTask transFormDataTask; @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) { @@ -219,67 +197,4 @@ public class FormdataServiceImpl implements FormdataService { } return path; } - - /** - * 上传oos - * - * @param file - * @return - */ - @Override - public void uploadOOS(MultipartFile file, List list) { - List wavList = zipUtil(file); - Map map = list.stream().collect(toMap(FormdataDto::getPhone, p -> p)); - for (WavDTO wav : wavList) { - if (map.containsKey(wav.getName())) { - //上传路径 - String newFileName = buildPathUtils.buildFileOosPath(wav.getSubFixFile()); - OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); - ossClient.putObject(bucketName, newFileName, wav.getLine()); - ossClient.shutdown(); - OosDTO oosDTO = toBean(map.get(wav.getName()), newFileName); - //发送下游 - transFormDataTask.doRunTask(oosDTO); - } - } - } - - private List zipUtil(MultipartFile file) { - List wavDTOS = new ArrayList<>(); - - String zipFileName = null; - try { - ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK")); - BufferedInputStream bs = new BufferedInputStream(zipInputStream); - ZipEntry zipEntry; - byte[] bytes = null; - while ((zipEntry = zipInputStream.getNextEntry()) != null) { - WavDTO wavDTO = new WavDTO(); - zipFileName = zipEntry.getName(); - wavDTO.setName(zipFileName.substring(0, zipFileName.lastIndexOf("."))); - - wavDTO.setSubFixFile(StringUtils.substringAfterLast(zipFileName, SPLIT_FILE_SYMBOL)); - bytes = new byte[(int) zipEntry.getSize()]; - bs.read(bytes, 0, (int) zipEntry.getSize()); - InputStream byteArrayInputStream = new ByteArrayInputStream(bytes); - wavDTO.setLine(byteArrayInputStream); - wavDTOS.add(wavDTO); - } - } catch (Exception e) { - log.error("读取部署包文件内容失败,请确认部署包格式正确: {} ", zipFileName, e); - } - return wavDTOS; - } - - private OosDTO toBean(FormdataDto info, String newFileName) { - OosDTO oosDTO = new OosDTO(); - oosDTO.setId(info.getSourceId().longValue()); - oosDTO.setAge(info.getAge()); - oosDTO.setCity(info.getCity()); - oosDTO.setName(info.getName()); - oosDTO.setPhone(info.getPhone()); - oosDTO.setProvince(info.getProvince()); - oosDTO.setVoiceaddr(newFileName); - return oosDTO; - } } \ No newline at end of file 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 b374168..2fb6076 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 @@ -36,6 +36,7 @@ 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.task.SaveToFileNewTask; +import me.zhengjie.modules.uploadnew.task.UploadOssTask; import me.zhengjie.utils.SecurityUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; @@ -83,6 +84,8 @@ public class TbUploadFileNewController { private RedisTemplate redisTemplate; private FormdataService formdataService; + @Autowired + private UploadOssTask uploadOssTask; @Autowired public TbUploadFileNewController(TbUploadFileNewService tbUploadFileNewService, SaveToFileNewTask saveToFileNewTask, RedisTemplate redisTemplate, FormdataService formdataService) { @@ -152,13 +155,12 @@ public class TbUploadFileNewController { @RequestParam(value = "taskName") String taskName) { // 记录日志 log.info("TbUploadFileNewController:newFileUpload | taskName : {} start ", taskName); - // 校验上传是否有文件 if (null == files) { return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK); } - //记录文件上传记录 + TbUploadFileNew resources = new TbUploadFileNew(); resources.setUploadTime(DateUtil.date().toTimestamp()); resources.setUploadFileTaskName(taskName); @@ -166,13 +168,11 @@ public class TbUploadFileNewController { resources.setGmtCreate(DateUtil.date().toTimestamp()); resources.setOperation(SecurityUtils.getCurrentUser().getUsername()); TbUploadFileNewDto resourcesDto = tbUploadFileNewService.create(resources); - if (ObjectUtil.isEmpty(resourcesDto)) { log.error("上传记录创建失败, resources is {} ", resources.toString()); return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.FILE_HANDLE_FAIL), HttpStatus.OK); } - - // 解析文件 + //解析文件 int finishTag = 0; List list = new ArrayList<>(); @@ -181,7 +181,7 @@ public class TbUploadFileNewController { if (StringUtils.isNotBlank(originalFilename)) { String subOriginalFilename = StringUtils.substringAfterLast(originalFilename, SPLIT_FILE_SYMBOL); if (XLSX_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { - // 解析 入库 + //解析 入库 list = formdataService.parseFileAndSaveToDB(file); if (CollectionUtil.isNotEmpty(list)) { finishTag += 1; @@ -189,49 +189,38 @@ public class TbUploadFileNewController { } if (RAR_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename) || ZIP_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { - // 以 压缩文件结尾的格式 则直接进行保存文件到指定文件夹目录下 + //以 压缩文件结尾的格式 则直接进行保存文件到指定文件夹目录下 String localSavePath = formdataService.parseMediaFileToLocal(file); if (StringUtils.isNotBlank(localSavePath)) { - // 记录保存位置 + //记录保存位置 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. 上传oos 并 异步发送给 下游接口 - formdataService.uploadOOS(file, list); - } } } + //异步上传oss,并传给下游 + uploadOssTask.uploadOss(files, list); // 确保所有文件都经过了响应的处理 if (finishTag != files.length) { - // 上传完成更新文件上传记录 + //上传失败更新文件上传记录 resourcesDto.setUploadTag(DefaultConstant.TWO_NUMBER); resourcesDto.setGmtModified(DateUtil.date().toTimestamp()); TbUploadFileNew tbUploadFileNew = new TbUploadFileNew(); BeanUtils.copyProperties(resourcesDto, tbUploadFileNew); tbUploadFileNewService.update(tbUploadFileNew); - log.error("TbUploadFileNewController:newFileUpload | taskName : {} fail ", taskName); return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.FILE_HANDLE_FAIL), HttpStatus.OK); } - // 上传完成更新文件上传记录 resourcesDto.setUploadTag(DefaultConstant.ZERO_NUMBER); resourcesDto.setGmtModified(DateUtil.date().toTimestamp()); + resourcesDto.setFileTransSuccessCount((long) files.length); TbUploadFileNew tbUploadFileNew = new TbUploadFileNew(); BeanUtils.copyProperties(resourcesDto, tbUploadFileNew); tbUploadFileNewService.update(tbUploadFileNew); - return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/ExcelDataDTO.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/ExcelDataDTO.java index e49b8cd..b0c6411 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/ExcelDataDTO.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/ExcelDataDTO.java @@ -1,5 +1,6 @@ package me.zhengjie.modules.uploadnew.service.dto; +import com.alibaba.excel.annotation.ExcelProperty; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -12,12 +13,12 @@ import javax.persistence.Column; @Data public class ExcelDataDTO { @Column(name = "name") - @ApiModelProperty(value = "称呼") + @ApiModelProperty(value = "姓名") private String name; @Column(name = "phone") - @ApiModelProperty(value = "联系方式") - private String phone; + @ApiModelProperty(value = "手机号") + private String phone; @Column(name = "age") @ApiModelProperty(value = "年龄") @@ -28,6 +29,6 @@ public class ExcelDataDTO { private String province; @Column(name = "city") - @ApiModelProperty(value = "城市") + @ApiModelProperty(value = "市") private String city; } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewQueryCriteria.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewQueryCriteria.java index 4692219..070999d 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewQueryCriteria.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewQueryCriteria.java @@ -32,6 +32,8 @@ public class TbUploadFileNewQueryCriteria{ @Query(type = Query.Type.INNER_LIKE) private String uploadFileTaskName; + + /** 模糊 */ @Query(type = Query.Type.INNER_LIKE) private String operation; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java index 2cf6a64..834fadb 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.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.impl; import cn.hutool.core.collection.CollectionUtil; @@ -50,11 +50,11 @@ import static me.zhengjie.modules.uploadnew.consts.SysConstNew.TEST_USER_NAME; import static me.zhengjie.modules.uploadnew.consts.UploadFileNewConst.*; /** -* @website https://el-admin.vip -* @description 服务实现 -* @author weizhongxi -* @date 2021-04-14 -**/ + * @author weizhongxi + * @website https://el-admin.vip + * @description 服务实现 + * @date 2021-04-14 + **/ @Service @RequiredArgsConstructor @Slf4j @@ -82,21 +82,21 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { private final TbUploadFileNewMapper tbUploadFileNewMapper; @Override - public Map queryAll(TbUploadFileNewQueryCriteria criteria, Pageable pageable){ - Page page = tbUploadFileNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + public Map queryAll(TbUploadFileNewQueryCriteria criteria, Pageable pageable) { + Page page = tbUploadFileNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable); return PageUtil.toPage(page.map(tbUploadFileNewMapper::toDto)); } @Override - public List queryAll(TbUploadFileNewQueryCriteria criteria){ - return tbUploadFileNewMapper.toDto(tbUploadFileNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + public List queryAll(TbUploadFileNewQueryCriteria criteria) { + return tbUploadFileNewMapper.toDto(tbUploadFileNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder))); } @Override @Transactional public TbUploadFileNewDto findById(Integer id) { TbUploadFileNew tbUploadFileNew = tbUploadFileNewRepository.findById(id).orElseGet(TbUploadFileNew::new); - ValidationUtil.isNull(tbUploadFileNew.getId(),"TbUploadFileNew","id",id); + ValidationUtil.isNull(tbUploadFileNew.getId(), "TbUploadFileNew", "id", id); return tbUploadFileNewMapper.toDto(tbUploadFileNew); } @@ -110,7 +110,7 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { @Transactional(rollbackFor = Exception.class) public void update(TbUploadFileNew resources) { TbUploadFileNew tbUploadFileNew = tbUploadFileNewRepository.findById(resources.getId()).orElseGet(TbUploadFileNew::new); - ValidationUtil.isNull( tbUploadFileNew.getId(),"TbUploadFileNew","id",resources.getId()); + ValidationUtil.isNull(tbUploadFileNew.getId(), "TbUploadFileNew", "id", resources.getId()); tbUploadFileNew.copy(resources); tbUploadFileNewRepository.save(tbUploadFileNew); } @@ -126,7 +126,7 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (TbUploadFileNewDto tbUploadFileNew : all) { - Map map = new LinkedHashMap<>(); + Map map = new LinkedHashMap<>(); map.put("创建时间", tbUploadFileNew.getGmtCreate()); map.put("修改时间", tbUploadFileNew.getGmtModified()); map.put("上传任务名称", tbUploadFileNew.getUploadFileTaskName()); @@ -163,10 +163,10 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { file.transferTo(Paths.get(eachFilePath)); baseStr = ""; } catch (IOException e) { - log.error("============== [transferTo file fail, path is {} ] ==============", eachFilePath,e); + log.error("============== [transferTo file fail, path is {} ] ==============", eachFilePath, e); } - String name=file.getOriginalFilename(); + String name = file.getOriginalFilename(); int lastIndexOf = name.lastIndexOf("."); String nameStr = name.substring(lastIndexOf); //记录文件格式 @@ -175,24 +175,24 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { //根据文件类型进行解析 try { // FIXME: 2021/4/23 0023 - if(nameStr.equals(".xlsx")||nameStr.equals(".xls")){ + if (nameStr.equals(".xlsx") || nameStr.equals(".xls")) { fileFormat = "excel文件"; ExcelUtils excelUtils = new ExcelUtils(); list = excelUtils.excelParseList(file.getInputStream()); - }else if (nameStr.equals(".txt")){ + } else if (nameStr.equals(".txt")) { fileFormat = "txt文件"; list = FileUtil.readLines(eachFilePath, "UTF-8"); - }else if (nameStr.equals(".csv")){ + } else if (nameStr.equals(".csv")) { fileFormat = "csv文件"; list = TxtUtils.csvParseList(file.getInputStream()); } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } // 统计行数 - if (CollectionUtil.isNotEmpty(list)){ + if (CollectionUtil.isNotEmpty(list)) { count += list.size(); } // 保存所有的临时存放路径 @@ -205,9 +205,9 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { tbUploadFileNew.setUploadFileTaskName(taskName); tbUploadFileNew.setUploadTime(new Timestamp(new Date().getTime())); String currentUsername; - if (SysConst.sysDebug){ + if (SysConst.sysDebug) { tbUploadFileNew.setOperation(TEST_USER_NAME);//fixme 这边测试环境补充一下需要的操作人 - }else { + } else { currentUsername = SecurityUtils.getCurrentUsername(); tbUploadFileNew.setOperation(currentUsername); } @@ -221,7 +221,7 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { tbUploadFileNew.setGmtCreate(nousedate); tbUploadFileNew.setGmtModified(nousedate); String tempFilesPath = pathBuilder.toString().substring(0, pathBuilder.length() - 1); - if (StringUtils.isNotBlank(tempFilesPath)){ + if (StringUtils.isNotBlank(tempFilesPath)) { tbUploadFileNew.setLocalSavePath(tempFilesPath); // 去掉最后的那个通配符 FILE_PATH_SPLIT } TbUploadFileNewDto tbUploadFileNewDto = create(tbUploadFileNew); @@ -240,20 +240,20 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { String dirPath; // 生成一个随机文件夹目录,方便整理和打包 - String filePath = RandomUtil.randomString(6) + FILE_NAME_SPLIT + baseStr + FILE_SPLIT + extName; + String filePath = RandomUtil.randomString(6) + FILE_NAME_SPLIT + baseStr + FILE_SPLIT + extName; if (osInfo.isWindows()) { - dirPath = remoteLinkFileBasePathWindows + timeFormate + File.separator; + dirPath = remoteLinkFileBasePathWindows + timeFormate + File.separator; FileUtil.mkdir(new File(dirPath)); // 构建存储文件 return dirPath + filePath; } else if (osInfo.isLinux()) { - dirPath = remoteLinkFileBasePathLinux + timeFormate + File.separator; + dirPath = remoteLinkFileBasePathLinux + timeFormate + File.separator; FileUtil.mkdir(new File(dirPath)); // 构建存储文件 return dirPath + filePath; } else if (osInfo.isMac()) { - dirPath = remoteLinkFileBasePathMac + timeFormate + File.separator; + dirPath = remoteLinkFileBasePathMac + timeFormate + File.separator; FileUtil.mkdir(new File(dirPath)); // 构建存储文件 return dirPath + filePath; 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 8d00675..f70846d 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 @@ -18,7 +18,6 @@ import java.time.LocalDateTime; import java.time.ZoneOffset; - @Component @Scope("prototype") @Slf4j diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/UploadOssTask.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/UploadOssTask.java new file mode 100644 index 0000000..166fb06 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/UploadOssTask.java @@ -0,0 +1,97 @@ +package me.zhengjie.modules.uploadnew.task; + +import cn.hutool.core.collection.CollectionUtil; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.modules.formdata.service.dto.FormdataDto; +import me.zhengjie.modules.formdata.service.impl.FormdataServiceImpl; +import me.zhengjie.modules.uploadnew.service.dto.OosDTO; +import me.zhengjie.modules.uploadnew.service.dto.WavDTO; +import me.zhengjie.modules.uploadnew.service.impl.BuildPathUtils; +import me.zhengjie.modules.uploadnew.util.ZipUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +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 org.springframework.web.multipart.MultipartFile; + +import java.util.List; +import java.util.Map; + +import static java.util.stream.Collectors.toMap; +import static me.zhengjie.modules.constant.FileConstant.*; + +/** + * @author wjt + * @date 2021/8/10 + * 上传oss + */ +@Slf4j +@Scope("prototype") +@Component +public class UploadOssTask { + @Value("${upload.accessKeyId}") + private String accessKeyId; + @Value("${upload.accessKeySecret}") + private String accessKeySecret; + @Value("${upload.bucketName}") + private String bucketName; + @Value("${upload.endpoint}") + private String endpoint; + @Autowired + FormdataServiceImpl formdataService; + + + @Autowired + private ZipUtils zipUtils; + @Autowired + private BuildPathUtils buildPathUtils; + @Autowired + private TransFormDataTask transFormDataTask; + + @Async(value = "SendBigDataTaskExecutor") + public void uploadOss(MultipartFile[] files, List 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)) { + // 上传oos 并 异步发送给 下游接口 + upload(file, list); + } + } + } + } + + private void upload(MultipartFile file, List list) { + List wavList = zipUtils.zipUtil(file); + Map map = list.stream().collect(toMap(FormdataDto::getPhone, p -> p, (key1, key2) -> key2)); + for (WavDTO wav : wavList) { + if (map.containsKey(wav.getName())) { + //上传路径 + String newFileName = buildPathUtils.buildFileOosPath(wav.getSubFixFile()); + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); + ossClient.putObject(bucketName, newFileName, wav.getLine()); + ossClient.shutdown(); + log.info("上传oos:{}", wav.getLine()); + OosDTO oosDTO = toBean(map.get(wav.getName()), newFileName); + //发送下游 + transFormDataTask.doRunTask(oosDTO); + } + } + } + + private OosDTO toBean(FormdataDto info, String newFileName) { + OosDTO oosDTO = new OosDTO(); + oosDTO.setId(info.getSourceId().longValue()); + oosDTO.setAge(info.getAge()); + oosDTO.setCity(info.getCity()); + oosDTO.setName(info.getName()); + oosDTO.setPhone(info.getPhone()); + oosDTO.setProvince(info.getProvince()); + oosDTO.setVoiceaddr(newFileName); + return oosDTO; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/ZipUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/ZipUtils.java new file mode 100644 index 0000000..38ff0ca --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/ZipUtils.java @@ -0,0 +1,56 @@ +package me.zhengjie.modules.uploadnew.util; + +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.modules.uploadnew.service.dto.WavDTO; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import static me.zhengjie.modules.constant.FileConstant.SPLIT_FILE_SYMBOL; + +/** + * @author wjt + * @date 2021/8/10 + */ +@Component +@Slf4j +public class ZipUtils { + + public List zipUtil(MultipartFile file) { + List wavs = new ArrayList<>(); + String zipFileName = null; + try { + ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK")); + BufferedInputStream bs = new BufferedInputStream(zipInputStream); + ZipEntry zipEntry; + byte[] bytes = null; + while ((zipEntry = zipInputStream.getNextEntry()) != null) { + if (!zipEntry.isDirectory()) { + if (zipEntry.getSize() > 0) { + WavDTO wavDTO = new WavDTO(); + zipFileName = zipEntry.getName(); + wavDTO.setName(zipFileName.substring(zipFileName.lastIndexOf("/") + 1, zipFileName.lastIndexOf(SPLIT_FILE_SYMBOL))); + wavDTO.setSubFixFile(StringUtils.substringAfterLast(zipFileName, SPLIT_FILE_SYMBOL)); + bytes = new byte[(int) zipEntry.getSize()]; + bs.read(bytes, 0, (int) zipEntry.getSize()); + InputStream byteArrayInputStream = new ByteArrayInputStream(bytes); + wavDTO.setLine(byteArrayInputStream); + wavs.add(wavDTO); + } + } + } + } catch (Exception e) { + log.error("读取部署包文件内容失败,请确认部署包格式正确: {} ", zipFileName, e); + } + return wavs; + } +}