What is the way to stop double encoding on File Path?

Viewed 74

I want to get the file in resource object using UrlResource, but .toUri method encode path in such a way, that there is file exist, but still, it can't get.

My code is:

val filepath = Paths.get(getClass.getClassLoader.getResource("some file4216.pdf").getPath)

val resource : Resource = new UrlResource(filepath.toUri)

val contentType = "application/pdf"

ResponseEntity.ok()
    .contentType(MediaType.parseMediaType(contentType))
    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename + "\"")
    .body(resource)

It will encode space as "%20", but then also it performs encoding and convert "%" into "%2520". And that's why file not found.

Give below path:

/home/user/Live_Projects/some-project/some-project-api/build/resources/main/some%2520file4216.pdf

I want only %20 not %2520.

0 Answers
Related