【发布时间】:2018-12-22 21:08:32
【问题描述】:
我可以下载单个文件,但如何下载包含多个文件的 zip 文件。
以下是下载单个文件的代码,但我有多个文件要下载。任何帮助都将不胜感激,因为我在过去 2 天一直坚持这一点。
@GET
@Path("/download/{fname}/{ext}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFile(@PathParam("fname") String fileName,@PathParam("ext") String fileExt){
File file = new File("C:/temp/"+fileName+"."+fileExt);
ResponseBuilder rb = Response.ok(file);
rb.header("Content-Disposition", "attachment; filename=" + file.getName());
Response response = rb.build();
return response;
}
【问题讨论】:
-
你试过
response.setContentType("application/zip");吗? -
我认为你将不得不使用
rb.header("Content-Type", "application/zip"); -
所以 zip 文件已经存在或者您希望即时创建它?另外,一个问题是完整的文件需要在内存中还是在下载时需要以块的形式流式传输?
-
我必须即时创建 zip 文件并回答您的第二个问题是完整文件需要在内存中,但如果超过 100 MB,我们将限制下载。
-
@abhishekchauhan:不能保证您将拥有一个并发客户端,因此根据内存占用量,100MB 可能必须乘以允许的并发连接数。只是一个想法。
标签: rest spring-boot spring-rest