I am trying to download large file(in GBs), hence ...
# spring
y
I am trying to download large file(in GBs), hence need to directly write to the disk(without memory). I am able to get it. However, I would also like to get the filename of the attachment I am downloading. Can somehow help? This code works..(but here I am manually setting the filename) . Is there a way to read the header onstatus successful perhaps?
not kotlin but kotlin colored 2
I was able to get the filename by looking at the header response when there was a success
Copy code
suspend fun downloadFile(
        dir: String
    ) {
        val finalCookie = getCookie()
        try {
            var fileName = ""
            val dirPath = Paths.get(dir)
            if (!Files.exists(dirPath)) {
                Files.createDirectories(dirPath)
            }
            val response = webClient.
            get()
                .uri { uriBuilder: UriBuilder ->
                    UriComponentsBuilder.fromUri(uriBuilder.build())
                        .path("/download/file")
                        .encode()
                        .build(false)
                        .toUri()
                }
                .header(HttpHeaders.COOKIE, finalCookie)
                .retrieve()
                .onStatus(HttpStatus::is2xxSuccessful) { clientResponse ->
                    clientResponse.headers().asHttpHeaders().contentDisposition.filename?.let {
                        fileName = it
                    }
                    Mono.empty()
                }
                .bodyToFlux(DataBuffer::class.java)

            when(fileName)
            {
                "" -> fileName = UUID.randomUUID().toString()
            }
            val destination = Paths.get(dir, filename)
            // Need to use Databuffer so that no memory is used
            DataBufferUtils.write(response, destination).block()
            println("Downloaded $destination")
        } catch (e: Exception) {
            println("Error in downloading file: $e")
        }
    }