我已使用此方法为我的 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/