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

基于Helidon MP/SE的大文件异步上传示例与大小限制咨询

Helidon 大文件异步上传相关问题解答

1. Helidon MP 大文件异步上传示例

结合JAX-RS异步特性与Servlet多部件配置,可实现大文件异步上传,示例如下:

资源类实现

import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.container.AsyncResponse;
import jakarta.ws.rs.container.Suspended;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.context.ManagedExecutor;
import jakarta.inject.Inject;
import jakarta.servlet.http.Part;
import jakarta.ws.rs.Consumes;

@Path("/upload")
public class AsyncFileUploadResource {

    @Inject
    ManagedExecutor executor;

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void uploadFile(@Suspended AsyncResponse response, Part file) {
        executor.runAsync(() -> {
            try {
                // 自定义文件存储逻辑,示例为写入本地目录
                String targetPath = "/path/to/storage/" + file.getSubmittedFileName();
                file.write(targetPath);
                response.resume("文件上传成功:" + file.getSubmittedFileName());
            } catch (Exception e) {
                response.resume(e);
            } finally {
                try {
                    file.delete();
                } catch (Exception ignore) {}
            }
        });
    }
}

配置文件调整(application.properties)

需修改Servlet多部件参数以支持大文件:

# 单文件最大限制
server.servlet.multipart.max-file-size=5GB
# 请求总大小限制
server.servlet.multipart.max-request-size=5GB
# 文件超过此阈值时写入磁盘而非内存
server.servlet.multipart.file-size-threshold=10MB

2. Helidon MP 与 SE 分别实现文件异步上传

Helidon MP 实现

除上述多部件示例外,还可使用ChunkedInput实现流式上传,更适合超大文件:

@POST
@Path("/stream-upload")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public void streamUpload(@Suspended AsyncResponse response, ChunkedInput<byte[]> input) {
    executor.runAsync(() -> {
        try (OutputStream out = new FileOutputStream("/path/to/storage/large-file.bin")) {
            byte[] chunk;
            while ((chunk = input.read()) != null) {
                out.write(chunk);
            }
            response.resume("流式上传完成");
        } catch (Exception e) {
            response.resume(e);
        }
    });
}

Helidon SE 实现

利用Helidon SE的WebServer异步API,结合多部件处理:

import io.helidon.webserver.WebServer;
import io.helidon.webserver.http.HttpRouting;
import io.helidon.webserver.multipart.MultiPart;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class AsyncFileUploadSE {
    public static void main(String[] args) {
        WebServer server = WebServer.builder()
                .routing(routing -> routing
                        .post("/upload", (req, res) -> {
                            req.content().as(MultiPart.class)
                                    .thenAccept(multiPart -> {
                                        multiPart.part("file")
                                                .ifPresent(part -> {
                                                    part.content().asStream()
                                                            .thenAccept(inputStream -> {
                                                                try (var out = Files.newOutputStream(Paths.get("/path/to/storage/" + part.filename()), StandardOpenOption.CREATE)) {
                                                                    inputStream.transferTo(out);
                                                                    res.send("文件上传完成:" + part.filename());
                                                                } catch (Exception e) {
                                                                    res.status(500).send("上传失败:" + e.getMessage());
                                                                }
                                                            });
                                                });
                                    });
                        })
                )
                .build()
                .start();

        System.out.println("服务器启动于 http://localhost:" + server.port());
    }
}

对应配置文件(application.yaml)调整:

server:
  multipart:
    max-file-size: 5GB
    max-request-size: 5GB
    file-size-threshold: 10MB

3. HTTPS浏览器上传的推荐文件大小限制

针对500MB至5GB的文件范围:

  • 推荐单文件上限设为5GB,与业务需求匹配,同时需结合服务器内存、磁盘IO和网络带宽调整。
  • 浏览器本身无严格硬限制,但建议采用分块上传(每块100MB-200MB)提升容错性,避免网络波动导致整份文件上传失败。Helidon支持分块上传的处理逻辑,可在资源类中实现分块合并。
  • 需调整JVM参数(如-Xmx)避免内存溢出,优先使用流式处理(如ChunkedInput或InputStream转存),避免将大文件全部加载至内存。

内容的提问来源于stack exchange,提问作者Arun Taneja

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 20:33:31