Example usage for com.vaadin.server StreamResource setFilename

List of usage examples for com.vaadin.server StreamResource setFilename

Introduction

In this page you can find the example usage for com.vaadin.server StreamResource setFilename.

Prototype

public void setFilename(String filename) 

Source Link

Document

Sets the filename.

Usage

From source file:com.foc.vaadin.gui.components.FVImageField.java

License:Apache License

public boolean refreshImageFromProperty() {
    BufferedImage photo = getBufferedImage();
    boolean error = photo == null;

    if (!error) {
        originalHeight = photo.getHeight();
        originalWidth = photo.getWidth();
        setAttributes(getAttributes());//from  www. ja va  2  s.  c om
        StreamResource.StreamSource streamSource = new StreamResource.StreamSource() {

            public InputStream getStream() {
                try {
                    /* Write the image to a buffer. */
                    ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
                    ImageIO.write(getBufferedImage()/*Globals.getApp().getCompany().getLogo()*/, "png",
                            imagebuffer);
                    /* Return a stream from the buffer. */
                    return new ByteArrayInputStream(imagebuffer.toByteArray());
                } catch (IOException e) {
                    Globals.logException(e);
                    return null;
                }
            }

        };
        UI application = FocWebApplication.getInstanceForThread();
        if (application != null) {
            StreamResource streamResource = new StreamResource(streamSource, "");
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String timestamp = df.format(new Date(System.currentTimeMillis()));
            streamResource.setFilename("FileName-" + timestamp + ".jpg");

            if (embedded != null) {
                embedded.setSource(streamResource);
            }
        }
    }
    return error;
}