Pioneer丶 2023-03-31 16:17 采纳率: 40%
浏览 40

springboot下载文件时遇到io异常和response响应已开始异常,求解决,如何解决?(语言-java)

下面是DownFileUtils类中的代码,找了好久都找不出问题。我明明没有在前面的代码中关闭流。为什么会报io流已关闭异常呢?请各位帮忙!!
(有时候报io异常有时候报response异常)

    private static byte[] createZip(String sourceFilePath) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        // 将目标文件打包成zip导出
        File file = new File(sourceFilePath);
        handlerFile(zip, file, "附件");
        // 无异常关闭流,它将无条件的关闭一个可被关闭的对象而不抛出任何异常。
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }


  @SneakyThrows
    public static void exportZip(HttpServletResponse response, String sourceFilePath, String fileName, boolean isBatch, Predicate<MultipartFile> predicate) {
        // 默认文件名以时间戳作为前缀
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String downloadName = fileName + "-" + sdf.format(new Date()) + ".zip";
        // 将文件进行打包下载
        boolean b = false;
        OutputStream os = null;
        File zip = null;
        try {
            os = response.getOutputStream();
            // 接收压缩包字节
            byte[] data = createZip(sourceFilePath);
            //上传文件到文件服务器
            if (isBatch) {
                zip = ZipUtil.zip(sourceFilePath, Charset.forName("UTF-8"));
                FileInputStream fis = new FileInputStream(zip);
                MultipartFile file = new MockMultipartFile("file", downloadName, "text/plain", fis);
                b = predicate.test(file);
            } else {
                response.reset();
                response.setCharacterEncoding("UTF-8");
                response.addHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Expose-Headers", "*");
                // 下载文件名乱码问题
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(downloadName, "UTF-8"));
                //response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + downloadName);
                response.addHeader("Content-Length", "" + data.length);
                response.setContentType("application/octet-stream;charset=UTF-8");
               //org.apache.commons.io下的类
                IOUtils.write(data, os);
            }

        } catch (Exception e) {
            log.info("下载异常");
            e.printStackTrace();
        } finally {
            os.flush();
            os.close();
            if (isBatch) {
                // 删除临时文件
                zip.delete();
            }
            deleteDir(new File(sourceFilePath));
        }
    }

下面是异常日志:

img

img

  • 写回答

2条回答 默认 最新

  • 瞬间的未来式 2023-03-31 16:22
    关注

    如果在执行createZip方法时抛出了异常,那么IOUtils.closeQuietly(zip)方法将不会执行,可能导致输出流未被关闭,从而造成内存泄漏。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月31日

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆