多次使用 InputStream
Use InputStream multiple times
我有一个观点,用户可以先根据一些参数生成PDF,然后下载and/or通过邮件发送。
现在,生成 PDF 文件的方法 returns 和 InputStream
,然后我将其存储为 class 的字段,如下所示:
public class PDFWindow extends VerticalLayout {
...
private InputStream pdfInputStream;
...
private void createPDF() {
this.pdfInputStream = pdfCreator.createPDF();
}
我的问题是,pdfInputStream
在被 FileDownloader
使用后关闭:
Button download = new Button("Download");
final FileDownloader fileDownloader = new FileDownloader(
new StreamResource(() -> this.pdfInputStream, this.pdfFileName));
fileDownloader.extend(download);
或者我写的SpringEmailService
:
SpringEmailService.send(
"test@mail.com", recipients, this.subject.getValue(),
this.message.getValue(),this.pdfInputStream,"test.pdf", "application/pdf");
有什么方法可以阻止 InputStream
关闭然后手动关闭它,还是我应该寻找一种完全不同的方法?
只是不要将 InputStream 存储为字段。
每次需要时,请创建者创建流。
如果它太昂贵,则将生成的字节存储在内存中、文件中、数据库中或任何你想要的地方,并在需要时在这些 bytes/file 上创建一个流。
我有一个观点,用户可以先根据一些参数生成PDF,然后下载and/or通过邮件发送。
现在,生成 PDF 文件的方法 returns 和 InputStream
,然后我将其存储为 class 的字段,如下所示:
public class PDFWindow extends VerticalLayout {
...
private InputStream pdfInputStream;
...
private void createPDF() {
this.pdfInputStream = pdfCreator.createPDF();
}
我的问题是,pdfInputStream
在被 FileDownloader
使用后关闭:
Button download = new Button("Download");
final FileDownloader fileDownloader = new FileDownloader(
new StreamResource(() -> this.pdfInputStream, this.pdfFileName));
fileDownloader.extend(download);
或者我写的SpringEmailService
:
SpringEmailService.send(
"test@mail.com", recipients, this.subject.getValue(),
this.message.getValue(),this.pdfInputStream,"test.pdf", "application/pdf");
有什么方法可以阻止 InputStream
关闭然后手动关闭它,还是我应该寻找一种完全不同的方法?
只是不要将 InputStream 存储为字段。
每次需要时,请创建者创建流。
如果它太昂贵,则将生成的字节存储在内存中、文件中、数据库中或任何你想要的地方,并在需要时在这些 bytes/file 上创建一个流。