简体   繁体   中英

InputStream to OutputStream

I have to a class that generates an Excel file, class takes an output stream, intended for a FileOutputWriter . However I need to upload the file to S3 later, so saving it to a file would be an unnecessary step. To upload a file to S3 do I need it as an input stream.

I have therefore been looking for a way to convert an output stream to an input stream or transfer the data to an input stream.

I have tried PipeOutputStream , but xlsFile.write(outstr); just blocks forever when using a PipedOutputStream , using a FileOutputStream will it work fine and write the file.

PipedInputStream inputStream = new PipedInputStream();
try (OutputStream outstr = new PipedOutputStream(inputStream)) {
    xlsFile.write(outstr);
    outstr.flush();
    return inputStream;
}

JDK 9 has added InputStream#transferTo(OutputStream out) for this functionality, though JDK 9 is not yet released. Early releases are available here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM