如何使用Java REST API下载tar文件?

问题描述 投票:-2回答:1

我正在尝试编写Java REST API,它将tar文件流回给用户。有人可以指出一些资源吗?我尝试了一些示例,但没有一个起作用。

java api rest streaming
1个回答
0
投票

您应该返回字节数组,并在header中指定这是一个Tape archive,例如:

@RequestMapping("/resources/tape_archive.tar")
public ResponseEntity<?> tapeArchive() throws IOException {
    URL url = MyClass.class.getClassLoader()
            .getResource("/resources/tape_archive.tar");

    assert url != null;
    File file = new File(url.getFile());

    InputStreamReader isr = new InputStreamReader(new FileInputStream(file));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int read;
    while ((read = isr.read()) != -1) {
        baos.write(read);
    }

    return ResponseEntity
            .ok()
            .header("content-type", "application/x-tar")
            .body(baos.toByteArray());
}

参见:List of archive formats | wikipedia.org

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.