【发布时间】:2017-07-13 08:57:07
【问题描述】:
我曾使用此方法为我的REST Web 服务创建一个使用球衣 JAX-RS 下载文件的 API:
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)的最佳方法,我将不胜感激。
【问题讨论】:
标签: java rest jersey streaming download