gpt4 book ai didi

java - Jersey (网络服务): the advantages of StreamingOutput and the best way for create downloading API

转载 作者:太空宇宙 更新时间:2023-11-04 11:19:33 26 4
gpt4 key购买 nike

我已使用此方法为我的 REST Web 服务创建一个 API,以便使用 jersey JAX-RS 下载文件:

public Response returnFile(String filePath,String fileName,MediaType type) throws IOException {
File file = new File(filePath);
String[] filePart = filePath.split("\\.");
String fileEnd = filePart[filePart.length - 1];
return Response.ok(file, type)
.header("Content-Disposition","attachment; filename=\""
+ URLEncoder.encode(fileName + "." + fileEnd,"UTF-8") + "\"" )
.build();
}

但有人告诉我这可能会出现性能问题并导致 OutOfMemory 异常。

所以我最近搜索创建这样的 API,我发现大多数示例都使用 StreamingOutput

那么使用StreamingOutput有什么优势呢?在我的方法中使用它而不是 File 更好吗?如果有人说出创建此类 API(用于下载文件的 API)的最佳方法,我将非常感激。

最佳答案

是的,它的内存效率更高。阅读本文了解更多信息:https://dzone.com/articles/jax-rs-streaming-response

关于java - Jersey (网络服务): the advantages of StreamingOutput and the best way for create downloading API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45075920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com