package com.ydtech.modules.order.components; import com.ydtech.exception.SystemException; import com.ydtech.utils.baidu.FileUtil; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.ArrayList; import java.util.List; @Component public class FileComponents { @Value("${upload.file.path}") private String uploadFilePath; @Value("${upload.file.url}") private String showFileUrl; @Value("${api.url}") private String apiUrl; public String gainCarInsPolicyNew(String url, String fileName, String subOrderId, String password,boolean flag) { try { String dir = uploadFilePath + "carInsPolicyNew/" + subOrderId + "/"; String showPath = apiUrl + showFileUrl + "carInsPolicyNew/" + subOrderId + "/" + fileName; File existFile = new File(dir + fileName); if (existFile.exists()) { if(!flag){ return showPath; }else{ //刪除重新生成 existFile.delete(); } } File folder = new File(dir); if (!folder.isDirectory()) { folder.mkdirs(); } unzipFile(url, dir, password + ".zip", password, fileName); return showPath; } catch (Exception e) { e.printStackTrace(); throw new SystemException("文件异常"); } } public String gainCarInsPolicy(String url, String fileName, String licensePlate, String password) { try { String dir = uploadFilePath + "carInsPolicy/" + licensePlate + "/"; String showPath = apiUrl + showFileUrl + "carInsPolicy/" + licensePlate + "/" + fileName; File existFile = new File(dir + fileName); if (existFile.exists()) { return showPath; } File folder = new File(dir); if (!folder.isDirectory()) { folder.mkdirs(); } unzipFile(url, dir, password + ".zip", password, fileName); return showPath; } catch (Exception e) { e.printStackTrace(); throw new SystemException("文件异常"); } } public String gainCarInsPolicyNew(MultipartFile file, String subOrderId, boolean flag) { try { String dir = uploadFilePath + "carInsPolicyNew/" + subOrderId + "/"; String showPath = apiUrl + showFileUrl + "carInsPolicyNew/" + subOrderId + "/" + file.getOriginalFilename(); File existFile = new File(dir + file.getOriginalFilename()); if (existFile.exists()) { if (!flag) { return showPath; // 已存在且不要求覆盖 } else { existFile.delete(); // 删除旧文件 } } File folder = new File(dir); if (!folder.isDirectory()) { folder.mkdirs(); } // 将上传的文件保存到本地 File destFile = new File(dir + file.getOriginalFilename()); file.transferTo(destFile); return showPath; } catch (Exception e) { e.printStackTrace(); throw new SystemException("文件上传异常"); } } // public static boolean uploadCarInsPolicyNew(MultipartFile file, String fileName, String subOrderId, boolean flag) { // FileComponents fileComponents = new FileComponents(); // String policyPath = fileComponents.gainCarInsPolicyNew(file, fileName, subOrderId, flag); // return true; // } public static void unzipFile(String url, String zipPath, String zipName, String password, String newFileName) throws ZipException, IOException { String path = zipPath + zipName; URI u = URI.create(url); InputStream inputStream = u.toURL().openStream(); File file = new File(path); FileUtil.copyInputStreamToFile(inputStream, file); ZipFile zipFile = new ZipFile(path); zipFile.setPassword(password); String s = zipPath + password + "/"; zipFile.extractAll(s); String fileName = getFileNames(s).get(0); File oldFile = new File(s + fileName); File newFile = new File(zipPath, newFileName); oldFile.renameTo(newFile); } /** * 得到文件名称 */ public static List getFileNames(String path) { File file = new File(path); if (!file.exists()) { return null; } List fileNames = new ArrayList<>(); return getFileNames(file, fileNames); } /** * 得到文件名称 * * @param file 文件 * @param fileNames 文件名 * @return {@link List}<{@link String}> */ public static List getFileNames(File file, List fileNames) { File[] files = file.listFiles(); for (File f : files) { if (f.isDirectory()) { getFileNames(f, fileNames); } else { fileNames.add(f.getName()); } } return fileNames; } }