From ea4f0a824b56cd56e089420eed867a47e771abc5 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Wed, 8 Mar 2023 13:50:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../account/CustomAuthenticationProvider.java | 2 +- .../straw/controller/SysUserController.java | 4 +- .../service/impl/SysUserServiceImpl.java | 2 +- .../AliYunSmsUtils.java | 2 +- .../EncryptDecryptUtil.java | 2 +- .../xkrs/straw/utilsold/ExcelImportUtil.java | 206 ---------- .../com/xkrs/straw/utilsold/FileFastDfs.java | 383 ------------------ .../xkrs/straw/utilsold/FileUploadUtils.java | 52 --- 8 files changed, 6 insertions(+), 647 deletions(-) rename src/main/java/com/xkrs/straw/{utilsold => utilsnew}/AliYunSmsUtils.java (98%) rename src/main/java/com/xkrs/straw/{utilsold => utilsnew}/EncryptDecryptUtil.java (98%) delete mode 100644 src/main/java/com/xkrs/straw/utilsold/ExcelImportUtil.java delete mode 100644 src/main/java/com/xkrs/straw/utilsold/FileFastDfs.java delete mode 100644 src/main/java/com/xkrs/straw/utilsold/FileUploadUtils.java diff --git a/src/main/java/com/xkrs/common/account/CustomAuthenticationProvider.java b/src/main/java/com/xkrs/common/account/CustomAuthenticationProvider.java index e6cf57e..287d646 100644 --- a/src/main/java/com/xkrs/common/account/CustomAuthenticationProvider.java +++ b/src/main/java/com/xkrs/common/account/CustomAuthenticationProvider.java @@ -21,7 +21,7 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; -import static com.xkrs.straw.utilsold.EncryptDecryptUtil.encry256; +import static com.xkrs.straw.utilsnew.EncryptDecryptUtil.encry256; /** * 自定义认证Provider diff --git a/src/main/java/com/xkrs/straw/controller/SysUserController.java b/src/main/java/com/xkrs/straw/controller/SysUserController.java index 8693135..0063448 100644 --- a/src/main/java/com/xkrs/straw/controller/SysUserController.java +++ b/src/main/java/com/xkrs/straw/controller/SysUserController.java @@ -14,7 +14,7 @@ import com.xkrs.straw.model.qo.SysUserVipLevelQo; import com.xkrs.straw.model.vo.SysUserVo; import com.xkrs.straw.service.SysUserService; import com.xkrs.straw.utilsnew.*; -import com.xkrs.straw.utilsold.AliYunSmsUtils; +import com.xkrs.straw.utilsnew.AliYunSmsUtils; import com.xkrs.straw.utilsnew.RandomUtil; import org.apache.hc.core5.util.TextUtils; import org.springframework.context.i18n.LocaleContextHolder; @@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationErrorList; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; -import static com.xkrs.straw.utilsold.EncryptDecryptUtil.encry256; +import static com.xkrs.straw.utilsnew.EncryptDecryptUtil.encry256; /** * 系统用户Controller diff --git a/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java b/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java index d1850f0..8345675 100644 --- a/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java +++ b/src/main/java/com/xkrs/straw/service/impl/SysUserServiceImpl.java @@ -33,7 +33,7 @@ import java.util.Locale; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; import static com.xkrs.straw.utilsold.DateTimeUtil.dateTimeToString; -import static com.xkrs.straw.utilsold.EncryptDecryptUtil.encry256; +import static com.xkrs.straw.utilsnew.EncryptDecryptUtil.encry256; /** * 系统用户服务实现 diff --git a/src/main/java/com/xkrs/straw/utilsold/AliYunSmsUtils.java b/src/main/java/com/xkrs/straw/utilsnew/AliYunSmsUtils.java similarity index 98% rename from src/main/java/com/xkrs/straw/utilsold/AliYunSmsUtils.java rename to src/main/java/com/xkrs/straw/utilsnew/AliYunSmsUtils.java index d7bbfbd..ac41cda 100644 --- a/src/main/java/com/xkrs/straw/utilsold/AliYunSmsUtils.java +++ b/src/main/java/com/xkrs/straw/utilsnew/AliYunSmsUtils.java @@ -1,4 +1,4 @@ -package com.xkrs.straw.utilsold; +package com.xkrs.straw.utilsnew; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; diff --git a/src/main/java/com/xkrs/straw/utilsold/EncryptDecryptUtil.java b/src/main/java/com/xkrs/straw/utilsnew/EncryptDecryptUtil.java similarity index 98% rename from src/main/java/com/xkrs/straw/utilsold/EncryptDecryptUtil.java rename to src/main/java/com/xkrs/straw/utilsnew/EncryptDecryptUtil.java index 3fcba69..3865a21 100644 --- a/src/main/java/com/xkrs/straw/utilsold/EncryptDecryptUtil.java +++ b/src/main/java/com/xkrs/straw/utilsnew/EncryptDecryptUtil.java @@ -1,4 +1,4 @@ -package com.xkrs.straw.utilsold; +package com.xkrs.straw.utilsnew; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; diff --git a/src/main/java/com/xkrs/straw/utilsold/ExcelImportUtil.java b/src/main/java/com/xkrs/straw/utilsold/ExcelImportUtil.java deleted file mode 100644 index feb20f6..0000000 --- a/src/main/java/com/xkrs/straw/utilsold/ExcelImportUtil.java +++ /dev/null @@ -1,206 +0,0 @@ -package com.xkrs.straw.utilsold; - -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.springframework.web.multipart.MultipartFile; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.*; - -/** - * @author ZHY - * @date 2020/6/17 15:56 - */ -public class ExcelImportUtil { - private Workbook wb; - private Sheet sheet; - private Row row; - - String xls = ".xls"; - String xlsx = ".xlsx"; - - /** - * 读取Excel - * - * @author ZHY - */ - public ExcelImportUtil(MultipartFile file) throws Exception { - String filename = file.getOriginalFilename(); - String ext = filename.substring(filename.lastIndexOf(".")); - InputStream is = file.getInputStream(); - if (xls.equals(ext)) { - wb = new HSSFWorkbook(is); - } else if (xlsx.equals(ext)) { - wb = new XSSFWorkbook(is); - } else { - wb = null; - } - } - - public ExcelImportUtil(File file) throws Exception { - String filename = file.getAbsolutePath(); - String ext = filename.substring(filename.lastIndexOf(".")); - InputStream is = new FileInputStream(file); - if (xls.equals(ext)) { - wb = new HSSFWorkbook(is); - } else if (xlsx.equals(ext)) { - wb = new XSSFWorkbook(is); - } else { - wb = null; - } - } - - - /** - * 读取Excel表格表头的内容输出 - */ - public List> readExcelTitleOut() { - List> list = new ArrayList>(); - if (wb != null) { - sheet = wb.getSheetAt(0); - row = sheet.getRow(0); - // 标题总列数 - int colNum = row.getPhysicalNumberOfCells(); - - System.out.println("colNum:" + colNum); - - Map map = new LinkedHashMap(); - - for (int i = 0; i < colNum; i++) { - String stringCellValue = row.getCell(i).getStringCellValue(); - map.put(stringCellValue, null); - } - list.add(map); - return list; - } - return list; - } - - /** - * 读取Excel表格表头 - */ - public String[] readExcelTitle() { - String[] title = {}; - if (wb != null) { - sheet = wb.getSheetAt(0); - row = sheet.getRow(0); - // 标题总列数 - int colNum = row.getPhysicalNumberOfCells(); - - System.out.println("colNum:" + colNum); - - title = new String[colNum]; - - for (int i = 0; i < colNum; i++) { - Cell cell = row.getCell(i); - title[i] = cell.getStringCellValue().replaceAll("\\s+", ""); - } - } - return title; - } - - /** - * 读取Excel表格的某一个数值 - * - * @return - */ - public Map readExcelSomeTitle() { - Map map = new LinkedHashMap<>(); - if (wb != null) { - sheet = wb.getSheetAt(0); - String title = parseExcel(sheet.getRow(2).getCell(1)); - String remark = parseExcel(sheet.getRow(3).getCell(1)); - map.put("date", title); - map.put("remark", remark); - } - return map; - } - - /** - * 读取Excel数据内容 - */ - public List> readExcelContent() { - List> list = new ArrayList<>(); - if (wb != null) { - //获取sheet表 - sheet = wb.getSheetAt(0); - // 得到总行数 - int rowNum = sheet.getLastRowNum(); - //获取表头的标题 - String[] readExcelTitle = readExcelTitle(); - // 正文内容应该从第二行开始,第一行为表头的标题 - for (int i = 1; i <= rowNum; i++) { - row = sheet.getRow(i); - if (row == null) { - continue; - } - Map map = new LinkedHashMap<>(); - for (int j = 0; j < readExcelTitle.length; j++) { - //获取每一列的数据值 - String str = parseExcel(row.getCell(j)); - //判断对应行的列值是否为空 - if (str != null || "".equals(str)) { - //表头的标题为键值,列值为值 - map.put(readExcelTitle[j], str); - } - } - //判段添加的对象是否为空 - if (!map.isEmpty()) { - list.add(map); - } - } - } - return list; - } - - /** - * 根据Cell类型设置数据 - */ - int ss = 20; - int xx = 58; - - private String parseExcel(Cell cell) { - String result = ""; - if (cell != null) { - SimpleDateFormat sdf = null; - switch (cell.getCellType()) { - // 数字类型 - case NUMERIC: - // 处理日期格式、时间格式 - if (DateUtil.isCellDateFormatted(cell)) { - if (cell.getCellStyle().getDataFormat() == ss) { - sdf = new SimpleDateFormat("HH:mm"); - } else {// 日期 - sdf = new SimpleDateFormat("yyyy-MM-dd"); - } - String dateFormat = sdf.format(cell.getDateCellValue()); - result = dateFormat; - } else if (cell.getCellStyle().getDataFormat() == xx) { - // 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58) - sdf = new SimpleDateFormat("yyyy-MM-dd"); - double value = cell.getNumericCellValue(); - Date date = DateUtil.getJavaDate(value); - result = sdf.format(date); - } else { - double value = cell.getNumericCellValue(); - DecimalFormat format = new DecimalFormat("#.###########"); - String strVal = format.format(value); - result = strVal; - } - break; - // String类型 - case STRING: - result = cell.getRichStringCellValue().toString(); - break; - default: - break; - } - } - return result; - } -} diff --git a/src/main/java/com/xkrs/straw/utilsold/FileFastDfs.java b/src/main/java/com/xkrs/straw/utilsold/FileFastDfs.java deleted file mode 100644 index 7860b9d..0000000 --- a/src/main/java/com/xkrs/straw/utilsold/FileFastDfs.java +++ /dev/null @@ -1,383 +0,0 @@ -package com.xkrs.straw.utilsold; - -import cn.hutool.core.io.resource.InputStreamResource; -import cn.hutool.core.thread.ThreadUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.http.HttpUtil; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import io.tus.java.client.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.kafka.core.KafkaTemplate; -import org.springframework.stereotype.Component; -import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; -import javax.servlet.http.HttpServletResponse; -import java.io.*; -import java.net.URL; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.Base64; -import java.util.HashMap; -import java.util.Objects; - -/** - * @author dong - * @date 2020/12/31 - */ -@Component -public class FileFastDfs { - - @Value("${dfs.ip}") - public String ip; - @Value("${dfs.port}") - public String port; - - @Resource - private KafkaTemplate kafkaTemplate; - private static final Logger log = LoggerFactory.getLogger(FileFastDfs.class); - private static final String STATUS = "status"; - private static final String UPLOAD_BIG_PATH = "http://192.168.2.166:2001/group1/big/upload"; - private static final String UPLOAD_PATH = "http://192.168.2.166:2001/group1/upload"; - - /** - * 文件上传到dfs服务器 - * - * @param file - * @param dir - * @return - */ - public String uploadsFile(MultipartFile file, String dir) { - File file1 = null; - InputStreamResource isr = null; - try { - file1 = multipartFileToFile(file); - isr = new InputStreamResource(file.getInputStream(), file.getOriginalFilename()); - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } catch (Exception e) { - e.printStackTrace(); - } - //文件地址 - //声明参数集合 - HashMap paramMap = new HashMap<>(7); - //文件 - paramMap.put("file", isr); - //输出 - paramMap.put("output", "json"); - //自定义路径 - paramMap.put("path", dir); - //场景 文件分类 - if (null == file1) { - return null; - } - String name = file1.getName(); - System.err.println("file:" + file1.getName()); - paramMap.put("scene", name.substring(name.lastIndexOf(".") + 1)); - paramMap.put("fileName", System.currentTimeMillis() + "" + Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."))); - System.err.println(paramMap); - // - String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/upload", paramMap); - System.err.println(result); - - // 拿出状态码,判断上传状态 - ObjectMapper mapper = new ObjectMapper(); - JsonNode node = null; - try { - node = mapper.readTree(result); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - final String fail = "fail"; - if (null != node) { - if (fail.equals(node.path(STATUS).asText())) { - file1.delete(); - return null; - } - } - file1.delete(); - return result; - } - - public String upddloadBigFile(File file, String dir, String fileType) { - // 下面这个一定要注意,如果不设置为true,将会直接返回301 - System.setProperty("http.strictPostRedirect", Boolean.toString(true)); - TusClient tusClient = new TusClient(); - try { - tusClient.setUploadCreationURL(new URL(UPLOAD_BIG_PATH)); - -// tusClient.enableResuming(new TusRedisUrlStrore()); - tusClient.enableResuming(new TusURLMemoryStore()); - - final TusUpload upload = new TusUpload(file); - System.out.println("start upload......"); - TusExecutor executor = new TusExecutor() { - @Override - protected void makeAttempt() throws ProtocolException, IOException { - TusUploader uploader = tusClient.resumeOrCreateUpload(upload); - uploader.setChunkSize(1024 * 1024); - - long start = System.currentTimeMillis(); - do { - long totalBytes = upload.getSize(); - long bytesUploaded = uploader.getOffset(); - double progress = (double) bytesUploaded / totalBytes * 100; - - System.out.printf("Upload at %06.2f%%.\n", progress); - } while (uploader.uploadChunk() > -1); - - uploader.finish(); - - String uploadUrl = uploader.getUploadURL().toString(); - System.out.println("Upload finished."); - System.out.format("Upload available at: %s\n", uploadUrl); - - long end = System.currentTimeMillis(); - - System.out.println((end - start) + "ms"); - // 使用hutool进行秒传置换url - String fileId = StrUtil.subAfter(uploadUrl, UPLOAD_BIG_PATH + "/", true); - System.out.println("fileId: " + fileId); - String url = StrUtil.format("{}?md5={}&output=json", UPLOAD_PATH, fileId); - System.out.println("url: " + url); - // 上传大文件的时候(1.xG)需要sleep一下,要不然会有问题 - ThreadUtil.sleep(5000); - String result = HttpUtil.get(url); - } - }; - executor.makeAttempts(); - } catch (IOException | ProtocolException e) { - e.printStackTrace(); - } - return null; - } - - /** - * @description: 文件base64流获取 - * @params [fileName : 文件路径] - * @return: java.lang.String - * @author: chqf - * @time: 2020/3/31 2020/3/31 - */ - public String geddtBase64(String fileName) { - if (fileName == null || "".equals(fileName)) { - return null; - } - InputStream fis = null; - URL url = null; - try { - url = new URL("http://" + ip + ":" + port + "/" + URLEncoder.encode(fileName, StandardCharsets.UTF_8)); - System.err.println(url); - fis = url.openStream(); - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } - final ByteArrayOutputStream data = new ByteArrayOutputStream(); - String imgStr = ""; - try { - if (fis == null) { - return null; - } - int len = -1; - byte[] buf = new byte[2048]; - while (-1 != (len = fis.read(buf, 0, buf.length))) { - data.write(buf, 0, len); - } - Base64.Encoder encoder = Base64.getEncoder(); - imgStr = encoder.encodeToString(data.toByteArray()); - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } finally { - try { - if (null != fis) { - fis.close(); - } - data.close(); - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } - } - - return "data:image/jpeg;base64," + imgStr; - } - - /** - * @description: 文件格式转换multipartFil 转为 File 格式 - * @params [file] - * @return: java.io.File - * @author: dong - * @time: 2020/3/31 2020/3/31 - */ - public File multipartFileToFile(MultipartFile file) throws Exception { - - File toFile = null; - if ("".equals(file) || file.getSize() <= 0) { - file = null; - } else { - InputStream ins; - ins = file.getInputStream(); - toFile = new File(Objects.requireNonNull(file.getOriginalFilename())); - inputStreamToFile(ins, toFile); - ins.close(); - } - return toFile; - } - - private static void inputStreamToFile(InputStream ins, File file) { - OutputStream os = null; - try { - os = new FileOutputStream(file); - int bytesRead = 0; - byte[] buffer = new byte[8192]; - while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { - os.write(buffer, 0, bytesRead); - } - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } finally { - try { - if (null != os) { - os.close(); - } - if (null != ins) { - ins.close(); - } - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } - } - } - - /** - * @description: 文件base64流获取 - * @params [fileName : 文件路径] - * @return: java.lang.String - * @author: dong - * @time: 2020/3/31 2020/3/31 - */ - public Boolean getFile(String fileName, HttpServletResponse response) { - if (fileName == null || "".equals(fileName)) { - return null; - } - InputStream fis = null; - URL url = null; - try { - url = new URL("http://" + ip + ":" + port + "/" + URLEncoder.encode(fileName, StandardCharsets.UTF_8)); - System.err.println(url); - fis = url.openStream(); - response.reset(); - response.setContentType("bin"); - response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } - // - byte[] b = new byte[256]; - int len; - try { - assert fis != null; - while ((len = fis.read(b)) > 0) { - response.getOutputStream().write(b, 0, len); - } - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } finally { - try { - if (null != fis) { - fis.close(); - } - } catch (IOException e) { - log.info("IO错误{}", (Object) e.getStackTrace()); - } - } - return true; - } - - /** - * 删除文件 path - * - * @param filePath 文件路径 上传返回的path - * @return - */ - public Boolean deleteFileByPath(String filePath) { - if (filePath == null || "".equals(filePath)) { - return false; - } - HashMap paramMap = new HashMap<>(1); - // 参数 - paramMap.put("path", filePath); - - String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/delete", paramMap); - System.out.println(result); - ObjectMapper mapper = new ObjectMapper(); - JsonNode node = null; - try { - node = mapper.readTree(result); - } catch (JsonProcessingException e) { - - e.printStackTrace(); - } - - final String statusOk = "ok"; - - if (null == node) { - log.error("返回结果为空"); - return false; - } - if (!statusOk.equals(node.path(STATUS).asText())) { - log.error("未删除成功"); - return false; - } - return true; - } - - /** - * 删除文件 path - * - * @param md5 上传返回的md5 - * @return - */ - public Boolean deleteFileByMd5(String md5) { - if (md5 == null || "".equals(md5)) { - return false; - } - HashMap paramMap = new HashMap<>(1); - // 参数 - paramMap.put("md5", md5); - - String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/delete", paramMap); - - ObjectMapper mapper = new ObjectMapper(); - JsonNode node = null; - try { - node = mapper.readTree(result); - } catch (JsonProcessingException e) { - - e.printStackTrace(); - } - final String status = "status"; - final String statusOk = "ok"; - - if (null == node) { - log.error("返回结果为空"); - return false; - } - if (!statusOk.equals(node.path(status).asText())) { - log.error("未删除成功"); - return false; - } - return true; - } - - - public static void main(String[] args) { - File file = new File("C:\\Users\\dong\\Desktop\\遥感影像\\遥感影像\\GF1\\GF1_PMS1_E116.8_N36.6_20190528_L1A0004026837-MSS1_ORTHO_MS.tif"); - FileFastDfs fileFastDfs = new FileFastDfs(); - } -} - - diff --git a/src/main/java/com/xkrs/straw/utilsold/FileUploadUtils.java b/src/main/java/com/xkrs/straw/utilsold/FileUploadUtils.java deleted file mode 100644 index a9a6630..0000000 --- a/src/main/java/com/xkrs/straw/utilsold/FileUploadUtils.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.xkrs.straw.utilsold; - -import org.springframework.web.multipart.MultipartFile; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class FileUploadUtils { - - public static final boolean release = false; - - private FileUploadUtils() { - } - - /** - * 上传单个文件 - */ - public static String uploadFile(MultipartFile file) throws Exception { - String uploadDirPath = "/Users/liuchengqian/Desktop/DaJiang/"; - String originalFilename = file.getOriginalFilename(); - int lastPointIndex = originalFilename.lastIndexOf("."); - String extensionName = originalFilename.substring(lastPointIndex); - String newFileName = UUID.randomUUID().toString() + extensionName; - File uploadDir = new File(uploadDirPath); - if ((!uploadDir.exists()) || (!uploadDir.isDirectory())) { - uploadDir.mkdirs(); - } - String uploadFilePath = uploadDirPath + newFileName; - //实现上传 - file.transferTo(new File(uploadFilePath)); - - return uploadFilePath; - } - - /** - * 上传多个文件 - */ - public static List uploadFileArray(MultipartFile[] fileArray) throws Exception { - List pathList = new ArrayList<>(); - if (fileArray == null || fileArray.length == 0) { - return pathList; - } - for (MultipartFile file : fileArray) { - if (file != null) { - pathList.add(uploadFile(file)); - } - } - return pathList; - } -}