tangxiaobao
2021-08-27 81befb9a1f22c0d3f15a496f06ce26710bac9638
下载报告增加压缩处理、修改bug
4个文件已修改
258 ■■■■ 已修改文件
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/FileUtil.java 84 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
springcloud_k8s_panzhihuazhihuishequ/community_backstage/src/main/java/com/panzhihua/community_backstage/api/ComSwPatrolRecordApi.java 170 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/model/dos/ComSwDangerReportDO.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComSwPatrolRecordServiceImpl.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/FileUtil.java
@@ -1,7 +1,8 @@
package com.panzhihua.common.utlis;
import java.io.File;
import java.io.InputStream;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtil {
@@ -32,4 +33,83 @@
    public static File readUserHomeFile(String pathName) {
        return new File(System.getProperty("user.home") + File.separator + pathName);
    }
    /**
     * 压缩文件
     *
     * @param sourceFilePath 源文件路径
     * @param zipFilePath    压缩后文件存储路径
     * @param zipFilename    压缩文件名
     */
    public static void compressToZip(String sourceFilePath, String zipFilePath, String zipFilename) {
        File sourceFile = new File(sourceFilePath);
        File zipPath = new File(zipFilePath);
        if (!zipPath.exists()) {
            zipPath.mkdirs();
        }
        File zipFile = new File(zipPath + File.separator + zipFilename);
        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            writeZip(sourceFile, "", zos);
            //文件压缩完成后,删除被压缩文件
            boolean flag = deleteDir(sourceFile);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e.getCause());
        }
    }
    /**
     * 遍历所有文件,压缩
     *
     * @param file       源文件目录
     * @param parentPath 压缩文件目录
     * @param zos        文件流
     */
    private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
        if (file.isDirectory()) {
            //目录
            parentPath += file.getName() + File.separator;
            File[] files = file.listFiles();
            for (File f : files) {
                writeZip(f, parentPath, zos);
            }
        } else {
            //文件
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
                //指定zip文件夹
                ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
                zos.putNextEntry(zipEntry);
                int len;
                byte[] buffer = new byte[1024 * 10];
                while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
                    zos.write(buffer, 0, len);
                    zos.flush();
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(), e.getCause());
            }
        }
        /**
         * 删除文件夹
         *
         * @param dir
         * @return
         */
    }
    private static boolean deleteDir (File dir){
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        //删除空文件夹
        return dir.delete();
    }
}
springcloud_k8s_panzhihuazhihuishequ/community_backstage/src/main/java/com/panzhihua/community_backstage/api/ComSwPatrolRecordApi.java
@@ -10,6 +10,9 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.jcraft.jsch.SftpException;
import com.panzhihua.common.utlis.*;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -31,9 +34,6 @@
import com.panzhihua.common.model.vos.community.*;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.service.partybuilding.PartyBuildingService;
import com.panzhihua.common.utlis.HttpUtils;
import com.panzhihua.common.utlis.SFTPUtil;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.community_backstage.excel.CustomSheetWriteHandler;
import freemarker.template.Configuration;
@@ -480,26 +480,31 @@
    @PostMapping("/export/safetyWorkRecord")
    public R exportSafetyWorkRecord(@RequestBody List<Long> ids) {
        Long communityId = this.getLoginUserInfo().getCommunityId();
        List<String> downLoadUrl = new ArrayList<>();
        try {
            for (Long id : ids) {
                SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
                sftp.login();
        String property = System.getProperty("user.dir");
        String sourceFile =property+File.separator+"word"+File.separator+System.currentTimeMillis()+File.separator;
        String zipFile=property+File.separator+"zip"+File.separator;
        String ftpUrl = "/mnt/data/web/excel/";
        for (Long id : ids) {
            try {
                Map<String, Object> dataMap = new HashMap<String, Object>();
                ComSwSafetyWorkRecordVO comSwSafetyWorkRecordVO = JSONObject.parseObject(
                    JSONObject.toJSONString(communityService.detailSafetyWorkRecord(id, communityId).getData()),
                    ComSwSafetyWorkRecordVO.class);
                // 生成动态模板excel通过ftp工具上传到主节点,然后返回模板下载地址
                String ftpUrl = "/mnt/data/web/excel/";
                String name = "安全工作记录_" + comSwSafetyWorkRecordVO.getId() + ".doc";
                boolean existDir = sftp.isExistDir(ftpUrl + name);
                if (!existDir) {
                    String property = System.getProperty("user.dir");
                    String fileName = property + File.separator + name;
                    // 这里 需要指定写用哪个class去写
                    ExcelWriter excelWriter = null;
                    InputStream inputStream = null;
                    File file0=new File("d:/safetyWork/file");
                    if(!file0.isDirectory()&&!file0.exists()){
                        file0.mkdirs();
                    }
                    file0 = new File(sourceFile);
                    if(!file0.isDirectory()&&!file0.exists()){
                        file0.mkdirs();
                    }
@@ -701,7 +706,7 @@
                        // 输出文档路径及名称
                        // File outFile = new File("mnt/data/web/excel/安全工作记录_" + comSwSafetyWorkRecordVO.getId() +
                        // ".doc");
                        File file = new File(fileName);
                        File file = new File(sourceFile + name);
                        // 以utf-8的编码读取ftl文件
                        Template template = configuration.getTemplate("安全工作记录.ftl", "utf-8");
@@ -709,13 +714,13 @@
                            new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), 10240);
                        template.process(dataMap, out);
                        out.close();
                        inputStream = new FileInputStream(file);
                        sftp.uploadMore(ftpUrl, name, inputStream);
                        sftp.logout();
                        inputStream.close();
                        String absolutePath = file.getAbsolutePath();
                        boolean delete = file.delete();
                        log.info("删除excel【{}】结果【{}】", absolutePath, delete);
                        //                        inputStream = new FileInputStream(file);
//                        sftp.uploadMore(ftpUrl, name, inputStream);
//                        sftp.logout();
//                        inputStream.close();
//                        String absolutePath = file.getAbsolutePath();
//                        boolean delete = file.delete();
//                        log.info("删除excel【{}】结果【{}】", absolutePath, delete);
                    } finally {
                        // 千万别忘记finish 会帮忙关闭流
                        if (inputStream != null) {
@@ -723,41 +728,60 @@
                        }
                    }
                }
                downLoadUrl.add(excelUrl + name);
            }
            return R.ok(downLoadUrl);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文件传输失败【{}】", e.getMessage());
        }
        return R.fail();
        }
        FileUtil.compressToZip(sourceFile,zipFile,"安全工作记录.zip");
        String currentDateString = String.valueOf(System.currentTimeMillis());
        String name = "安全工作记录_"+ currentDateString+".zip";
        try {
            InputStream inputStream=new FileInputStream(zipFile+"安全工作记录.zip");
            sftp.uploadMore(ftpUrl, name , inputStream);
            sftp.logout();
            inputStream.close();
            FileUtils.deleteDirectory(new File(property+File.separator+"word"+File.separator));
            FileUtils.deleteDirectory(new File(zipFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok(excelUrl + name);
    }
    @ApiOperation(value = "下载巡查记录报告")
    @PostMapping("/export/patrolRecord")
    public R exportPatrolRecord(@RequestBody List<Long> ids) {
        Long communityId = this.getLoginUserInfo().getCommunityId();
        List<String> downLoadUrl = new ArrayList<>();
        try {
            for (Long id : ids) {
                SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
                sftp.login();
        String property = System.getProperty("user.dir");
        String sourceFile =property+File.separator+"word"+File.separator+System.currentTimeMillis()+File.separator;
        String zipFile=property+File.separator+"zip"+File.separator;
        String ftpUrl = "/mnt/data/web/excel/";
        for (Long id : ids) {
            try {
                Map<String, Object> dataMap = new HashMap<String, Object>();
                ComSwPatrolRecordVO comSwPatrolRecordVO =
                    JSONObject.parseObject(JSONObject.toJSONString(communityService.detailPatrolRecord(id).getData()),
                        ComSwPatrolRecordVO.class);
                // 生成动态模板excel通过ftp工具上传到主节点,然后返回模板下载地址
                String ftpUrl = "/mnt/data/web/excel/";
                String name = "巡查记录_" + comSwPatrolRecordVO.getId() + ".doc";
                boolean existDir = sftp.isExistDir(ftpUrl + name);
                if (!existDir) {
                    String property = System.getProperty("user.dir");
                    String fileName = property + File.separator + name;
                    // 这里 需要指定写用哪个class去写
                    ExcelWriter excelWriter = null;
                    InputStream inputStream = null;
                    File file0=new File("d:/patrolRecord/file");
                    if(!file0.isDirectory()&&!file0.exists()){
                        file0.mkdirs();
                    }
                    file0 = new File(sourceFile);
                    if(!file0.isDirectory()&&!file0.exists()){
                        file0.mkdirs();
                    }
@@ -970,7 +994,7 @@
                        // 输出文档路径及名称
                        // File outFile = new File("mnt/data/web/excel/安全工作记录_" + comSwSafetyWorkRecordVO.getId() +
                        // ".doc");
                        File file = new File(fileName);
                        File file = new File(sourceFile + name);
                        // 以utf-8的编码读取ftl文件
                        Template template = configuration.getTemplate("巡查记录.ftl", "utf-8");
@@ -978,13 +1002,13 @@
                            new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), 10240);
                        template.process(dataMap, out);
                        out.close();
                        inputStream = new FileInputStream(file);
                        sftp.uploadMore(ftpUrl, name, inputStream);
                        sftp.logout();
                        inputStream.close();
                        String absolutePath = file.getAbsolutePath();
                        boolean delete = file.delete();
                        log.info("删除excel【{}】结果【{}】", absolutePath, delete);
//                        inputStream = new FileInputStream(file);
//                        sftp.uploadMore(ftpUrl, name, inputStream);
//                        sftp.logout();
//                        inputStream.close();
//                        String absolutePath = file.getAbsolutePath();
//                        boolean delete = file.delete();
//                        log.info("删除excel【{}】结果【{}】", absolutePath, delete);
                    } finally {
                        // 千万别忘记finish 会帮忙关闭流
                        if (inputStream != null) {
@@ -992,41 +1016,60 @@
                        }
                    }
                }
                downLoadUrl.add(excelUrl + name);
            }
            return R.ok(downLoadUrl);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文件传输失败【{}】", e.getMessage());
        }
        return R.fail();
        }
        FileUtil.compressToZip(sourceFile,zipFile,"巡查记录.zip");
        String currentDateString = String.valueOf(System.currentTimeMillis());
        String name = "巡查记录_"+ currentDateString+".zip";
        try {
            InputStream inputStream=new FileInputStream(zipFile+"巡查记录.zip");
            sftp.uploadMore(ftpUrl, name , inputStream);
            sftp.logout();
            inputStream.close();
            FileUtils.deleteDirectory(new File(property+File.separator+"word"+File.separator));
            FileUtils.deleteDirectory(new File(zipFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok(excelUrl + name);
    }
    @ApiOperation(value = "下载隐患报告")
    @PostMapping("/export/DangerReport")
    public R exportDangerReport(@RequestBody List<Long> ids) {
        Long communityId = this.getLoginUserInfo().getCommunityId();
        List<String> downLoadUrl = new ArrayList<>();
        try {
            for (Long id : ids) {
                SFTPUtil sftp = new SFTPUtil(userName, password, host, port);
                sftp.login();
        String property = System.getProperty("user.dir");
        String sourceFile =property+File.separator+"word"+File.separator+System.currentTimeMillis()+File.separator;
        String zipFile=property+File.separator+"zip"+File.separator;
        String ftpUrl = "/mnt/data/web/excel/";
        for (Long id : ids) {
            try {
                Map<String, Object> dataMap = new HashMap<String, Object>();
                ComSwDangerReportVO comSwDangerReportVO =
                    JSONObject.parseObject(JSONObject.toJSONString(communityService.detailDangerReport(id).getData()),
                        ComSwDangerReportVO.class);
                // 生成动态模板excel通过ftp工具上传到主节点,然后返回模板下载地址
                String ftpUrl = "/mnt/data/web/excel/";
                String name = "隐患报告_" + comSwDangerReportVO.getId() + ".doc";
                boolean existDir = sftp.isExistDir(ftpUrl + name);
                if (!existDir) {
                    String property = System.getProperty("user.dir");
                    String fileName = property + File.separator + name;
                    // 这里 需要指定写用哪个class去写
                    ExcelWriter excelWriter = null;
                    InputStream inputStream = null;
                    File file0=new File("d:/dangerReport/file");
                if(!file0.isDirectory()&&!file0.exists()){
                    file0.mkdirs();
                }
                file0 = new File(sourceFile);
                    if(!file0.isDirectory()&&!file0.exists()){
                        file0.mkdirs();
                    }
@@ -1465,7 +1508,7 @@
                        // 输出文档路径及名称
                        // File outFile = new File("mnt/data/web/excel/安全工作记录_" + comSwSafetyWorkRecordVO.getId() +
                        // ".doc");
                        File file = new File(fileName);
                    File file = new File(sourceFile+name);
                        // 以utf-8的编码读取ftl文件
                        Template template = configuration.getTemplate("隐患报告.ftl", "utf-8");
@@ -1473,13 +1516,13 @@
                            new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), 10240);
                        template.process(dataMap, out);
                        out.close();
                        inputStream = new FileInputStream(file);
                        sftp.uploadMore(ftpUrl, name, inputStream);
                        sftp.logout();
                        inputStream.close();
                        String absolutePath = file.getAbsolutePath();
                        boolean delete = file.delete();
                        log.info("删除excel【{}】结果【{}】", absolutePath, delete);
//                    inputStream = new FileInputStream(file);
//                    sftp.uploadMore(ftpUrl, name, inputStream);
//                    sftp.logout();
//                    inputStream.close();
//                        String absolutePath = file.getAbsolutePath();
//                        boolean delete = file.delete();
//                        log.info("删除excel【{}】结果【{}】", absolutePath, delete);
                    } finally {
                        // 千万别忘记finish 会帮忙关闭流
                        if (inputStream != null) {
@@ -1487,14 +1530,29 @@
                        }
                    }
                }
                downLoadUrl.add(excelUrl + name);
            }
            return R.ok(downLoadUrl);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文件传输失败【{}】", e.getMessage());
        }
        return R.fail();
        }
        FileUtil.compressToZip(sourceFile,zipFile,"隐患报告.zip");
        String currentDateString = String.valueOf(System.currentTimeMillis());
        String name = "隐患报告_"+ currentDateString+".zip";
        try {
            InputStream inputStream=new FileInputStream(zipFile+"隐患报告.zip");
            sftp.uploadMore(ftpUrl, name , inputStream);
            sftp.logout();
            inputStream.close();
            FileUtils.deleteDirectory(new File(property+File.separator+"word"+File.separator));
            FileUtils.deleteDirectory(new File(zipFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok(excelUrl + name);
    }
    private List<List<String>> headDataFilling() {
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/model/dos/ComSwDangerReportDO.java
@@ -22,7 +22,7 @@
    /**
     * 主键id
     */
    @TableId(type = IdType.INPUT)
    @TableId(value = "id",type = IdType.INPUT)
    private Long id;
    /**
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComSwPatrolRecordServiceImpl.java
@@ -93,7 +93,7 @@
        BeanUtils.copyProperties(comSwPatrolRecordAddDTO, comSwPatrolRecordDO);
        Long patrolRecordId = Snowflake.getId();
        comSwPatrolRecordDO.setId(patrolRecordId);
        comSwPatrolRecordDO.setPatrolTime(new Date());
//        comSwPatrolRecordDO.setPatrolTime(new Date());
        comSwPatrolRecordDO.setSuccessionTime(
            DateUtils.stringToDate(comSwPatrolRecordAddDTO.getSuccessionTime(), DateUtils.yyyyMMdd_format));
        StringBuilder nameString = new StringBuilder();