读取文件解析文件发生的错误,已解决

在处理多个PDF文件时,程序出现错误导致合并后的PDF在压缩并上传后损坏。问题在于文件流未关闭就开始压缩操作。解决方法是确保在合并PDF文件后立即关闭文件流,然后再进行压缩和上传,这样可以避免文件损坏。调整代码顺序后,问题得到解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误背景

从文件服务器上面读取zip压缩文件下来,然后解压,放指定目录下。获取所有解压后文件的路径地址保存在list集合里面。最后要求是把所有获取到zip里面的pdf文件合成为一个总pdf文件,放入压缩包,上传到文件服务器上面就完成了。
发生错误点:所有pdf合成一个总pdf文件,在压缩为zip文件上传上去后,打开发下合成的pdf损坏了。
错误代码:

public static String start(List<String> savepath){
        Stopwatch stopwatch = Stopwatch.createStarted();
        // 获取回单文件在linux上的临时存放地址
        final String linuxFilePath = HippoClientUtil.getValue(HippoKeyConstant.FILE_PATH_TEMP);
        List<String> fileList = new ArrayList<>();
        String downloadUrl = null;
        for (int i = 0; i < savepath.size(); i++) {
            fileList.add(linuxFilePath+savepath.get(i));
        }
        log.info("fileList集合:{}",fileList.size());
        Document  document = null;
        String pdfUrl = linuxFilePath+System.currentTimeMillis()+".pdf";
        String pdfZip = linuxFilePath+System.currentTimeMillis()+".zip";
        try {
            document = new Document();

            PdfCopy copy = new PdfCopy(document, new FileOutputStream(pdfUrl));
            document.open();
            for (String s : fileList) {
                PdfReader reader = new PdfReader(s);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }
            
            //放入压缩文件中
            ZipFile zipFile = new ZipFile(pdfZip);
            zipFile.addFile(new File(pdfUrl));
            String fileRelativePath = FastdfsUtil.uploadFile(pdfZip);
            downloadUrl = FastdfsUtil.getDownUrl(fileRelativePath);
            log.info("相对路径:{},下载地址:{}", fileRelativePath, downloadUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (String s : fileList) {
            FileUtils.deleteQuietly(new File(s));
            FileUtils.deleteQuietly(new File(pdfUrl));
            FileUtils.deleteQuietly(new File(pdfZip));
        }
		document.close();
        stopwatch.stop();
        log.info("耗时:{}",stopwatch.elapsed(TimeUnit.SECONDS));
        return downloadUrl;
    }

解释下:获取文件路径然后把这些pdf文件合成一个,然后放入压缩包中,在上传到文件服务器上面去
问题原因:在pdf文件合成一个总pdf文件时,文件流还未关闭,就进行压缩操作,这是获取到的文件就是损坏的。
解决方法:

      document.close();
      stopwatch.stop();

代码块放入合成总pdf文件之后,然后在做其他操作,文件就不是损坏的了。
正确代码示例:

public static String start(List<String> savepath){
        Stopwatch stopwatch = Stopwatch.createStarted();
        // 获取回单文件在linux上的临时存放地址
        final String linuxFilePath = HippoClientUtil.getValue(HippoKeyConstant.FILE_PATH_TEMP);
        List<String> fileList = new ArrayList<>();
        String downloadUrl = null;
        for (int i = 0; i < savepath.size(); i++) {
            fileList.add(linuxFilePath+savepath.get(i));
        }
        log.info("fileList集合:{}",fileList.size());
        Document  document = null;
        String pdfUrl = linuxFilePath+System.currentTimeMillis()+".pdf";
        String pdfZip = linuxFilePath+System.currentTimeMillis()+".zip";
        try {
            document = new Document();

            PdfCopy copy = new PdfCopy(document, new FileOutputStream(pdfUrl));
            document.open();
            for (String s : fileList) {
                PdfReader reader = new PdfReader(s);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
            }
            document.close();
            stopwatch.stop();
            //放入压缩文件中
            ZipFile zipFile = new ZipFile(pdfZip);
            zipFile.addFile(new File(pdfUrl));
            String fileRelativePath = FastdfsUtil.uploadFile(pdfZip);
            downloadUrl = FastdfsUtil.getDownUrl(fileRelativePath);
            log.info("相对路径:{},下载地址:{}", fileRelativePath, downloadUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (String s : fileList) {
            FileUtils.deleteQuietly(new File(s));
            FileUtils.deleteQuietly(new File(pdfUrl));
            FileUtils.deleteQuietly(new File(pdfZip));
        }
        log.info("耗时:{}",stopwatch.elapsed(TimeUnit.SECONDS));
        return downloadUrl;
    }

刚刚开始以为是压缩文件的时候,压缩的问题,后来换了几种方式,还是报文件损坏,最后午休起来灵光一现,发下这文件流放最后面关闭,是否是因为这个问题,最后终于可了,哎,纠结了一个上午。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值