简体   繁体   中英

Multiple zip file download on single Spring Boot rest controller

I have a requirement where:

    I have multiple files on the server location
    My Response size limit is 100 MB, Max

So my use case here is to merge files and produce in zip format and send attachments to the client browser. But, here client browser has only one button "DOWNLOAD ALL".Based on the click of the button, all files which are located on the server should get downloaded as multiple zip files to the client. For example, I have 7 files

 1.txt - 24 MB
 2.txt - 30 MB
 3.txt - 30 MB
 4.txt - 30 MB
 5.txt - 40 MB

so, By clicking of button two zip files should get downloaded as 1.zip contains 1.txt,2.txt,3.txt because it. has around 100 MB, and the other 2.zip will contain 4.txt and 5.txt.

I came across multiple things on the web like zipping a file and sending it as a response, but it sends only a response once the channel gets closed after the response is transferred.

http://techblog.games24x7.com/2017/07/18/streaming-large-data-as-zipped-file-from-rest-services/

https://javadigest.wordpress.com/2012/02/13/downloading-multiple-files-using-multipart-response/

Moreover, UI can have multiple requests to the endpoint, but I have multiple users, so I may need to keep track of users and files. Any idea or implementation will be appreciated.

Thanks in advance.

"Moreover, UI can have multiple requests to the endpoint, but I have multiple users, so I may need to keep track of users and files. Any idea or implementation will be appreciated."

This can be solved by using spring-actuators and log4J. Using spring actuators you can monitor from where the request is coming.

Can be done using the dependency

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

and in application.properties file:-

management.endpoints.web.exposure.include=*

as by default only 2 endpoints are exposed.

Regarding the multiple file download... is there a specific limit? like the zip file must be of 100mb in size?

If yes, then you need to orchestrate your solution by calling multiple end points one by one and download the same.

Maybe I have a workaround regarding this, if you take a look into the video-streaming concept, it's the same as your use-case since a huge file size served for users. So; I would like to suggest this as a possible solution.

The concept of streaming is to load specific chunks of the required file based on the client's request till the stream ends. so assuming that the file size is 100MB then the client has the ability to specify in the request header the chunk bypassing the Range HTTP Header, and your backend should understand this header and respond with the required data till the file ends.

Take a look at the below link, maybe will give you more details:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM