Search in sources :

Example 1 with FileWriter

use of cn.hutool.core.io.file.FileWriter in project onex-boot by zhangchaoxu.

the class LocalOssService method upload.

@Override
public String upload(String prefix, InputStream inputStream, String fileName) {
    String prefixTotal = StrUtil.isNotEmpty(config.getPrefix()) ? config.getPrefix() : "";
    if (StrUtil.isNotEmpty(prefix)) {
        if (StrUtil.isNotEmpty(prefixTotal)) {
            prefixTotal += "/" + prefix;
        } else {
            prefixTotal = prefix;
        }
    }
    String objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), false);
    File localFile = new File(config.getLocalPath() + File.separator + objectKey);
    if (localFile.exists()) {
        // 文件已存在,则需要对文件重命名
        objectKey = buildUploadPath(prefixTotal, fileName, config.getKeepFileName(), true);
    }
    new FileWriter(localFile).writeFromStream(inputStream, true);
    // FileUtil.copy(inputStream, localFile, true);
    return config.getDomain() + objectKey;
}
Also used : FileWriter(cn.hutool.core.io.file.FileWriter) File(java.io.File)

Example 2 with FileWriter

use of cn.hutool.core.io.file.FileWriter in project my-feed-OPML by superleeyom.

the class OpmlUtils method importOpml.

private static File importOpml(String opmlText) {
    log.info("===========================开始创建opml===========================");
    File opmlFile = new File("feed.opml");
    FileWriter opmlWriter = new FileWriter(opmlFile);
    opmlWriter.write(opmlText);
    log.info("===========================opml创建成功===========================");
    return opmlFile;
}
Also used : FileWriter(cn.hutool.core.io.file.FileWriter) File(java.io.File)

Example 3 with FileWriter

use of cn.hutool.core.io.file.FileWriter in project HOJ by HimitZH.

the class ContestFileController method downloadContestPrintText.

@GetMapping("/download-contest-print-text")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
public void downloadContestPrintText(@RequestParam("id") Long id, HttpServletResponse response) {
    ContestPrint contestPrint = contestPrintService.getById(id);
    String filename = contestPrint.getUsername() + "_Contest_Print.txt";
    String filePath = Constants.File.CONTEST_TEXT_PRINT_FOLDER.getPath() + File.separator + id + File.separator + filename;
    if (!FileUtil.exist(filePath)) {
        FileWriter fileWriter = new FileWriter(filePath);
        fileWriter.write(contestPrint.getContent());
    }
    FileReader zipFileReader = new FileReader(filePath);
    // 放到缓冲流里面
    BufferedInputStream bins = new BufferedInputStream(zipFileReader.getInputStream());
    // 获取文件输出IO流
    OutputStream outs = null;
    BufferedOutputStream bouts = null;
    try {
        outs = response.getOutputStream();
        bouts = new BufferedOutputStream(outs);
        response.setContentType("application/x-download");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        int bytesRead = 0;
        byte[] buffer = new byte[1024 * 10];
        // 开始向网络传输文件流
        while ((bytesRead = bins.read(buffer, 0, 1024 * 10)) != -1) {
            bouts.write(buffer, 0, bytesRead);
        }
        // 刷新缓存
        bouts.flush();
    } catch (IOException e) {
        log.error("下载比赛打印文本文件异常------------>", e);
        response.reset();
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        Map<String, Object> map = new HashMap<>();
        map.put("status", CommonResult.STATUS_ERROR);
        map.put("msg", "下载文件失败,请重新尝试!");
        map.put("data", null);
        try {
            response.getWriter().println(JSONUtil.toJsonStr(map));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    } finally {
        try {
            bins.close();
            if (outs != null) {
                outs.close();
            }
            if (bouts != null) {
                bouts.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) FileWriter(cn.hutool.core.io.file.FileWriter) FileReader(cn.hutool.core.io.file.FileReader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ContestPrint(top.hcode.hoj.pojo.entity.contest.ContestPrint) GetMapping(org.springframework.web.bind.annotation.GetMapping) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Example 4 with FileWriter

use of cn.hutool.core.io.file.FileWriter in project HOJ by HimitZH.

the class TestCaseController method uploadTestcaseZip.

@PostMapping("/upload-testcase-zip")
@ResponseBody
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
public CommonResult uploadTestcaseZip(@RequestParam("file") MultipartFile file) {
    // 获取文件后缀
    String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
    if (!"zip".toUpperCase().contains(suffix.toUpperCase())) {
        return CommonResult.errorResponse("请上传zip格式的测试数据压缩包!");
    }
    String fileDirId = IdUtil.simpleUUID();
    String fileDir = Constants.File.TESTCASE_TMP_FOLDER.getPath() + File.separator + fileDirId;
    String filePath = fileDir + File.separator + file.getOriginalFilename();
    // 文件夹不存在就新建
    FileUtil.mkdir(fileDir);
    try {
        file.transferTo(new File(filePath));
    } catch (IOException e) {
        log.error("评测数据文件上传异常-------------->{}", e.getMessage());
        return CommonResult.errorResponse("服务器异常:评测数据上传失败!", CommonResult.STATUS_ERROR);
    }
    // 将压缩包压缩到指定文件夹
    ZipUtil.unzip(filePath, fileDir);
    // 删除zip文件
    FileUtil.del(filePath);
    // 检查文件是否存在
    File testCaseFileList = new File(fileDir);
    File[] files = testCaseFileList.listFiles();
    if (files == null || files.length == 0) {
        FileUtil.del(fileDir);
        return CommonResult.errorResponse("评测数据压缩包里文件不能为空!");
    }
    HashMap<String, String> inputData = new HashMap<>();
    HashMap<String, String> outputData = new HashMap<>();
    // 遍历读取与检查是否in和out文件一一对应,否则报错
    for (File tmp : files) {
        String tmpPreName = null;
        if (tmp.getName().endsWith(".in")) {
            tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf(".in"));
            inputData.put(tmpPreName, tmp.getName());
        } else if (tmp.getName().endsWith(".out")) {
            tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf(".out"));
            outputData.put(tmpPreName, tmp.getName());
        } else if (tmp.getName().endsWith(".ans")) {
            tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf(".ans"));
            outputData.put(tmpPreName, tmp.getName());
        } else if (tmp.getName().endsWith(".txt")) {
            tmpPreName = tmp.getName().substring(0, tmp.getName().lastIndexOf(".txt"));
            if (tmpPreName.contains("input")) {
                inputData.put(tmpPreName.replaceAll("input", "$*$"), tmp.getName());
            } else if (tmpPreName.contains("output")) {
                outputData.put(tmpPreName.replaceAll("output", "$*$"), tmp.getName());
            }
        }
    }
    // 进行数据对应检查,同时生成返回数据
    List<HashMap<String, String>> problemCaseList = new LinkedList<>();
    for (String key : inputData.keySet()) {
        HashMap<String, String> testcaseMap = new HashMap<>();
        String inputFileName = inputData.get(key);
        testcaseMap.put("input", inputFileName);
        String outputFileName = key + ".out";
        if (inputFileName.endsWith(".txt")) {
            outputFileName = inputFileName.replaceAll("input", "output");
        }
        // 若有名字对应的out文件不存在的,直接生成对应的out文件
        if (outputData.getOrDefault(key, null) == null) {
            FileWriter fileWriter = new FileWriter(fileDir + File.separator + outputFileName);
            fileWriter.write("");
        }
        testcaseMap.put("output", outputFileName);
        problemCaseList.add(testcaseMap);
    }
    List<HashMap<String, String>> fileList = problemCaseList.stream().sorted((o1, o2) -> {
        String a = o1.get("input").split("\\.")[0];
        String b = o2.get("input").split("\\.")[0];
        if (a.length() > b.length()) {
            return 1;
        } else if (a.length() < b.length()) {
            return -1;
        }
        return a.compareTo(b);
    }).collect(Collectors.toList());
    return CommonResult.successResponse(MapUtil.builder().put("fileList", fileList).put("fileListDir", fileDir).map(), "上传测试数据成功!");
}
Also used : java.util(java.util) MapUtil(cn.hutool.core.map.MapUtil) IdUtil(cn.hutool.core.util.IdUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) Controller(org.springframework.stereotype.Controller) ZipUtil(cn.hutool.core.util.ZipUtil) CommonResult(top.hcode.hoj.common.result.CommonResult) JSONUtil(cn.hutool.json.JSONUtil) ProblemCase(top.hcode.hoj.pojo.entity.problem.ProblemCase) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) FileReader(cn.hutool.core.io.file.FileReader) HttpServletResponse(javax.servlet.http.HttpServletResponse) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) URLEncoder(java.net.URLEncoder) Logical(org.apache.shiro.authz.annotation.Logical) java.io(java.io) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) FileWriter(cn.hutool.core.io.file.FileWriter) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) MultipartFile(org.springframework.web.multipart.MultipartFile) FileUtil(cn.hutool.core.io.FileUtil) Constants(top.hcode.hoj.utils.Constants) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Assert(org.springframework.util.Assert) ProblemCaseServiceImpl(top.hcode.hoj.service.problem.impl.ProblemCaseServiceImpl) FileWriter(cn.hutool.core.io.file.FileWriter) MultipartFile(org.springframework.web.multipart.MultipartFile) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Example 5 with FileWriter

use of cn.hutool.core.io.file.FileWriter in project HOJ by HimitZH.

the class TestCaseController method downloadTestcase.

@GetMapping("/download-testcase")
@RequiresAuthentication
@RequiresRoles(value = { "root", "problem_admin" }, logical = Logical.OR)
public void downloadTestcase(@RequestParam("pid") Long pid, HttpServletResponse response) {
    String workDir = Constants.File.TESTCASE_BASE_FOLDER.getPath() + File.separator + "problem_" + pid;
    File file = new File(workDir);
    if (!file.exists()) {
        // 本地为空 尝试去数据库查找
        QueryWrapper<ProblemCase> problemCaseQueryWrapper = new QueryWrapper<>();
        problemCaseQueryWrapper.eq("pid", pid);
        List<ProblemCase> problemCaseList = problemCaseService.list(problemCaseQueryWrapper);
        Assert.notEmpty(problemCaseList, "对不起,该题目的评测数据为空!");
        boolean hasTestCase = true;
        if (problemCaseList.get(0).getInput().endsWith(".in") && (problemCaseList.get(0).getOutput().endsWith(".out") || problemCaseList.get(0).getOutput().endsWith(".ans"))) {
            hasTestCase = false;
        }
        Assert.isTrue(hasTestCase, "对不起,该题目的评测数据为空!");
        FileUtil.mkdir(workDir);
        // 写入本地
        for (int i = 0; i < problemCaseList.size(); i++) {
            String filePreName = workDir + File.separator + (i + 1);
            String inputName = filePreName + ".in";
            String outputName = filePreName + ".out";
            FileWriter infileWriter = new FileWriter(inputName);
            infileWriter.write(problemCaseList.get(i).getInput());
            FileWriter outfileWriter = new FileWriter(outputName);
            outfileWriter.write(problemCaseList.get(i).getOutput());
        }
    }
    String fileName = "problem_" + pid + "_testcase_" + System.currentTimeMillis() + ".zip";
    // 将对应文件夹的文件压缩成zip
    ZipUtil.zip(workDir, Constants.File.FILE_DOWNLOAD_TMP_FOLDER.getPath() + File.separator + fileName);
    // 将zip变成io流返回给前端
    FileReader fileReader = new FileReader(Constants.File.FILE_DOWNLOAD_TMP_FOLDER.getPath() + File.separator + fileName);
    // 放到缓冲流里面
    BufferedInputStream bins = new BufferedInputStream(fileReader.getInputStream());
    // 获取文件输出IO流
    OutputStream outs = null;
    BufferedOutputStream bouts = null;
    try {
        outs = response.getOutputStream();
        bouts = new BufferedOutputStream(outs);
        response.setContentType("application/x-download");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        int bytesRead = 0;
        byte[] buffer = new byte[1024 * 10];
        // 开始向网络传输文件流
        while ((bytesRead = bins.read(buffer, 0, 1024 * 10)) != -1) {
            bouts.write(buffer, 0, bytesRead);
        }
        bouts.flush();
    } catch (IOException e) {
        log.error("下载题目测试数据的压缩文件异常------------>{}", e.getMessage());
        response.reset();
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        Map<String, Object> map = new HashMap<>();
        map.put("status", CommonResult.STATUS_ERROR);
        map.put("msg", "下载文件失败,请重新尝试!");
        map.put("data", null);
        try {
            response.getWriter().println(JSONUtil.toJsonStr(map));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    } finally {
        try {
            bins.close();
            if (outs != null) {
                outs.close();
            }
            if (bouts != null) {
                bouts.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 清空临时文件
        FileUtil.del(Constants.File.FILE_DOWNLOAD_TMP_FOLDER.getPath() + File.separator + fileName);
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) FileWriter(cn.hutool.core.io.file.FileWriter) ProblemCase(top.hcode.hoj.pojo.entity.problem.ProblemCase) FileReader(cn.hutool.core.io.file.FileReader) MultipartFile(org.springframework.web.multipart.MultipartFile) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Aggregations

FileWriter (cn.hutool.core.io.file.FileWriter)24 FileReader (cn.hutool.core.io.file.FileReader)16 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)10 JSONObject (cn.hutool.json.JSONObject)9 JSONArray (cn.hutool.json.JSONArray)8 File (java.io.File)8 ProblemCase (top.hcode.hoj.pojo.entity.problem.ProblemCase)7 MultipartFile (org.springframework.web.multipart.MultipartFile)6 Collectors (java.util.stream.Collectors)5 Slf4j (lombok.extern.slf4j.Slf4j)5 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)5 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)5 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)5 FileUtil (cn.hutool.core.io.FileUtil)4 IdUtil (cn.hutool.core.util.IdUtil)4 ZipUtil (cn.hutool.core.util.ZipUtil)4 JSONUtil (cn.hutool.json.JSONUtil)4 java.io (java.io)4 URLEncoder (java.net.URLEncoder)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4