Skip to content

Instantly share code, notes, and snippets.

@asbestos
Last active August 29, 2015 14:03
Show Gist options
  • Save asbestos/b1fb278b55cefc73dcfe to your computer and use it in GitHub Desktop.
Save asbestos/b1fb278b55cefc73dcfe to your computer and use it in GitHub Desktop.
public class LettingUserDownladFile extends UI {
@Override
protected void init(VaadinRequest request) {
Button downloadButton = new Button("Download image");
StreamResource myResource = createResource();
FileDownloader fileDownloader = new FileDownloader(myResource);
fileDownloader.extend(downloadButton);
setContent(downloadButton);
}
private StreamResource createResource() {
return new StreamResource(new StreamSource() {
@Override
public InputStream getStream() {
String text = "My image";
BufferedImage bi = new BufferedImage(100, 30, BufferedImage.TYPE_3BYTE_BGR);
bi.getGraphics().drawChars(text.toCharArray(), 0, text.length(), 10, 20);
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bi, "png", bos);
return new ByteArrayInputStream(bos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}, "myImage.png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment