java执行linux命令的形式导出图片压缩包(后台导出,单线程运行)

 @GetMapping("exportTask")
    public @ResponseBody
    RestResult exportTask(String taskId) {
        Map<String, Object> hashMap = new HashMap<>();
        Task task = taskService.getById(taskId);
        //TODO 需要在这里知道最后用户的上传时间,如果最后时间小于生成时间,直接返回下载连接
        String time = taskUserService.getMaxCdt(taskId);
        Integer filestatus = task.getFilestatus();
        if (filestatus == null) {
            filestatus = 0;
        }
        //TODO 判断是有有正在生成的任务 状态有 0未生成 1进行中 2完成 3失败
        if (filestatus == 1) {
            return RestResult.Fail("任务正在生成,请稍后在试");
        }
        //TODO 判断文件已经生成并且为最新zip
        if (filestatus == 2 && StringUtils.isNotBlank(task.getFileurl()) && StringUtils.isNotBlank(task.getUpdatetime())) {
            //最后一次提交时间小于文件生成时间
            if (StringUtils.isNotBlank(task.getUpdatetime()) && DateUtil.compareTime(time, task.getUpdatetime())) {
                hashMap.put("url", task.getFileurl());
                return RestResult.OK(hashMap);
            }
        }
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                task.setFilestatus(1);
                taskService.updateById(task);
                String zipPath = UpLoadUtil.getPath() + "/task2021" + UpLoadUtil.getTIMEPath() + "/zip/" + taskId + "/";
                File file = new File(zipPath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                String savePath = "/task2021" + UpLoadUtil.getTIMEPath() + "/zip/" + taskId + "/";
                //TODO 删除历史文件和压缩包
                if (StringUtils.isNotBlank(task.getFileurl())) {
                    try {
                        String command = "cd " + zipPath + " && rm -rf " + task.getFileurl();
                        System.out.println(command);
                        CommandUtil.exec(command);
                    } catch (Exception e) {
                        task.setFilestatus(3);
                        taskService.updateById(task);
                        e.printStackTrace();
                    }
                }
                //TODO 在数据库中查询所要导出的数据
                Map<String, List<TaskAnswer>> map = checkExportFiles(taskId);
                if (map.size() > 0) {
                    //生成要导出的文件夹
                    answerUserService.moveCopyFile(zipPath, map);
                    String fileName = task.getTitle() + ".zip";
                    System.out.println("文件名称 = " + fileName);
                    String command = "cd " + zipPath + " && zip -q -r " + fileName + " *";
                    System.out.println(command);
                    //TODO linux压缩命令执行
                    try {
                        CommandUtil.exec(command);
                        String url = savePath + fileName;
                        System.out.println("拼接文件下载链接 url=" + url);
                        if (StringUtils.isNotBlank(url)) {
                            task.setFileurl(url);
                            task.setUpdatetime(DateUtil.now());
                            task.setFilestatus(2);
                            taskService.updateById(task);
                        }
                    } catch (Exception e) {
                        task.setFilestatus(3);
                        taskService.updateById(task);
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
        return RestResult.Fail("任务正在生成,请稍后在试");
    }
public class CommandUtil {

    public static void main(String[] args) {
//        try {
//            run("cd /Users/lyh/Documents/my/test/test && zip -q -r test2021.zip * & ");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        exec("cd /Users/lyh/Documents/my/test/test && zip -q -r test202111.zip * ");
    }

    public static Object exec(String cmd) {
        try {
            String[] cmdA = {"/bin/sh", "-c", cmd};
            Process process = Runtime.getRuntime().exec(cmdA);
            LineNumberReader br = new LineNumberReader(new InputStreamReader(
                    process.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                sb.append(line).append("\n");
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值