'Is it possible to get multiple files in one GET request?

I'm working on a Spring Boot project and I need users to be able to send a GET request to my service and get multiple files as response without pages involved. This is what I have right now:

public void get(HttpServletResponse response,FileInputStream[] streams){
    String boundary = "------customBoundary";
    response.setContentType("multipart/mixed; boundary="+boundary);
    String contentType = "Content-type: audio/mpeg";
    var outputStream = response.getOutputStream();
    for(FileInputStream ois:streams){
        if(ois!=null){
            outputStream.println("--"+boundary);
            outputStream.println(contentType);
            outputStream.println("Content-disposition: attachment; filename=\"random.mp3\"");
            outputStream.println();
            try{
                BufferedInputStream inputStream = new BufferedInputStream(ois);
                int swap;
                while ((swap = inputStream.read()) != -1) {
                    outputStream.write(swap);
                }
                outputStream.println();
                outputStream.flush();
                ois.close();
                inputStream.close();
            }catch (Exception s){
                s.printStackTrace();
            }
        }
    }
    outputStream.println("--"+boundary+"--");
    outputStream.flush();
    outputStream.close();
}

If you access the service on Chrome, it'll automatically download a file name "download", as the browser sees the whole this as one giant file attachment.

I think the problem may be at the ContentType part, so far I tried "multipart/x-mixed-replace" as some post here suggested, only to find out later that it was removed several years ago. I tried "multipart/form-data" out of desperation and that didn't work.

The files I'm dealing with are media files, it's quite large and I'm running this on a crappy server, so putting them in one zip file is not an option.

Most of the result I found more or less have something to do with html or javascript, but what I'm doing here is intended as an API for other services to call so I'm hoping it can be done with pure java.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source