code: private String buildFileUrl(String fileId) {
  
      if (RequestContextHolder.getRequestAttributes() != null) {
        ServletUriComponentsBuilder currentRequest = ServletUriComponentsBuilder.fromCurrentRequest();
        UriComponents downloadUri =
            currentRequest
                .replacePath(FileDownloadController.URI + '/' + fileId)
                .replaceQuery(null)
                .build();
        return downloadUri.toUriString();
      } else {
        // TODO this is a workaround for the situation where the File url needs to be created without
        // a request
        // context, in order to fix the properly we would need to add a deploy time property
        // defining the file download server location
        return FileDownloadController.URI + '/' + fileId;
      }
    }
docs: Build a downLoadUrl for the file identified by the given fileId. If this method is called
  outside of a request context ( system initialization for example) the server context is used
  for generating a relative path