java itextpdf文件输出流未实际关闭

3ks5zfa0  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(491)

我正在尝试使用itextpdf 5.5.10使用java spring引导程序生成pdf文件。我设法将所需内容写入pdf文件,我可以打开和查看生成的文件,但无法删除它们。每次尝试删除生成的文件时,windows系统都会提示我该文件正被JavaSE平台使用。然而,在文件生成后大约10到15分钟,它就可以被删除。理想的情况是,我可以在生成文件后立即删除它。这对我来说真是一个奇怪的情况,因为我相信我已经正确地关闭了文档和文件输出流。下面是我的代码:
我将itextpdf提供的方法 Package 在自己的类中 MyPdfWriter :

public class MyPdfWriter {

    private Document document;
    private PdfWriter writer;

    private Font titleFont;
    private Font subtitleFont;
    private Font hFont; // heading font
    private Font pFont; // paragraph font
    private Font captionFont;
    private Font thFont;
    private Font tdFont;

    private static final float PAGE_MARGIN = 71f;

    ...

    public MyPdfWriter(String filename, String filepath) throws IOException, DocumentException {
        this.document = new Document(PageSize.A4);
        this.document.setMargins(PAGE_MARGIN, PAGE_MARGIN, PAGE_MARGIN, PAGE_MARGIN);
        this.writer = PdfWriter.getInstance(this.document, new FileOutputStream(filepath + "/" + filename));
        // open document
        this.document.open();
        // set metadata
        this.document.addTitle(filename);
        this.document.addAuthor(ShiroUtils.getSysUser().getUserName());

        // set font style
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        this.titleFont = new Font(bfChinese, 16, Font.BOLD);
        this.pFont = new Font(bfChinese, 12, Font.NORMAL);
        ...
    }

    public void addTitle(String text) throws DocumentException {
        Paragraph title = new Paragraph(text, this.titleFont);
        title.setAlignment(Element.ALIGN_CENTER);
        this.document.add(title);
    }

    public void addParagraph(String text) throws DocumentException {
        Paragraph paragraph = new Paragraph(text, this.pFont);
        this.document.add(paragraph);
    }

    public void addImage(String url) throws IOException, DocumentException {
        ...
    }

    ...

    /**
     * close writer
     */
    public void closeWriter() {
        this.document.close();
        this.writer.close();
        // I've tested this method with the following code:
        // this.document.isOpen(); // return false;
        // this.writer.isCloseStream(); // return true;
        // and nothing seems wrong...
    }

}

班级 MyPdfWriter 被示例化并在另一个类中使用 ReportServiceImpl :

@Service
public class ReportServiceImpl implements ReportService {

    ...

    @Override
    public int generateReport(Map<String, Object> data) {
        try {
            ...

            String filename = ...;
            String filepath = ...;

            // instantiate MyPdfWriter
            MyPdfWriter writer = new MyPdfWriter(filename, filepath);
            // write content to file
            writer.addTitle(...);
            writer.addParagraph(...);
            ...

            // close the pdf writer and file output stream
            writer.closeWriter();

        } catch (Exception e) {...}
    }

}

这个问题困扰了我一整天。。。我不确定问题出在哪里,但我怀疑这可能是由班级引起的 MyPdfWriter . 熟悉java和itextpdf的人能告诉我我的代码出了什么问题吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题