0

I am trying to download a large file in a Spring Boot microservices application. Can anyone suggest a way of doing that?

Here is my front-end code

<a href={path + curDoc.id} download></a>

AJAX controller code

@GetMapping("/download-collector-file/{collectorFileId}")
public ResponseEntity<Resource> downloadCollectorFile(@PathVariable String collectorFileId) {
    System.out.println("kjwenfjkewew beforr");

    ResponseEntity<Resource> r=   fileServiceAPI.downloadCollectorFile("token",collectorFileId, false);

    System.out.println("after api");

    return r;
}

Feign client code

@GetMapping("/order-files/read/{id}")
ResponseEntity<Resource> downloadCollectorFile(@RequestHeader("Authorization") String auth,
                                  @PathVariable String id, 
                                  @RequestParam("isDownload") Boolean isDownload);

And my service back-end API code

@RequestMapping(value="/{id}" ,  method = RequestMethod.GET )
public ResponseEntity<InputStreamResource> downloadCollectorFile(@RequestHeader("Authorization") String auth, @PathVariable String id, @RequestParam(required = false, value = "isDownload") boolean isDownload) throws TgxValidationException, IOException {
    System.out.println("new file  id------===>"+id);

    Optional<TgxFilesEntity> optionalTgxFilesEntity = tgxFilesRepository.findByIdAndDeletedFalse(id);

    if (optionalTgxFilesEntity.isPresent()) {
        TgxFilesEntity tgxFilesEntity = optionalTgxFilesEntity.get();
        logger.info(">> UPLOAD_FOLDER=" + UPLOAD_FOLDER_PATH);

        String absolutePath = UPLOAD_FOLDER_PATH + "/collector/" + tgxFilesEntity.getRelationalId() + "/";

        final File parent = new File(absolutePath + tgxFilesEntity.getFilename());
        InputStreamResource resource = new InputStreamResource(new FileInputStream(parent));

        String mimeType = URLConnection.guessContentTypeFromName(tgxFilesEntity.getFilename());

        logger.info("mimeType" + mimeType);

        HttpHeaders headers = new HttpHeaders();
        if (!isDownload) {
            headers.add("Content-disposition", "inline; filename=" + tgxFilesEntity.getFilename());
            if (mimeType == null) {
                int lastIndexOf = tgxFilesEntity.getFilename().lastIndexOf(".");
                if (tgxFilesEntity.getFilename().substring(lastIndexOf).contains(".json")) {
                    mimeType = "application/json";
                } else if (tgxFilesEntity.getFilename().substring(lastIndexOf).contains(".xlsx")) {
                    mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                } else if (tgxFilesEntity.getFilename().substring(lastIndexOf).contains(".doc")) {
                    mimeType = "application/msword";
                }
                else if (tgxFilesEntity.getFilename().substring(lastIndexOf).contains(".zip")) {
                    mimeType = "application/zip";
                }
                else {
                    mimeType = "text/plain";
                }
            }
        } else {
            logger.info("hello else");
            headers.add("Content-disposition", "attachment; filename=" + tgxFilesEntity.getFilename());
            mimeType = "multipart/form-data";
        }

        System.out.println("brefore return===>"+resource.getFilename());

        return ResponseEntity.ok().headers(headers).contentType(MediaType.parseMediaType(mimeType)).body(resource);
    } else throw new FileNotFoundException(environment.getProperty("tgx.validation.file_not_found"));
}

Basically, it is not working for larger files like (300mb,400mb).

Is there any better approach of doing this?

Ultivic
  • 53
  • 1
  • 5
  • 1
    Can you explain what "it is not working" means? – Tarmo Jan 04 '23 at 12:00
  • i am hitting server api direclty it starts downloading file immedeatly but if i hit it from web side using feign client it doesnt start downloading and wait for server to download completely when server returns resources than it starts downloading the file. – Ultivic Jan 04 '23 at 12:13
  • Sorry to bother but the problem is very unclear. Is it like you are downloading a file with the help of a 'java backend code' by hitting an API of another server that has the file? Currently, it's very unclear what is `server API`, what is `foreign client` etc., etc., and how you are creating the request and where the problem is occurring. – Ryednap Jan 04 '23 at 15:10
  • actually i have a form in my frontend side where i i have put an anchor tag with the help of it, i hit an api on backend service using feign client... when i hit my api it works well for small files but for larger files it doesnt return anything...but when i hit my backend service api from postman than it works fine for larger files as well.... can you suggest me a way of downloading large files in springboot from frontend using feign client?? – Ultivic Jan 04 '23 at 17:34

0 Answers0