【问题标题】:OutputStream issue with Documents4jDocuments4j 的 OutputStream 问题
【发布时间】:2017-07-24 08:19:43
【问题描述】:

我正在使用 Documents4j 将文档转换为 PDF/a。我想构建一个函数,使用以下代码返回我的文件的String 表示:

    String input=...;
    DocumentType[] docType= {DocumentType.CSV,DocumentType.DOC,DocumentType.MHTML,DocumentType.MS_EXCEL,DocumentType.MS_WORD,DocumentType.ODS,DocumentType.PDF,DocumentType.RTF,DocumentType.TEXT,DocumentType.XML};

    IConverter converter = LocalConverter.make();

    ByteArrayInputStream in= new ByteArrayInputStream(input.getBytes());       
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    Future<Boolean> conversion = converter.convert(in)
    .as(docType[n-1])
    .to(out) 
    .as(DocumentType.PDFA)
    .prioritizeWith(1000) // optional
    .schedule();

    String output=out.toString();

    in.close();
    out.flush();
    out.close();
    System.out.println(output);
    return(output);

但是我的输出是空白的。我想我通过输入不适当的参数滥用了.to() 方法。如果不是ByteArrayOutputStream,我应该使用哪个OutputStream?如果除了FileOutputStream 之外没有任何可行的OutputStream,您是否知道如何在不创建文件的情况下在输出中返回String

提前感谢您的关注和回答。

【问题讨论】:

    标签: java outputstream documents4j


    【解决方案1】:

    当调用schedule 而不是exectue 时,作业在后台运行。您在这里面临着赛车状况。

    此外,没有充分的理由在字节数组输出流上调用toString;宁可使用getBytes()

    【讨论】:

      猜你喜欢
      • 2011-05-20
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多