Last active
August 29, 2015 14:03
-
-
Save asbestos/b1fb278b55cefc73dcfe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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