JAVA_OPTS: -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8
File System : Linux
Spring Boot : 2.1.4
Spring framework: 5.1.6.RELEASE
Library used : org.springframework.core.io.FileSystemResource;
The problem exists for example in German characters. For filename containing space %20
(encoded) works fine.
The problematic file is named Einzelh?ndler überprüfen.pdf
and it's 12KB
Locally in windows and IntelliJ it works okay but in Linux it doesn't download .
final String relativePath = Paths.get(directory, UriUtils.decode(fileName, StandardCharsets.UTF_8)).toString();
final FileSystemResource res = new FileSystemResource(relativePath);
final String actualFileName = res.getFile().toPath().getFileName().toString();
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDisposition(ContentDisposition.builder("attachment").filename(actualFileName).build());
return new ResponseEntity<>(res, headers, HttpStatus.OK);
final String actualFileName = res.getFile().toPath().getFileName().toString();
I have tried also this line with
final String actualFileName = UriUtils.encode(res.getFile().toPath().getFileName().toString(), StandardCharsets.UTF_8)).toString()
Does anybody know why this problem is happening ?
question from:
https://stackoverflow.com/questions/65682390/java-spring-boot-download-file-with-filename-containing-special-characters-produ 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…