any better way than using old servlet api for downloads I am wondering if the following code can be modified to be used with latest version of spring APIs?
The following code works fine. However, I wanted to know if servlet APIs can be avoided which i have used in the code below.
public void download (HttpServletRequest request, HttpServletResponse response, @PathVariable String filename, @PathVariable String user) throws IOException{
              
            String param1 = filename;
            String param2 = user;

            //The following URL will need to be updated with &quot;prod&quot; or &quot;dev&quot; or &quot;test&quot; depending upon the environment
            final String FILE_LOCATION = &quot;/mnt/nfs/myfolder/prod/documents/custom_documents/&quot;+param2;
            
            
            if(param1 == null || param2 == null) {
                // The Request Parameters Were Not Present In The Query String. Do Something Or Exception Handling !!
                System.out.println(&quot;Request Parameter Not Found in first if!&quot;);
            } else if (&quot;&quot;.equals(param1) || &quot;&quot;.equals(param2)) {
                // The Request Parameters Were Present In The Query String But Has No Value. Do Something Or  Exception Handling !!
                System.out.println(&quot;Request Parameter Not Found in second if!&quot;);
            } else {

            
                String fileName = (String) param1;
                String contentType = getContentType(fileName.split(&quot;\\.&quot;)[1]);
                File file = new File(FILE_LOCATION + &quot;/&quot; +fileName);
                response.setContentType(contentType);
                response.addHeader(&quot;Content-Disposition&quot;,&quot;attachment; filename=&quot; + fileName);
                response.setContentLength((int) file.length());
                ServletOutputStream servletOutputStream = response.getOutputStream();
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                int bytesRead = bufferedInputStream.read();
                while (bytesRead != -1) {
                    servletOutputStream.write(bytesRead);
                    bytesRead = bufferedInputStream.read();
                }
                if (servletOutputStream != null) servletOutputStream.close();
                if(bufferedInputStream != null) bufferedInputStream.close();

            }
            
            
            
    }




private String getContentType(String fileType) {
        String returnType = null;
        for(int i = 0; i &lt; contentTypes.length; i++) {
            if(fileType.equals(contentTypes[i][0])) returnType = contentTypes[i][1];
        }
        return returnType;
    }
Resource interface provides an effective way of accessing the content of the resource and supports handling of all kinds of file resources. Please see if that helps you to some extent.
public ResponseEntity &lt; Resource &gt; downloadFile(String fileName) {

  final String FILE_LOCATION = &quot;/mnt/nfs/myfolder/prod/documents/custom_documents/&quot;;

  File file = new File(FILE_LOCATION + &quot;/&quot; + fileName);

  Path path = Paths.get(file.getAbsolutePath());
  ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));

  HttpHeaders headers = new HttpHeaders();
  //headers.add(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=&quot; + file.getName());

  return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource);
}