【问题标题】:jersey (web service) : the advantages of StreamingOutput and the best way for create downloading APIjersey (web service) : StreamingOutput 的优势和创建下载 API 的最佳方式
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 更具体地说出你的答案
    • ohhhhh,这个问题太老了,我很久以前就找到了答案,但我很感谢你花时间在这个问题上
    猜你喜欢
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2018-07-11
    • 2017-07-26
    • 2012-01-15
    相关资源
    最近更新 更多