From 42c9d90ad7d4b1d88159d3c08cce6d080fb79cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E5=B5=A9?= Date: Tue, 28 Mar 2023 02:46:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91OTA=E5=A4=87=E6=A1=88?= =?UTF-8?q?=E5=B7=A5=E5=85=B7-=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ota/controller/OtaBaCxListController.java | 6 +- .../ota/service/IOtaBaCxJbxxService.java | 2 + .../ota/service/IOtaBaCxListService.java | 76 ++++---- .../ota/service/IOtaBaCxZxsjyqService.java | 2 + .../service/impl/OtaBaCxJbxxServiceImpl.java | 43 +++++ .../service/impl/OtaBaCxListServiceImpl.java | 45 ++--- .../impl/OtaBaCxZxsjyqServiceImpl.java | 5 + .../modules/ota/utils/ImportFileUtil.java | 165 +++++++++++++++++- 8 files changed, 271 insertions(+), 73 deletions(-) diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxListController.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxListController.java index 76babb51b..35cba9c8e 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxListController.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxListController.java @@ -215,7 +215,11 @@ public class OtaBaCxListController extends JeroController { OtaBaCxJbxx importData(MultipartFile file, String otaId, String cut) throws Exception; OtaBaCxJbxx parseFileList(List upLoadFiles, String otaId, String cut) throws Exception; + + void exportData(File file, String otaId, String cut) throws Exception; } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/IOtaBaCxListService.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/IOtaBaCxListService.java index 1e87985e8..2d6663673 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/IOtaBaCxListService.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/IOtaBaCxListService.java @@ -11,65 +11,65 @@ import java.util.List; /** * @Description: 车型及功能备案列表 * @Author: jero-boot - * @Date: 2023-03-17 + * @Date: 2023-03-17 * @Version: V1.0 */ public interface IOtaBaCxListService extends IService { - /** - * 保存 - * - * @param otaBaCxList - * @return - */ + /** + * 保存 + * + * @param otaBaCxList + * @return + */ void add(OtaBaCxList otaBaCxList); - /** - * 更新 - * - * @param otaBaCxList - * @return - */ + /** + * 更新 + * + * @param otaBaCxList + * @return + */ void editById(OtaBaCxList otaBaCxList); - /** - * 通过id删除 - * - * @param id - * @return - */ + /** + * 通过id删除 + * + * @param id + * @return + */ void deleteById(String id); - /** - * 批量删除 - * - * @param ids - * @return - */ + /** + * 批量删除 + * + * @param ids + * @return + */ void deleteByIds(List ids); - /** - * 通过id查询 - * - * @param id - * @return - */ + /** + * 通过id查询 + * + * @param id + * @return + */ OtaBaCxList queryById(String id); /** - * 列表查询 - */ + * 列表查询 + */ List queryList(); - /** - * 备案维护 - */ - void batchRecord(String ids, String bazt, String batjsj, String bz, String cut); + /** + * 备案维护 + */ + void batchRecord(String ids, String bazt, String batjsj, String bz, String cut); void exportTemplate(HttpServletResponse response, HttpServletRequest request); void importData(MultipartFile file, String cut) throws Exception; - void exportData(String otaId, HttpServletResponse response, HttpServletRequest request); + void exportData(String otaId, HttpServletResponse response, HttpServletRequest request) throws Exception; } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/IOtaBaCxZxsjyqService.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/IOtaBaCxZxsjyqService.java index e049a150e..1a9db6653 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/IOtaBaCxZxsjyqService.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/IOtaBaCxZxsjyqService.java @@ -72,4 +72,6 @@ public interface IOtaBaCxZxsjyqService extends IService { OtaBaCxZxsjyq importData(MultipartFile file, String otaId, String cut) throws Exception; OtaBaCxZxsjyq parseFileList(List upLoadFiles, String otaId, String cut) throws Exception; + + void exportData(File file, String otaId, String cut) throws Exception; } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/impl/OtaBaCxJbxxServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/impl/OtaBaCxJbxxServiceImpl.java index 28562bfea..282f53525 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/impl/OtaBaCxJbxxServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/impl/OtaBaCxJbxxServiceImpl.java @@ -236,5 +236,48 @@ public class OtaBaCxJbxxServiceImpl extends ServiceImpl importZip(MultipartFile file, String cut, String uploadPath) throws Exception { - List fileList = new ArrayList<>(); + public static File copyZip(MultipartFile file, String cut, String uploadPath) throws Exception { int pos = file.getOriginalFilename().lastIndexOf("."); String str = file.getOriginalFilename().substring(pos + 1).toLowerCase(); String filenameorg = file.getOriginalFilename().substring(0, pos); @@ -77,7 +83,12 @@ public class ImportFileUtil { if (str.equals("zip")) { zipEntryName = FileUnZip.unZipFiles(path + File.separator + file.getOriginalFilename(), path); } - File fileNew = new File(path); + return new File(path); + } + + public static List importZip(MultipartFile file, String cut, String uploadPath) throws Exception { + List fileList = new ArrayList<>(); + File fileNew = ImportFileUtil.copyZip(file, cut, uploadPath); for (File file1 : fileNew.listFiles()) { if (file1.getName().contains(".xls") || file1.getName().contains(".xlsx") || file1.isDirectory() || file1.getName().contains(".docx") || file1.getName().contains(".doc")) { @@ -112,7 +123,6 @@ public class ImportFileUtil { } public static Workbook readExcel(File file) { - Workbook wb = null; if (file == null) { return null; } @@ -121,9 +131,9 @@ public class ImportFileUtil { try { is = new FileInputStream(file); if (".xls".equals(extString)) { - return wb = new HSSFWorkbook(is); + return new HSSFWorkbook(is); } else if (".xlsx".equals(extString)) { - return wb = new XSSFWorkbook(is); + return new XSSFWorkbook(is); } is.close(); } catch (FileNotFoundException e) { @@ -133,7 +143,7 @@ public class ImportFileUtil { } finally { IOUtils.closeQuietly(is); } - return wb; + return null; } public static String getCellValue(Cell cell, Workbook workbook) throws Exception { @@ -202,6 +212,19 @@ public class ImportFileUtil { return null; } + public static File findFile(List file, String fileName) throws Exception { + for (File file1 : file) {// 遍历数组 + // 判断当前的File对象是否为文件夹 + if (!file1.isDirectory()) { + String name = file1.getName(); + if (name.equals(fileName)) { + return file1; + } + } + } + return null; + } + public static MultipartFile createMfileByFile(File file) { MultipartFile mFile = null; try { @@ -253,4 +276,130 @@ public class ImportFileUtil { return res; } + private static final int BUFFER_SIZE = 2 * 1024; + + /** + * 压缩成ZIP 方法1 + * + * @param srcDir 压缩文件夹路径 + * @param out 压缩文件输出流 + * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; + * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) + * @throws RuntimeException 压缩失败会抛出运行时异常 + */ + public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) + throws RuntimeException { + + long start = System.currentTimeMillis(); + ZipOutputStream zos = null; + try { + zos = new ZipOutputStream(out); + File sourceFile = new File(srcDir); + compress(sourceFile, zos, sourceFile.getName(), KeepDirStructure); + long end = System.currentTimeMillis(); + System.out.println("压缩完成,耗时:" + (end - start) + " ms"); + } catch (Exception e) { + throw new RuntimeException("zip error from ZipUtils", e); + } finally { + if (zos != null) { + try { + zos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + } + + /** + * 压缩成ZIP 方法2 + * + * @param srcFiles 需要压缩的文件列表 + * @param out 压缩文件输出流 + * @throws RuntimeException 压缩失败会抛出运行时异常 + */ + public static void toZip(List srcFiles, OutputStream out) throws RuntimeException { + long start = System.currentTimeMillis(); + ZipOutputStream zos = null; + try { + zos = new ZipOutputStream(out); + for (File srcFile : srcFiles) { + byte[] buf = new byte[BUFFER_SIZE]; + zos.putNextEntry(new ZipEntry(srcFile.getName())); + int len; + FileInputStream in = new FileInputStream(srcFile); + while ((len = in.read(buf)) != -1) { + zos.write(buf, 0, len); + } + zos.closeEntry(); + in.close(); + } + long end = System.currentTimeMillis(); + System.out.println("压缩完成,耗时:" + (end - start) + " ms"); + } catch (Exception e) { + throw new RuntimeException("zip error from ZipUtils", e); + } finally { + if (zos != null) { + try { + zos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * 递归压缩方法 + * + * @param sourceFile 源文件 + * @param zos zip输出流 + * @param name 压缩后的名称 + * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构; + * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败) + * @throws Exception + */ + private static void compress(File sourceFile, ZipOutputStream zos, String name, + boolean KeepDirStructure) throws Exception { + byte[] buf = new byte[BUFFER_SIZE]; + if (sourceFile.isFile()) { + // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字 + zos.putNextEntry(new ZipEntry(name)); + // copy文件到zip输出流中 + int len; + FileInputStream in = new FileInputStream(sourceFile); + while ((len = in.read(buf)) != -1) { + zos.write(buf, 0, len); + } + // Complete the entry + zos.closeEntry(); + in.close(); + } else { + File[] listFiles = sourceFile.listFiles(); + if (listFiles == null || listFiles.length == 0) { + // 需要保留原来的文件结构时,需要对空文件夹进行处理 + if (KeepDirStructure) { + // 空文件夹的处理 + zos.putNextEntry(new ZipEntry(name + "/")); + // 没有文件,不需要文件的copy + zos.closeEntry(); + } + + } else { + for (File file : listFiles) { + // 判断是否需要保留原来的文件结构 + if (KeepDirStructure) { + // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠, + // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了 + compress(file, zos, name + "/" + file.getName(), KeepDirStructure); + } else { + compress(file, zos, file.getName(), KeepDirStructure); + } + + } + } + } + } } +