韦忠喜提交
parent
d4613c136f
commit
8823eca478
@ -0,0 +1,55 @@
|
|||||||
|
package me.zhengjie.modules.uploadnew.consts;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统相关的配置
|
||||||
|
*/
|
||||||
|
public class SysConstNew {
|
||||||
|
|
||||||
|
// Test相关的 - start
|
||||||
|
|
||||||
|
// 远程服务器的相关配置
|
||||||
|
// public static final String LOCAL_HOST = "localhost";
|
||||||
|
//
|
||||||
|
// public static final String LOCAL_DIR = "C:\\Users\\Administrator\\Desktop\\trans\\";
|
||||||
|
|
||||||
|
// Test相关的 - end
|
||||||
|
|
||||||
|
public static final String REMOTE_TRANS_HOST = "116.62.197.152"; // 118.178.137.129 116.62.197.152
|
||||||
|
|
||||||
|
public static final Integer REMOTE_TRANS_PORT = 22;
|
||||||
|
|
||||||
|
public static final String REMOTE_TRANS_SSH_USER = "root";
|
||||||
|
|
||||||
|
public static final String REMOTE_TRANS_SSH_PW = "yuyou@ECS2020";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 远程SFTP保存文件的路径
|
||||||
|
*/
|
||||||
|
public static final String REMOTE_TRANS_DIR_PATH = "/home/";
|
||||||
|
|
||||||
|
// fixme 以后改成rpc调用的地址
|
||||||
|
public static final String REMOTE_UPDATE_ADDR = "http://116.62.197.152:8000/api/remoteRecord/remote/rec";
|
||||||
|
|
||||||
|
// 测试内容临时定义
|
||||||
|
public static final String TEST_USER_NAME = "测试用户";
|
||||||
|
|
||||||
|
// 系统开发,上线环境下进行关闭 - DEBUG开关 - 默认是打开的
|
||||||
|
public static Boolean sysDebug = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 临时文件后缀
|
||||||
|
*/
|
||||||
|
public static String TEMP_FILE_END_STR = "bak";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用Set方式注入
|
||||||
|
*
|
||||||
|
* @param debug debug开关
|
||||||
|
*/
|
||||||
|
@Value(value = "${sys.debug}")
|
||||||
|
public static void setUrl(Boolean debug) {
|
||||||
|
SysConstNew.sysDebug = debug;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package me.zhengjie.modules.uploadnew.consts;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 常量定义
|
||||||
|
*/
|
||||||
|
public class UploadFileNewConst {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 白名单 - 对特殊账号做查询限制
|
||||||
|
*/
|
||||||
|
public static final List<String> WHITE_LIST = Arrays.asList("admin", "xiaoxing", "yangbin");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ext 文件类型分隔符 - 通配
|
||||||
|
*/
|
||||||
|
public static final String FILE_SPLIT = ".";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件路径的分隔
|
||||||
|
*/
|
||||||
|
public static final String FILE_PATH_SPLIT = ",";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 正在执行的标识
|
||||||
|
*/
|
||||||
|
public static final Integer DOING_TAG = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 成功执行的标识
|
||||||
|
*/
|
||||||
|
public static final Integer SUCCESS_TAG = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 失败执行的标识
|
||||||
|
*/
|
||||||
|
public static final Integer FAIL_TAG = 2;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件名的分隔符 _
|
||||||
|
*/
|
||||||
|
public static final String FILE_NAME_SPLIT = "_";
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* 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.repository;
|
||||||
|
|
||||||
|
import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @website https://el-admin.vip
|
||||||
|
* @author weizhongxi
|
||||||
|
* @date 2021-04-14
|
||||||
|
**/
|
||||||
|
public interface TbUploadFileNewRepository extends JpaRepository<TbUploadFileNew, Integer>, JpaSpecificationExecutor<TbUploadFileNew> {
|
||||||
|
}
|
@ -0,0 +1,217 @@
|
|||||||
|
/*
|
||||||
|
* 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.rest;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import io.swagger.models.auth.In;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import me.zhengjie.annotation.AnonymousAccess;
|
||||||
|
import me.zhengjie.annotation.Log;
|
||||||
|
import me.zhengjie.common.http.CommonResponse;
|
||||||
|
import me.zhengjie.common.http.ResponseCode;
|
||||||
|
import me.zhengjie.modules.security.security.TokenFilter;
|
||||||
|
import me.zhengjie.modules.upload.service.dto.UploadFileDto;
|
||||||
|
import me.zhengjie.modules.upload.service.dto.UploadFileQueryCriteria;
|
||||||
|
import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew;
|
||||||
|
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 org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
//import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import io.swagger.annotations.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.http.Cookie;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import static me.zhengjie.modules.upload.consts.UploadFileConst.FILE_PATH_SPLIT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @website https://el-admin.vip
|
||||||
|
* @author weizhongxi
|
||||||
|
* @date 2021-04-14
|
||||||
|
**/
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Api(tags = "文件上传接口管理")
|
||||||
|
@RequestMapping("/api/tbUploadFileNew")
|
||||||
|
@Slf4j
|
||||||
|
public class TbUploadFileNewController {
|
||||||
|
|
||||||
|
private final TbUploadFileNewService tbUploadFileNewService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SaveToFileNewTask saveToFileNewTask;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
|
@Log("导出数据")
|
||||||
|
@ApiOperation("导出数据")
|
||||||
|
@GetMapping(value = "/download")
|
||||||
|
@PreAuthorize("@el.check('tbUploadFileNew:list')")
|
||||||
|
public void download(HttpServletResponse response, TbUploadFileNewQueryCriteria criteria) throws IOException {
|
||||||
|
tbUploadFileNewService.download(tbUploadFileNewService.queryAll(criteria), response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
@Log("查询文件上传接口")
|
||||||
|
@ApiOperation("查询文件上传接口")
|
||||||
|
@PreAuthorize("@el.check('tbUploadFileNew:list')")
|
||||||
|
public ResponseEntity<Object> query(TbUploadFileNewQueryCriteria criteria, Pageable pageable){
|
||||||
|
return new ResponseEntity<>(tbUploadFileNewService.queryAll(criteria,pageable),HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@Log("新增文件上传接口")
|
||||||
|
@ApiOperation("新增文件上传接口")
|
||||||
|
@PreAuthorize("@el.check('tbUploadFileNew:add')")
|
||||||
|
public ResponseEntity<Object> create(@Validated @RequestBody TbUploadFileNew resources){
|
||||||
|
return new ResponseEntity<>(tbUploadFileNewService.create(resources),HttpStatus.CREATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
@Log("修改文件上传接口")
|
||||||
|
@ApiOperation("修改文件上传接口")
|
||||||
|
@PreAuthorize("@el.check('tbUploadFileNew:edit')")
|
||||||
|
public ResponseEntity<Object> update(@Validated @RequestBody TbUploadFileNew resources){
|
||||||
|
tbUploadFileNewService.update(resources);
|
||||||
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Log("删除文件上传接口")
|
||||||
|
@ApiOperation("删除文件上传接口")
|
||||||
|
@PreAuthorize("@el.check('tbUploadFileNew:del')")
|
||||||
|
@DeleteMapping
|
||||||
|
public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
|
||||||
|
tbUploadFileNewService.deleteAll(ids);
|
||||||
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传接口
|
||||||
|
*
|
||||||
|
* @return 返回调用信息
|
||||||
|
*/
|
||||||
|
@Log("上传并加密任务")
|
||||||
|
@ApiOperation("上传并加密任务")
|
||||||
|
@AnonymousAccess
|
||||||
|
@PostMapping("/fileUpload")
|
||||||
|
// @ResponseBody
|
||||||
|
public ResponseEntity<Object> fileUpload(@RequestParam("file") MultipartFile file, @RequestParam(value = "taskName") String taskName, @RequestParam(value = "isEcryptionNew") String isEcryptionNew, ServletRequest servletRequest) {
|
||||||
|
// 记录日志
|
||||||
|
log.info("TbUploadFileNewController:fileUpload | taskName="+taskName+"===== isEcryptionNew="+isEcryptionNew);
|
||||||
|
Integer isEcryption = Integer.parseInt(isEcryptionNew);
|
||||||
|
String name=file.getOriginalFilename();
|
||||||
|
|
||||||
|
String keyName = "";
|
||||||
|
String token = "";
|
||||||
|
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
|
||||||
|
Cookie[] cookies = httpServletRequest.getCookies();
|
||||||
|
for(Cookie cookie : cookies) {
|
||||||
|
//获取cookie的名字
|
||||||
|
String cookieName = cookie.getName();
|
||||||
|
token=cookie.getValue();
|
||||||
|
System.out.println(cookieName);
|
||||||
|
System.out.println(cookieName+" : "+ cookie.getValue() );
|
||||||
|
}
|
||||||
|
keyName = token+"==="+name;
|
||||||
|
Object keyValue = redisTemplate.opsForValue().get(keyName);
|
||||||
|
//防止用户多次提交
|
||||||
|
if (keyValue != null){
|
||||||
|
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_STAETING), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
redisTemplate.opsForValue().set(keyName, "value11");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int lastIndexOf = name.lastIndexOf(".");
|
||||||
|
String nameStr = name.substring(lastIndexOf);
|
||||||
|
// 校验文件格式
|
||||||
|
if (!((nameStr.equals(".xlsx")||nameStr.equals(".xls")) || nameStr.equals(".txt") ||nameStr.equals(".csv"))){
|
||||||
|
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_FORMAT), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
// 任务名称检验,为必填参数,且不能重复
|
||||||
|
if (StringUtils.isNotBlank(taskName)){
|
||||||
|
// modify by x bug fix on POSTMAN request
|
||||||
|
if (StringUtils.contains(taskName, FILE_PATH_SPLIT)){
|
||||||
|
taskName = taskName.substring(0, taskName.length()-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TbUploadFileNewQueryCriteria criteria = new TbUploadFileNewQueryCriteria();
|
||||||
|
criteria.setUploadFileTaskName(taskName);
|
||||||
|
List<TbUploadFileNewDto> uploadFileDtos = tbUploadFileNewService.queryAll(criteria);
|
||||||
|
if (CollectionUtil.isNotEmpty(uploadFileDtos)){
|
||||||
|
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_NAME_IS_EXIST), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.EMPTY_ARGUMENT), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验上传是否有文件
|
||||||
|
if (file== null ){
|
||||||
|
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成本地文件
|
||||||
|
TbUploadFileNewDto tbUploadFileNewDto = tbUploadFileNewService.encryptDataAndSaveToFileNew(file, taskName, isEcryption);
|
||||||
|
// 如果临时生成失败就修改状态为失败
|
||||||
|
if (Objects.isNull(tbUploadFileNewDto)){
|
||||||
|
log.error("TbUploadFileNewController:fileUpload | Operation Fail.tbUploadFileNewDto is Null");
|
||||||
|
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_FILE_SAVE_FAIL), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用异步任务
|
||||||
|
saveToFileNewTask.doRunTask(tbUploadFileNewDto);
|
||||||
|
|
||||||
|
redisTemplate.delete(keyName);
|
||||||
|
return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/aa")
|
||||||
|
@Log("查询文件上传接口")
|
||||||
|
@ApiOperation("查询文件上传接口")
|
||||||
|
@AnonymousAccess
|
||||||
|
public ResponseEntity<Object> aa(){
|
||||||
|
redisTemplate.opsForValue().set("key111", "value11");
|
||||||
|
Object key111 = redisTemplate.opsForValue().get("key111");
|
||||||
|
System.out.println("key111 = " + key111.toString());
|
||||||
|
|
||||||
|
redisTemplate.delete("key111");
|
||||||
|
|
||||||
|
Object aaaa = redisTemplate.opsForValue().get("key111");
|
||||||
|
System.out.println("aaaa = " + aaaa);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew;
|
||||||
|
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto;
|
||||||
|
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.List;
|
||||||
|
import java.io.IOException;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @website https://el-admin.vip
|
||||||
|
* @description 服务接口
|
||||||
|
* @author weizhongxi
|
||||||
|
* @date 2021-04-14
|
||||||
|
**/
|
||||||
|
public interface TbUploadFileNewService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据分页
|
||||||
|
* @param criteria 条件
|
||||||
|
* @param pageable 分页参数
|
||||||
|
* @return Map<String,Object>
|
||||||
|
*/
|
||||||
|
Map<String,Object> queryAll(TbUploadFileNewQueryCriteria criteria, Pageable pageable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询所有数据不分页
|
||||||
|
* @param criteria 条件参数
|
||||||
|
* @return List<TbUploadFileNewDto>
|
||||||
|
*/
|
||||||
|
List<TbUploadFileNewDto> queryAll(TbUploadFileNewQueryCriteria criteria);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID查询
|
||||||
|
* @param id ID
|
||||||
|
* @return TbUploadFileNewDto
|
||||||
|
*/
|
||||||
|
TbUploadFileNewDto findById(Integer id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建
|
||||||
|
* @param resources /
|
||||||
|
* @return TbUploadFileNewDto
|
||||||
|
*/
|
||||||
|
TbUploadFileNewDto create(TbUploadFileNew resources);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 编辑
|
||||||
|
* @param resources /
|
||||||
|
*/
|
||||||
|
void update(TbUploadFileNew resources);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多选删除
|
||||||
|
* @param ids /
|
||||||
|
*/
|
||||||
|
void deleteAll(Integer[] ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 导出数据
|
||||||
|
* @param all 待导出的数据
|
||||||
|
* @param response /
|
||||||
|
* @throws IOException /
|
||||||
|
*/
|
||||||
|
void download(List<TbUploadFileNewDto> all, HttpServletResponse response) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密并生成存储文件
|
||||||
|
*
|
||||||
|
* @param file
|
||||||
|
* @param taskName
|
||||||
|
* @param isEcryption
|
||||||
|
*/
|
||||||
|
TbUploadFileNewDto encryptDataAndSaveToFileNew(MultipartFile file, String taskName,Integer isEcryption);
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.List;
|
||||||
|
import me.zhengjie.annotation.Query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @website https://el-admin.vip
|
||||||
|
* @author weizhongxi
|
||||||
|
* @date 2021-04-14
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
public class TbUploadFileNewQueryCriteria{
|
||||||
|
|
||||||
|
/** 模糊 */
|
||||||
|
@Query(type = Query.Type.INNER_LIKE)
|
||||||
|
private String uploadFileTaskName;
|
||||||
|
|
||||||
|
/** 模糊 */
|
||||||
|
@Query(type = Query.Type.INNER_LIKE)
|
||||||
|
private String operation;
|
||||||
|
|
||||||
|
/** 不为空 */
|
||||||
|
@Query(type = Query.Type.NOT_NULL)
|
||||||
|
private Integer uploadTag;
|
||||||
|
|
||||||
|
/** 模糊 */
|
||||||
|
@Query(type = Query.Type.INNER_LIKE)
|
||||||
|
private String localSavePath;
|
||||||
|
|
||||||
|
/** 不为空 */
|
||||||
|
@Query(type = Query.Type.NOT_NULL)
|
||||||
|
private Integer isEncryption;
|
||||||
|
|
||||||
|
/** 模糊 */
|
||||||
|
@Query(type = Query.Type.INNER_LIKE)
|
||||||
|
private String fileFormat;
|
||||||
|
/** BETWEEN */
|
||||||
|
@Query(type = Query.Type.BETWEEN)
|
||||||
|
private List<Timestamp> gmtModified;
|
||||||
|
/** BETWEEN */
|
||||||
|
@Query(type = Query.Type.BETWEEN)
|
||||||
|
private List<Timestamp> uploadTime;
|
||||||
|
}
|
@ -0,0 +1,355 @@
|
|||||||
|
package me.zhengjie.modules.uploadnew.task;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.codec.Base64;
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import cn.hutool.crypto.SecureUtil;
|
||||||
|
import cn.hutool.extra.ssh.JschUtil;
|
||||||
|
import cn.hutool.extra.ssh.Sftp;
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import cn.hutool.http.HttpResponse;
|
||||||
|
import cn.hutool.poi.excel.ExcelUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.jcraft.jsch.Session;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import me.zhengjie.modules.upload.domain.UploadFile;
|
||||||
|
import me.zhengjie.modules.upload.service.dto.UploadFileDto;
|
||||||
|
import me.zhengjie.modules.upload.task.model.ResponseEncryptJsonContent;
|
||||||
|
import me.zhengjie.modules.upload.task.model.SendEncryptJsonContent;
|
||||||
|
import me.zhengjie.modules.upload.task.model.SendRemoteUpdateJsonContent;
|
||||||
|
import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew;
|
||||||
|
import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService;
|
||||||
|
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto;
|
||||||
|
import me.zhengjie.modules.uploadnew.util.ExcelUtils;
|
||||||
|
import me.zhengjie.modules.uploadnew.util.TxtUtils;
|
||||||
|
import me.zhengjie.utils.ConvertUtil;
|
||||||
|
import me.zhengjie.utils.DateUtil;
|
||||||
|
import me.zhengjie.utils.FileUtil;
|
||||||
|
import me.zhengjie.utils.StringUtils;
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
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 java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static me.zhengjie.modules.upload.consts.SysConst.*;
|
||||||
|
import static me.zhengjie.modules.upload.consts.SysConst.REMOTE_TRANS_DIR_PATH;
|
||||||
|
import static me.zhengjie.modules.upload.consts.UploadFileConst.*;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Scope("prototype")
|
||||||
|
@Slf4j
|
||||||
|
public class SaveToFileNewTask {
|
||||||
|
|
||||||
|
private Boolean booleanTag = true;
|
||||||
|
|
||||||
|
@Value(value = "${inter.appid}")
|
||||||
|
private String encryptAppId;
|
||||||
|
|
||||||
|
@Value(value = "${inter.tk}")
|
||||||
|
private String encryptTK;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密请求需要的各种配置信息
|
||||||
|
*/
|
||||||
|
@Value(value = "${inter.address}")
|
||||||
|
private String encryptAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分割标识 - 逗号
|
||||||
|
*/
|
||||||
|
private static final String SPLIT_TAG = ",";
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用加密接口的加密内容数量限制
|
||||||
|
*/
|
||||||
|
private final static int SEND_ENCRYPT_LIMIT = 200;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 时间格式
|
||||||
|
*/
|
||||||
|
private final static String FORMATE_TIMESTAMP = "yyyyMMddHHmmss";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 号码长度
|
||||||
|
*/
|
||||||
|
public static final Integer PRE_SEND_NUM_LENGTH = 11;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密号码长度
|
||||||
|
*/
|
||||||
|
public static final Integer SEND_NUM_LENGTH = 24;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TbUploadFileNewService tbUploadFileNewService;
|
||||||
|
|
||||||
|
@Async(value = "SendBigDataTaskExecutor")
|
||||||
|
public void doRunTask(TbUploadFileNewDto tbUploadFileNewDto) {
|
||||||
|
Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
||||||
|
log.info("====== [ task start running, task name is {} ] ======", "SendBigDataTask");
|
||||||
|
runTask(tbUploadFileNewDto);
|
||||||
|
Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
||||||
|
log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "SendBigDataTask", ConvertUtil.secondToTime(endMilliSecond - satrtMilliSecond));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 执行异步任务
|
||||||
|
*
|
||||||
|
* @param tbUploadFileNewDto 需要传输用到的Bean
|
||||||
|
*/
|
||||||
|
private void runTask(TbUploadFileNewDto tbUploadFileNewDto) {
|
||||||
|
boolean finalTag = false;
|
||||||
|
// 获取需求的地址
|
||||||
|
String tempFilesPath = tbUploadFileNewDto.getLocalSavePath();
|
||||||
|
|
||||||
|
// 单文件处理
|
||||||
|
finalTag = handleEachFileContent(tempFilesPath, tbUploadFileNewDto);
|
||||||
|
|
||||||
|
|
||||||
|
// 更新状态为成功,更新解析成功的条数
|
||||||
|
TbUploadFileNew tbUploadFileNew = new TbUploadFileNew();
|
||||||
|
if (finalTag) {
|
||||||
|
BeanUtils.copyProperties(tbUploadFileNewDto, tbUploadFileNew);
|
||||||
|
tbUploadFileNew.setFileTransSuccessCount(tbUploadFileNewDto.getFileCount());
|
||||||
|
tbUploadFileNew.setUploadTag(SUCCESS_TAG);
|
||||||
|
tbUploadFileNewService.update(tbUploadFileNew);
|
||||||
|
} else {
|
||||||
|
// 失败进行容错
|
||||||
|
BeanUtils.copyProperties(tbUploadFileNewDto, tbUploadFileNew);
|
||||||
|
tbUploadFileNew.setUploadTag(FAIL_TAG);
|
||||||
|
tbUploadFileNew.setFileTransSuccessCount(tbUploadFileNewDto.getFileCount());
|
||||||
|
tbUploadFileNewService.update(tbUploadFileNew);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对每一个文件的处理操作
|
||||||
|
*
|
||||||
|
* @param filePath 进行操作的每一个文件的路径
|
||||||
|
*/
|
||||||
|
private boolean handleEachFileContent(String filePath, TbUploadFileNewDto tbUploadFileNewDto) {
|
||||||
|
//根据文件类型进行解析
|
||||||
|
List<String> list = null;
|
||||||
|
try {
|
||||||
|
if("excel文件".equals(tbUploadFileNewDto.getFileFormat())){
|
||||||
|
list = ExcelUtils.excelParseListByUrl(filePath);
|
||||||
|
}else if ("txt文件".equals(tbUploadFileNewDto.getFileFormat())){
|
||||||
|
list = TxtUtils.txtParseListVyUrl(filePath);
|
||||||
|
}else if ("csv文件".equals(tbUploadFileNewDto.getFileFormat())){
|
||||||
|
list = TxtUtils.csvParseListByUrl(filePath);
|
||||||
|
}
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if (CollectionUtil.isNotEmpty(list)){
|
||||||
|
batchSendToEncrypt(filePath, list);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加入一个全局控制开关
|
||||||
|
if (!booleanTag){
|
||||||
|
return Boolean.FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 把临时存储的文件进行删除
|
||||||
|
boolean delFileTag = delTempSaveFile(filePath);
|
||||||
|
|
||||||
|
// 文件传输给2号库服务器 - 走sftp协议 - 支持断点续传
|
||||||
|
transFileToOtherServer(filePath + TEMP_FILE_END_STR);
|
||||||
|
|
||||||
|
// 调用远程接口完成一条记录更新
|
||||||
|
boolean sendUpdatePostReqTag = sendUpdatePostReq(filePath+ TEMP_FILE_END_STR, tbUploadFileNewDto);
|
||||||
|
// fixme 这里要修改之前的平台给一个更新接口,然后这边可以用rpc调用,也可以用http,也可以考虑直接消息中间件进行解耦
|
||||||
|
if (delFileTag && sendUpdatePostReqTag) {
|
||||||
|
return Boolean.TRUE;
|
||||||
|
}
|
||||||
|
return Boolean.FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void batchSendToEncrypt(String filePath, List<String> fileAllLinesList) {
|
||||||
|
List<List<String>> partition = Lists.partition(fileAllLinesList, SEND_ENCRYPT_LIMIT);
|
||||||
|
|
||||||
|
partition.forEach(
|
||||||
|
list -> {
|
||||||
|
// 装配需要的Json参数
|
||||||
|
SendEncryptJsonContent sendEncryptJsonContent = new SendEncryptJsonContent();
|
||||||
|
//fixme 还有一个过期时间参数为选填参数,暂时不做设置
|
||||||
|
String tels = StringUtils.listPrintWithSpecialSplit(list, null);
|
||||||
|
sendEncryptJsonContent.setTels(Base64.encode(tels));
|
||||||
|
sendEncryptJsonContent.setReqId(RandomUtil.randomString(10));
|
||||||
|
sendEncryptJsonContent.setAppId(encryptAppId);
|
||||||
|
// 配置 sign
|
||||||
|
String signStr = makeSign();
|
||||||
|
if (StringUtils.isBlank(signStr)) {
|
||||||
|
log.error("SaveToFileTask|makeSign fail!");
|
||||||
|
}
|
||||||
|
sendEncryptJsonContent.setSig(signStr);
|
||||||
|
// 组装成JSON
|
||||||
|
String readSendJsonStr = JSON.toJSONString(sendEncryptJsonContent);
|
||||||
|
log.info("SaveToFileTask|batchSendToEncrypt ready send json is : {}", readSendJsonStr);
|
||||||
|
int count = 0;
|
||||||
|
while (count < 3) {
|
||||||
|
// 调用HTTP请求发送数据
|
||||||
|
HttpResponse httpResponse = sendPostReq(readSendJsonStr);
|
||||||
|
if (httpResponse.isOk() && httpResponse.body().contains("success")) {
|
||||||
|
log.info("========== [SaveToFileTask|batchSendToEncrypt request success, response is {} ] ==========", httpResponse.body());
|
||||||
|
String responseStr = httpResponse.body();
|
||||||
|
// 解析返回的结果,并写回本地
|
||||||
|
if (!parseResponseStr(filePath, responseStr)) {
|
||||||
|
log.error("========== [ SaveToFileTask|batchSendToEncrypt parse fail ] ==========");
|
||||||
|
booleanTag = false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
count++;
|
||||||
|
try {
|
||||||
|
// 重新发送前休眠3秒
|
||||||
|
Thread.sleep(3_0000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("SaveToFileTask|batchSendToEncrypt sleep ERROR. message is", e.getMessage(), e);
|
||||||
|
}
|
||||||
|
log.error("========== [SaveToFileTask|batchSendToEncrypt request fail, response is {} ] ==========", httpResponse.body());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count >= 3) {
|
||||||
|
log.error("========== [SaveToFileTask|batchSendToEncrypt update send status fail, url is {} ] ==========", encryptAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String makeSign() {
|
||||||
|
|
||||||
|
String signBuilder = encryptAppId +
|
||||||
|
encryptTK +
|
||||||
|
DateUtil.localDateTimeFormat(LocalDateTime.now(), FORMATE_TIMESTAMP);
|
||||||
|
|
||||||
|
return SecureUtil.md5(signBuilder).toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用HTTP请求发送Post请求
|
||||||
|
*
|
||||||
|
* @param json 请求的body内容
|
||||||
|
* @return 返回请求结果
|
||||||
|
*/
|
||||||
|
private HttpResponse sendPostReq(String json) {
|
||||||
|
|
||||||
|
HttpResponse httpResponse = HttpRequest
|
||||||
|
.post(encryptAddress)
|
||||||
|
.header("Content-Type", "application/json;charset=utf-8")
|
||||||
|
.body(json)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
return httpResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean parseResponseStr(String filePath, String responseStr) {
|
||||||
|
ResponseEncryptJsonContent responseContent = JSONArray.parseObject(responseStr, ResponseEncryptJsonContent.class);
|
||||||
|
// 处理需要的加密号串
|
||||||
|
String tels = responseContent.getTels();
|
||||||
|
|
||||||
|
String[] splitResTels = StringUtils.split(Base64.decodeStr(tels), SPLIT_TAG);
|
||||||
|
|
||||||
|
if (splitResTels != null && splitResTels.length > 0) {
|
||||||
|
List<String> stringList = Arrays.asList(splitResTels);
|
||||||
|
// TODO: 2021/1/5 0005 这里保存文件的路径进行定义下
|
||||||
|
String responseFilePath = filePath + TEMP_FILE_END_STR;
|
||||||
|
// 写入指定路径
|
||||||
|
writeToFile(stringList, responseFilePath);
|
||||||
|
return Boolean.TRUE;
|
||||||
|
}
|
||||||
|
return Boolean.FALSE;
|
||||||
|
}
|
||||||
|
private void writeToFile(List<String> collect, String fullPath) {
|
||||||
|
// 构建存储文件
|
||||||
|
try {
|
||||||
|
if (!FileUtil.exist(fullPath)) {
|
||||||
|
Files.write(Paths.get(fullPath), collect, StandardOpenOption.CREATE_NEW);
|
||||||
|
} else {
|
||||||
|
Files.write(Paths.get(fullPath), collect, StandardOpenOption.APPEND);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("write prepare send file fail, please check param, fullPath is {}, {}", fullPath, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean delTempSaveFile(String tempFilesPath) {
|
||||||
|
boolean del = FileUtil.del(tempFilesPath);
|
||||||
|
if (del) {
|
||||||
|
log.info("======== [success del file, file path is {} ] ========", tempFilesPath);
|
||||||
|
return Boolean.TRUE;
|
||||||
|
} else {
|
||||||
|
log.error("======== [fail del file, file path is {} ] ========", tempFilesPath);
|
||||||
|
return Boolean.FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void transFileToOtherServer(String filePath) {
|
||||||
|
|
||||||
|
Session session = JschUtil.createSession(REMOTE_TRANS_HOST, REMOTE_TRANS_PORT, REMOTE_TRANS_SSH_USER, REMOTE_TRANS_SSH_PW);
|
||||||
|
|
||||||
|
Sftp sftp = JschUtil.createSftp(session);
|
||||||
|
|
||||||
|
sftp.put(filePath, REMOTE_TRANS_DIR_PATH, Sftp.Mode.RESUME);
|
||||||
|
|
||||||
|
sftp.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用HTTP请求发送更新记录的Post请求
|
||||||
|
*
|
||||||
|
* @param path 单个存储在本地的路径地址
|
||||||
|
* @return 返回请求结果
|
||||||
|
*/
|
||||||
|
private boolean sendUpdatePostReq(String path,TbUploadFileNewDto tbUploadFileNewDto) {
|
||||||
|
// 构建发送参数
|
||||||
|
SendRemoteUpdateJsonContent sendRemoteUpdateJsonContent = new SendRemoteUpdateJsonContent();
|
||||||
|
|
||||||
|
BeanUtil.copyProperties(tbUploadFileNewDto, sendRemoteUpdateJsonContent);
|
||||||
|
String fileFullName = FileUtil.getName(path);
|
||||||
|
if (StringUtils.isNotBlank(fileFullName)) {
|
||||||
|
// FIXME: 2021/1/12 0012
|
||||||
|
sendRemoteUpdateJsonContent.setSftpFilePath(REMOTE_TRANS_DIR_PATH + fileFullName);
|
||||||
|
}
|
||||||
|
sendRemoteUpdateJsonContent.setFileTransSuccessCount(tbUploadFileNewDto.getFileCount());
|
||||||
|
sendRemoteUpdateJsonContent.setUploadRemoteTaskName(tbUploadFileNewDto.getUploadFileTaskName());
|
||||||
|
|
||||||
|
// 转成Json字符串
|
||||||
|
String readySendJson = JSON.toJSONString(sendRemoteUpdateJsonContent);
|
||||||
|
log.info("====== [SaveToFileTask|sendUpdatePostReq , readySendJson is {} ] ======", readySendJson);
|
||||||
|
// 发送请求
|
||||||
|
HttpResponse httpResponse = HttpRequest
|
||||||
|
.post(REMOTE_UPDATE_ADDR)
|
||||||
|
.header("Content-Type", "application/json;charset=utf-8")
|
||||||
|
.body(readySendJson)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
// 解析回送请求
|
||||||
|
if (httpResponse.isOk() && httpResponse.body().contains("SUCCESS")) {
|
||||||
|
log.info("====== [success send upload record request! ]======");
|
||||||
|
return Boolean.TRUE;
|
||||||
|
} else {
|
||||||
|
log.error("====== [ SaveToFileTask|sendUpdatePostReq , send upload record request Fail! , reason is {} ]======", httpResponse.body());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Boolean.FALSE;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
package me.zhengjie.modules.uploadnew.util;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
@Slf4j
|
||||||
|
public class ExcelUtils {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* excel解析方法
|
||||||
|
* @param inputStream 文件流
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static List<String> excelParseList(InputStream inputStream) throws IOException {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
Workbook workbook = new XSSFWorkbook(inputStream);
|
||||||
|
|
||||||
|
//获取第一张工作表
|
||||||
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
//从第二行开始获取
|
||||||
|
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
|
||||||
|
//循环获取工作表的每一行
|
||||||
|
Row sheetRow = sheet.getRow(i);
|
||||||
|
|
||||||
|
for (int j = 0; j < sheetRow.getPhysicalNumberOfCells(); j++) {
|
||||||
|
//将每一个单元格的值装入列集合
|
||||||
|
sheetRow.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
|
||||||
|
list.add(sheetRow.getCell(j).getStringCellValue());
|
||||||
|
}
|
||||||
|
//关闭资源
|
||||||
|
workbook.close();
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
log.info("ExcelUtils | excelParseList ==========未找到文件");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.info("ExcelUtils | excelParseList ==========解析失败!!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* excel解析方法
|
||||||
|
* @param url 文件路径
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static List<String> excelParseListByUrl(String url) throws IOException {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
InputStream inputStream = new FileInputStream(new File(url)) ;
|
||||||
|
try {
|
||||||
|
Workbook workbook = new XSSFWorkbook(inputStream);
|
||||||
|
|
||||||
|
//获取第一张工作表
|
||||||
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
//从第二行开始获取
|
||||||
|
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
|
||||||
|
//循环获取工作表的每一行
|
||||||
|
Row sheetRow = sheet.getRow(i);
|
||||||
|
|
||||||
|
for (int j = 0; j < sheetRow.getPhysicalNumberOfCells(); j++) {
|
||||||
|
//将每一个单元格的值装入列集合
|
||||||
|
sheetRow.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
|
||||||
|
list.add(sheetRow.getCell(j).getStringCellValue());
|
||||||
|
}
|
||||||
|
//关闭资源
|
||||||
|
workbook.close();
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
log.info("ExcelUtils | excelParseList ==========未找到文件");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.info("ExcelUtils | excelParseList ==========解析失败!!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,151 @@
|
|||||||
|
package me.zhengjie.modules.uploadnew.util;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
@Slf4j
|
||||||
|
public class TxtUtils {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(TxtUtils.class);
|
||||||
|
/**
|
||||||
|
* txt解析方法
|
||||||
|
* @param inputStream 文件流
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static List<String> txtParseList(InputStream inputStream) throws IOException {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
BufferedReader d = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
try {
|
||||||
|
String count;
|
||||||
|
while((count = d.readLine()) != null){
|
||||||
|
String u = count.toUpperCase();
|
||||||
|
list.add(u);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
log.info("TxtUtils | txtParseList ==========未找到文件");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
log.info("TxtUtils | txtParseList ==========解析失败!!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}finally {
|
||||||
|
d.close();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* txt解析方法
|
||||||
|
* @param url 文件路径
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static List<String> txtParseListVyUrl(String url) throws IOException {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
InputStream inputStream = new FileInputStream(new File(url)) ;
|
||||||
|
BufferedReader d = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
try {
|
||||||
|
String count;
|
||||||
|
while((count = d.readLine()) != null){
|
||||||
|
String u = count.toUpperCase();
|
||||||
|
list.add(u);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
log.info("TxtUtils | txtParseList ==========未找到文件");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
log.info("TxtUtils | txtParseList ==========解析失败!!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}finally {
|
||||||
|
d.close();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* csv解析方法
|
||||||
|
* @param inputStream 文件流
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static List<String> csvParseList(InputStream inputStream) throws IOException {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
BufferedReader d = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
try {
|
||||||
|
String count;
|
||||||
|
while((count = d.readLine()) != null){
|
||||||
|
String u = count.toUpperCase();
|
||||||
|
builder.append(u+",");
|
||||||
|
}
|
||||||
|
log.error("========== [builder.toString] =========="+builder.toString());
|
||||||
|
if (builder!=null){
|
||||||
|
String str= builder.toString();
|
||||||
|
String[] split = str.split(",");
|
||||||
|
for (String sp : split) {
|
||||||
|
list.add(sp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
log.info("TxtUtils | csvParseList ==========未找到文件");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
log.info("TxtUtils | csvParseList ==========解析失败!!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}finally {
|
||||||
|
d.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* csv解析方法
|
||||||
|
* @param url 文件路径
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static List<String> csvParseListByUrl(String url) throws IOException {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
InputStream inputStream = new FileInputStream(new File(url)) ;
|
||||||
|
BufferedReader d = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
try {
|
||||||
|
String count;
|
||||||
|
while((count = d.readLine()) != null){
|
||||||
|
String u = count.toUpperCase();
|
||||||
|
builder.append(u+",");
|
||||||
|
}
|
||||||
|
log.error("========== [builder.toString] =========="+builder.toString());
|
||||||
|
if (builder!=null){
|
||||||
|
String str= builder.toString();
|
||||||
|
String[] split = str.split(",");
|
||||||
|
for (String sp : split) {
|
||||||
|
list.add(sp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
log.info("TxtUtils | csvParseList ==========未找到文件");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
log.info("TxtUtils | csvParseList ==========解析失败!!");
|
||||||
|
e.printStackTrace();
|
||||||
|
}finally {
|
||||||
|
d.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue