From 5661e7b1dd63250829f6c3c37cfe094200ed2eac Mon Sep 17 00:00:00 2001 From: wujingtao Date: Fri, 6 Aug 2021 19:46:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eladmin-system/pom.xml | 40 ++- .../formdata/service/FormdataService.java | 102 +++--- .../service/impl/FormdataServiceImpl.java | 304 +++++++++++++++--- .../security/config/SecurityConfig.java | 5 +- .../uploadnew/httpRequest/HttpClient.java | 55 ++++ .../uploadnew/httpRequest/SSLClient.java | 42 +++ .../uploadnew/listener/EasyExcelListener.java | 51 +++ .../rest/TbUploadFileNewController.java | 49 ++- .../uploadnew/service/dto/ExcelDataDTO.java | 33 ++ .../modules/uploadnew/service/dto/OosDTO.java | 18 ++ .../modules/uploadnew/service/dto/WavDTO.java | 16 + .../uploadnew/task/TransFormDataTask.java | 12 +- .../main/resources/config/application-dev.yml | 4 +- .../src/main/resources/config/application.yml | 8 +- 14 files changed, 615 insertions(+), 124 deletions(-) create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/HttpClient.java create 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/listener/EasyExcelListener.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/ExcelDataDTO.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/OosDTO.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/WavDTO.java diff --git a/eladmin-system/pom.xml b/eladmin-system/pom.xml index efaf132..9ad3b09 100644 --- a/eladmin-system/pom.xml +++ b/eladmin-system/pom.xml @@ -40,10 +40,10 @@ - - org.springframework.boot - spring-boot-starter-websocket - + + org.springframework.boot + spring-boot-starter-websocket + com.aliyun.oss @@ -87,16 +87,16 @@ - - ch.ethz.ganymed - ganymed-ssh2 - build210 - - - com.jcraft - jsch - 0.1.55 - + + ch.ethz.ganymed + ganymed-ssh2 + build210 + + + com.jcraft + jsch + 0.1.55 + @@ -106,12 +106,18 @@ - - - + + + + + com.aliyun.oss + aliyun-sdk-oss + 3.10.0 + + 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 fcdd78d..03039df 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 @@ -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.formdata.service; import me.zhengjie.modules.formdata.domain.Formdata; @@ -27,60 +27,67 @@ import java.util.List; import java.util.Map; /** -* @website https://el-admin.vip -* @description 服务接口 -* @author x -* @date 2021-08-05 -**/ + * @author x + * @website https://el-admin.vip + * @description 服务接口 + * @date 2021-08-05 + **/ public interface FormdataService { /** - * 查询数据分页 - * @param criteria 条件 - * @param pageable 分页参数 - * @return Map - */ - Map queryAll(FormdataQueryCriteria criteria, Pageable pageable); + * 查询数据分页 + * + * @param criteria 条件 + * @param pageable 分页参数 + * @return Map + */ + Map queryAll(FormdataQueryCriteria criteria, Pageable pageable); /** - * 查询所有数据不分页 - * @param criteria 条件参数 - * @return List - */ + * 查询所有数据不分页 + * + * @param criteria 条件参数 + * @return List + */ List queryAll(FormdataQueryCriteria criteria); /** * 根据ID查询 + * * @param id ID * @return FormdataDto */ FormdataDto findById(Long id); /** - * 创建 - * @param resources / - * @return FormdataDto - */ + * 创建 + * + * @param resources / + * @return FormdataDto + */ FormdataDto create(Formdata resources); /** - * 编辑 - * @param resources / - */ + * 编辑 + * + * @param resources / + */ void update(Formdata resources); /** - * 多选删除 - * @param ids / - */ + * 多选删除 + * + * @param ids / + */ void deleteAll(Long[] ids); /** - * 导出数据 - * @param all 待导出的数据 - * @param response / - * @throws IOException / - */ + * 导出数据 + * + * @param all 待导出的数据 + * @param response / + * @throws IOException / + */ void download(List all, HttpServletResponse response) throws IOException; /** @@ -98,4 +105,11 @@ public interface FormdataService { * @return */ String parseMediaFileToLocal(MultipartFile file); + + /** + * 上传oos + * + * @param file + */ + void uploadOOS(MultipartFile file); } \ 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 295aaff..b2c8d36 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 @@ -1,33 +1,54 @@ /* -* 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.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 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; import me.zhengjie.modules.formdata.service.dto.FormdataDto; 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.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; @@ -35,44 +56,67 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +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; + +import static me.zhengjie.modules.constant.FileConstant.SPLIT_FILE_SYMBOL; /** -* @website https://el-admin.vip -* @description 服务实现 -* @author x -* @date 2021-08-05 -**/ + * @author x + * @website https://el-admin.vip + * @description 服务实现 + * @date 2021-08-05 + **/ +@Slf4j @Service @RequiredArgsConstructor public class FormdataServiceImpl implements FormdataService { private final FormdataRepository formdataRepository; private final FormdataMapper formdataMapper; - @Autowired - private TransFormDataTask transFormDataTask; + 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<>(); + @Override - public Map queryAll(FormdataQueryCriteria criteria, Pageable pageable){ - Page page = formdataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + public Map queryAll(FormdataQueryCriteria criteria, Pageable pageable) { + Page page = formdataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable); return PageUtil.toPage(page.map(formdataMapper::toDto)); } @Override - public List queryAll(FormdataQueryCriteria criteria){ - return formdataMapper.toDto(formdataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + public List queryAll(FormdataQueryCriteria criteria) { + return formdataMapper.toDto(formdataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder))); } @Override @Transactional public FormdataDto findById(Long id) { Formdata formdata = formdataRepository.findById(id).orElseGet(Formdata::new); - ValidationUtil.isNull(formdata.getId(),"Formdata","id",id); + ValidationUtil.isNull(formdata.getId(), "Formdata", "id", id); return formdataMapper.toDto(formdata); } @@ -86,7 +130,7 @@ public class FormdataServiceImpl implements FormdataService { @Transactional(rollbackFor = Exception.class) public void update(Formdata resources) { Formdata formdata = formdataRepository.findById(resources.getId()).orElseGet(Formdata::new); - ValidationUtil.isNull( formdata.getId(),"Formdata","id",resources.getId()); + ValidationUtil.isNull(formdata.getId(), "Formdata", "id", resources.getId()); formdata.copy(resources); formdataRepository.save(formdata); } @@ -102,7 +146,7 @@ public class FormdataServiceImpl implements FormdataService { public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (FormdataDto formdata : all) { - Map map = new LinkedHashMap<>(); + Map map = new LinkedHashMap<>(); map.put("上传时间", formdata.getUploadTime()); map.put("渠道方id", formdata.getSourceId()); map.put("称呼", formdata.getName()); @@ -123,28 +167,202 @@ public class FormdataServiceImpl implements FormdataService { */ @Override public boolean parseFileAndSaveToDB(MultipartFile file) { - List formdataDtos = new ArrayList<>(); + List formData; // 1. todo 先进行解析 excel 文件 + try { + InputStream inputStream = file.getInputStream(); + EasyExcelListener listener = new EasyExcelListener(); + EasyExcelFactory.read(inputStream, ExcelDataDTO.class, listener).sheet().doRead(); + //获取到解析后的数据 + formData = listener.getData(); + // 2. todo 把解析后的结果进行入库 + formdataRepository.saveAll(formData); + list = formdataMapper.toDto(formData); +// - - // 2. todo 把解析后的结果进行入库 - - - // 3. todo 异步发送给 下游接口 - transFormDataTask.doRunTask(formdataDtos); - - return false; + } catch (Exception e) { + log.error("解析失败"); + e.printStackTrace(); + return false; + } + return true; } /** * 相关逻辑 + * * @param file 需要进行处理的文件对象 * @return 是否操作成功 */ @Override public String parseMediaFileToLocal(MultipartFile file) { // todo 存储音频压缩文件到指定目录下 每次上传以 2021-8-5 为 文件名进行记录 每次记录一天的 并保存记录 + //文件名 + String fileName = file.getOriginalFilename(); + //文件保存地址 + String path = buildFilePath(fileName); + + OutputStream os = null; + InputStream inputStream = null; + try { + //获取流 + inputStream = file.getInputStream(); + byte[] bs = new byte[1024]; + // 读取到的数据长度 + int len; + // 输出的文件流保存到本地文件 + + File tempFile = new File(path); + if (!tempFile.exists()) { + tempFile.mkdirs(); + } + os = new FileOutputStream(tempFile.getPath() + File.separator + fileName); + // 开始读取 + while ((len = inputStream.read(bs)) != -1) { + os.write(bs, 0, len); + } + } catch (Exception e) { + log.error("上传失败"); + return ""; + } finally { + //关闭连接 + try { + assert os != null; + os.close(); + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return path; + } + + /** + * 上传oos + * + * @param file + * @return + */ + @Override + public void uploadOOS(MultipartFile file) { + String accessKeyId = "LTAI5t8agDMumxcjAVuuwqPY"; + String accessKeySecret = "i93lzrZwb2BUgtB9r0y0LT2Dt7tNbP"; + String bucketName = "hjrecording"; + String endpoint = "oss-cn-shanghai.aliyuncs.com"; + + 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()); + ossClient.shutdown(); + OosDTO oosDTO = toBean(info, 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("."))); + 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()); + 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; + } + + /** + * 构建文件上传保存路径 + */ + private String buildFilePath(String fileName) { + // 获取环境配置信息 + OsInfo osInfo = SystemUtil.getOsInfo(); + // 定义的时间格式 + String timeFormate = DateUtil.today() ; - return ""; + 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 a25040f..eb16fb4 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,7 +135,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { // 所有类型的接口都放行 .antMatchers(anonymousUrls.get(RequestMethodEnum.ALL.getType()).toArray(new String[0])).permitAll() // 所有请求都需要认证 - .anyRequest().authenticated() +// .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 new file mode 100644 index 0000000..314f44f --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/HttpClient.java @@ -0,0 +1,55 @@ +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 new file mode 100644 index 0000000..e006bd1 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/httpRequest/SSLClient.java @@ -0,0 +1,42 @@ +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 new file mode 100644 index 0000000..4bc9a69 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/listener/EasyExcelListener.java @@ -0,0 +1,51 @@ +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; +import com.alibaba.excel.event.AnalysisEventListener; +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.modules.formdata.domain.Formdata; +import me.zhengjie.modules.uploadnew.service.dto.ExcelDataDTO; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author wjt + * @date 2021/8/6 + */ +@Slf4j +public class EasyExcelListener extends AnalysisEventListener { + + + /** + * 用于存储解析的对象数据 + */ + private List FormDataList = new ArrayList<>(); + + @Override + public void invoke(ExcelDataDTO excelDataDTO, AnalysisContext analysisContext) { + log.info("解析到一条数据:{}", JSONUtil.toJsonStr(excelDataDTO)); + Formdata formdata = new Formdata(); + formdata.setSourceId((int) IdUtil.getSnowflake(1, 1).nextId()); + formdata.setUploadTime(DateUtil.date().toTimestamp()); + formdata.setName(excelDataDTO.getName()); + formdata.setAge(excelDataDTO.getAge()); + formdata.setCity(excelDataDTO.getCity()); + formdata.setPhone(String.valueOf(excelDataDTO.getPhone())); + formdata.setProvince(excelDataDTO.getProvince()); + FormDataList.add(formdata); + } + + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + log.info("解析完成"); + } + + public List getData() { + return FormDataList; + } +} 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 ec18c26..cbfb5e5 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 @@ -16,6 +16,8 @@ package me.zhengjie.modules.uploadnew.rest; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjectUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; @@ -25,13 +27,19 @@ import me.zhengjie.annotation.Log; import me.zhengjie.common.http.CommonResponse; import me.zhengjie.common.http.ResponseCode; 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.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; @@ -48,6 +56,7 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -76,6 +85,9 @@ public class TbUploadFileNewController { private RedisTemplate redisTemplate; private FormdataService formdataService; + @Autowired + private TbUploadFileNewMapper tbUploadFileNewMapper; + @Autowired public TbUploadFileNewController(TbUploadFileNewService tbUploadFileNewService, SaveToFileNewTask saveToFileNewTask, RedisTemplate redisTemplate, FormdataService formdataService) { @@ -150,9 +162,20 @@ public class TbUploadFileNewController { if (null == files) { return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK); } - // todo 记录文件上传记录 - + //记录文件上传记录 + + TbUploadFileNew resources = new TbUploadFileNew(); + resources.setUploadTime(DateUtil.date().toTimestamp()); + resources.setUploadFileTaskName(taskName); + resources.setFileCount((long) files.length); + resources.setGmtCreate(DateUtil.date().toTimestamp()); + //操作人 获取当前登录人 + resources.setOperation(SecurityUtils.getCurrentUser().getUsername()); + TbUploadFileNewDto resourcesDto = tbUploadFileNewService.create(resources); + if (ObjectUtil.isEmpty(resourcesDto)) { + log.error("保存失败"); + } int finishTag = 0; for (MultipartFile file : files) { @@ -160,7 +183,7 @@ public class TbUploadFileNewController { if (StringUtils.isNotBlank(originalFilename)) { String subOriginalFilename = StringUtils.substringAfterLast(originalFilename, SPLIT_FILE_SYMBOL); - finishTag = handleEachUploadFile(file , subOriginalFilename, finishTag); + finishTag = handleEachUploadFile(file, subOriginalFilename, finishTag, resourcesDto); } } @@ -172,7 +195,9 @@ 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); } @@ -183,23 +208,29 @@ public class TbUploadFileNewController { * @param subOriginalFilename 文件后缀名 * @return 处理后的步骤计数 */ - private int handleEachUploadFile(MultipartFile file, String subOriginalFilename, int finishTag) { + private int handleEachUploadFile(MultipartFile file, String subOriginalFilename, int finishTag, TbUploadFileNewDto resourcesDto) { + if (XLSX_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { - // TODO: 2021/8/5 0005 解析 入库 异步发送到下游 - if (formdataService.parseFileAndSaveToDB(file)){ + // 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 记录保存位置 + 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/ExcelDataDTO.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/ExcelDataDTO.java new file mode 100644 index 0000000..ae8cbbf --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/ExcelDataDTO.java @@ -0,0 +1,33 @@ +package me.zhengjie.modules.uploadnew.service.dto; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.persistence.Column; + +/** + * @author wjt + * @date 2021/8/6 + */ +@Data +public class ExcelDataDTO { + @Column(name = "name") + @ApiModelProperty(value = "称呼") + private String name; + + @Column(name = "phone") + @ApiModelProperty(value = "联系方式") + private Integer phone; + + @Column(name = "age") + @ApiModelProperty(value = "年龄") + private Integer age; + + @Column(name = "province") + @ApiModelProperty(value = "省份") + private String province; + + @Column(name = "city") + @ApiModelProperty(value = "城市") + private String city; +} 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 new file mode 100644 index 0000000..b6e0790 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/OosDTO.java @@ -0,0 +1,18 @@ +package me.zhengjie.modules.uploadnew.service.dto; + +import lombok.Data; + +/** + * @author wjt + * @date 2021/8/6 + */ +@Data +public class OosDTO { + private Integer id; + private String name; + private Integer age; + private String province; + private String city; + private String voiceaddr; + private String phone; +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/WavDTO.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/WavDTO.java new file mode 100644 index 0000000..4f17d14 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/WavDTO.java @@ -0,0 +1,16 @@ +package me.zhengjie.modules.uploadnew.service.dto; + +import lombok.Data; + +import java.io.InputStream; + +/** + * @author wjt + * @date 2021/8/6 + * 音频文件对象 + */ +@Data +public class WavDTO { + private String name; + private InputStream line; +} 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 ce4f9da..3c2efc4 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,7 +1,10 @@ package me.zhengjie.modules.uploadnew.task; +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.modules.uploadnew.service.dto.OosDTO; import me.zhengjie.utils.ConvertUtil; import org.springframework.context.annotation.Scope; import org.springframework.scheduling.annotation.Async; @@ -18,15 +21,16 @@ public class TransFormDataTask { @Async(value = "SendBigDataTaskExecutor") - public void doRunTask(List formdataDtos) { + public void doRunTask(OosDTO oosDTO) { Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); log.info("====== [ task start running, task name is {} ] ======", "TransFormDataTask"); - runTask(formdataDtos); + runTask(oosDTO); Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "TransFormDataTask", ConvertUtil.secondToTime(endMilliSecond - satrtMilliSecond)); } - private void runTask(List formdataDtos) { - + private void runTask(OosDTO oosDTO) { +// String jsonStr = JSONUtil.toJsonStr(oosDTO); +// HttpClient.doPost(jsonStr, "utf-8"); } } diff --git a/eladmin-system/src/main/resources/config/application-dev.yml b/eladmin-system/src/main/resources/config/application-dev.yml index cd10212..5b837e2 100644 --- a/eladmin-system/src/main/resources/config/application-dev.yml +++ b/eladmin-system/src/main/resources/config/application-dev.yml @@ -6,7 +6,7 @@ spring: driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy url: jdbc:log4jdbc:mysql://localhost:3306/eladmin-plat2?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false username: root - password: root + password: 12345678 # 初始连接数 initial-size: 5 # 最小连接数 @@ -100,7 +100,7 @@ ip: # 文件存储路径 file: mac: - path: ~/file/ + path: usr/file/ avatar: ~/avatar/ linux: path: /home/eladmin/file/ diff --git a/eladmin-system/src/main/resources/config/application.yml b/eladmin-system/src/main/resources/config/application.yml index 6a3d01f..91494c9 100644 --- a/eladmin-system/src/main/resources/config/application.yml +++ b/eladmin-system/src/main/resources/config/application.yml @@ -24,12 +24,12 @@ spring: redis: #数据库索引 database: 0 - host: 127.0.0.1 - port: 6379 + host: 118.178.137.129 +# port: 6379 password: #连接超时时间 timeout: 5000 - + open: false task: pool: # 核心线程池大小 @@ -59,7 +59,7 @@ remote: address: 'http://47.110.11.213:8000/api/temp/file/download' file-base-path-linux: /home/eladmin/file/temp/ file-base-path-windows: C:\eladmin\file\temp\ - file-base-path-mac: ~/file/eladmin/temp/ + file-base-path-mac: /Users/wujingtao/file/eladmin/temp/ # 发送加密家口需要的一些参数 inter: address: https://bd.hzdaba.cn:8085/v3/Accounts/yuyoukeji/BigData/EncryptTel