I have an InputStream from another server (PDF file from JasperReports) and user is able to download it.
...
VerticalLayout content;
OperationResult<InputStream> jresult;
...
final InputStream ent=jresult.getEntity();
if (ent.available()<=0) return;
Link link = new Link();
link.setCaption("Download the report");
link.setResource(new StreamResource(new StreamSource(){
private static final long serialVersionUID = 1L;
@Override
public InputStream getStream() {
return ent;
}
}, "FileName.pdf"));
content.addComponent(link);
If the print server returns the page, "Download the report" will appear and the user can download PDF file by click. But second click on the same link fails. It probably returns empty content. What is wrong? Maybe I must rewind the input stream. How?
That's because your getStream() method returns the same stream and streams are expected to read from them only once. Once you consume data in the stream, the data is not available anymore.
You may need to firstly convert your InputStream to bytes using this method (taken from this SO question)
and then in
getStream()
method return new InputStream every time:edit Solution #2: Like @Hink suggested in the comment you can also call reset() on your Stream object.