You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vaadin 8实现基于动态String内容的文本文件下载按钮

Download Dynamic String as Text File with Vaadin 8 FileDownloader

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_8 ensures your text doesn't have garbled characters, especially if it includes non-ASCII symbols.
  • MIME Type: Setting text/plain; charset=UTF-8 tells the browser exactly what kind of file it's dealing with, so it opens or saves it correctly.
  • Dynamic Content: Replace the dynamicContent variable with your actual dynamic string—this could be user input, generated reports, or anything else you need to export.
  • Filename: The second parameter in StreamResource is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:18:17