0

我正在使用 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()通过输入不适当的参数滥用了该方法。OutputStream如果不是,我应该使用哪个ByteArrayOutputStreamOutputStream如果除了之外没有其他可行FileOutputStream的方法,您是否知道如何String在不创建文件的情况下返回 in 输出?

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

4

1 回答 1

1

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

此外,没有充分的理由调用toString字节数组输出流;比较用getBytes()

于 2017-07-30T20:41:56.800 回答