opensagres / xdocreport

XDocReport means XML Document reporting. It's Java API to merge XML document created with MS Office (docx) or OpenOffice (odt), LibreOffice (odt) with a Java model to generate report and convert it if you need to another format (PDF, XHTML...).
https://github.com/opensagres/xdocreport
1.22k stars 372 forks source link

使用process方法后,不能关闭OutputStream流,导致不能删除文件 #565

Open haoyualways opened 2 years ago

haoyualways commented 2 years ago

private void exportListData(Map<String, Object> map){ FileInputStream ins = null; FileOutputStream out = null; try { ins = new FileInputStream(UserInfoExportConstant.TEMPLATE_PATH); IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);

        IContext context = report.createContext();

        FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
        for (String key : map.keySet()) {
            ArrayList<?> list = (ArrayList) map.get(key);
            if (CollectionUtil.isNotEmpty(list)){
                Class<?> clazz = list.get(0).getClass();
                fieldsMetadata.load(key,clazz,true);
            }
            context.put(key,map.get(key));
        }

        File file = new File(UserInfoExportConstant.TEMPORARY_FILES_PATH);
        File parentFile = file.getParentFile();
        if ( !parentFile.exists() ){
            if (parentFile.mkdirs()) {
                log.warn("mkdirs"+ parentFile.getAbsolutePath());
            }
        }

        out = new FileOutputStream(file);

        report.process(context,out);

        log.info("导出List数据成功...保存路径为:{}",UserInfoExportConstant.TEMPORARY_FILES_PATH);
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            if ( ins != null ){
                ins.close();
            }
            if ( out != null ){
                out.close();
            }
        }catch (IOException e){
            log.error("操作流时发生异常,{}",e.getMessage());
        }
    }
}
haoyualways commented 2 years ago

@mishadoff @pascalleclercq @xylo @tiry

angelozerr commented 2 years ago

Could you explain your problem please?

ManXliu commented 1 year ago

你用OutputStream2InputStream 这个类就可以了