Servlet: Make a download resummable

224 Views Asked by At

I am creating a servlet that makes files to client downloadable. I achieved this but not able to make that resumable downloads to the client.

Here is my code

private void startDownloadProcess(File file) {
    this.response.addHeader("Accept-Ranges", "bytes");
    this.response.setContentType("APPLICATION/OCTET-STREAM");
    this.response.setContentLength((int) file.length());
    this.response.setHeader("Content-disposition", String.format("attachment; filename=%s", file.getName()));
    try (ServletOutputStream outputStream = this.response.getOutputStream()) {
        try (FileInputStream inputStream = new FileInputStream(file)) {
            byte[] buffer = new byte[8072];
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, len);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

This code will make the download available but the client is not able to pause and resume the downloads.

0

There are 0 best solutions below