Vaadin 8实现基于动态String内容的文本文件下载按钮
Got it, let's walk through exactly how to make this work—no static files required, just generating a text file on the fly from your dynamic String content using Vaadin 8's FileDownloader.
Step 1: Create a StreamResource for your dynamic content
The key here is using StreamResource because it lets you generate content from an input stream, which is perfect for dynamic strings. You'll need to override its getStream() method to convert your String into a byte stream.
Step 2: Attach FileDownloader to a trigger component
FileDownloader needs to be attached to an interactive component (like a Button) that users can click to start the download.
Here's a complete, ready-to-use example:
import com.vaadin.server.StreamResource; import com.vaadin.ui.Button; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; import com.vaadin.server.FileDownloader; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class DynamicDownloadUI extends UI { @Override protected void init(VaadinRequest vaadinRequest) { VerticalLayout layout = new VerticalLayout(); // Your dynamic String content (this could come from user input, a database, etc.) String dynamicContent = "Hello from Vaadin 8!\nThis is dynamic text content generated on the fly.\nEnjoy your download!"; // Create the StreamResource StreamResource resource = new StreamResource(new StreamResource.StreamSource() { @Override public InputStream getStream() { // Convert the String to an input stream with UTF-8 encoding (avoids weird characters) return new ByteArrayInputStream(dynamicContent.getBytes(StandardCharsets.UTF_8)); } }, "my-dynamic-file.txt"); // Set your desired filename here // Set the content type to plain text so browsers handle it correctly resource.setMIMEType("text/plain; charset=UTF-8"); // Create a button to trigger the download Button downloadButton = new Button("Download Text File"); // Attach FileDownloader to the button FileDownloader fileDownloader = new FileDownloader(resource); fileDownloader.extend(downloadButton); layout.addComponent(downloadButton); setContent(layout); } }
Key Details to Note:
- Charset Encoding: Using
StandardCharsets.UTF_8ensures your text doesn't have garbled characters, especially if it includes non-ASCII symbols. - MIME Type: Setting
text/plain; charset=UTF-8tells the browser exactly what kind of file it's dealing with, so it opens or saves it correctly. - Dynamic Content: Replace the
dynamicContentvariable with your actual dynamic string—this could be user input, generated reports, or anything else you need to export. - Filename: The second parameter in
StreamResourceis the default filename users will see when downloading. You can change this dynamically too if needed (just update the resource's filename before triggering the download).
If you need to update the dynamic content after the UI is initialized (like when a user fills out a form), just re-create the StreamResource and re-extend the button with a new FileDownloader, or update the stream source dynamically.
内容的提问来源于stack exchange,提问作者kosiara - Bartosz Kosarzycki

