jiangping
2024-07-09 ddbb93dae803ea73b06bad25fb12eaa322768d18
server/service/src/main/java/com/doumee/core/utils/Constants.java
@@ -1,5 +1,6 @@
package com.doumee.core.utils;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdcardUtil;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
@@ -9,6 +10,7 @@
import com.doumee.dao.business.vo.CountCyclePriceVO;
import io.swagger.models.auth.In;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.lang3.StringUtils;
@@ -2202,7 +2204,13 @@
            InputStream inStream = null;
            OutputStream os = null;
            try {
                String tempDir = System.getProperty("java.io.tmpdir")+fileName;
                String tempDir = "C:\\hj\\jars\\temp\\";
                File f =  new File(tempDir);
                if(!f.exists() ){
                    f.mkdirs();
                }
                tempDir +=  fileName;
                System.out.println(url+"================="+tempDir);
                file = new File(tempDir);
                //下载
                urlfile = new URL(url);
@@ -2228,26 +2236,45 @@
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return file;
    }
    public static void packFilesToZip(List<File> files,    ServletOutputStream os) throws IOException {
        try (ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(os)) {
    public static void packFilesToZip(List<File> files,    OutputStream os) throws IOException {
        try {
            ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(os);
            zipOutputStream.setUseZip64(Zip64Mode.AsNeeded);
            for (File file : files) {
                if(!file.exists()){
                    System.out.println("===文件不存在======="+file.getPath());
                    continue;
                }else{
                    System.out.println("===文件存在======="+file.getPath());
                }
                ZipArchiveEntry entry = new ZipArchiveEntry(file.getName());
                zipOutputStream.putArchiveEntry(entry);
                try (FileInputStream fileInputStream = new FileInputStream(file)) {
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fileInputStream.read(buffer)) > 0) {
                        zipOutputStream.write(buffer, 0, length);
                    }
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = fileInputStream.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, length);
                }
                zipOutputStream.closeArchiveEntry();
                zipOutputStream.flush();
                IoUtil.close(fileInputStream);
                file.delete();
            }
        }catch (Exception e){
            throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR.getCode(),"对不起,下载压缩文件失败");
        }finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }