added content_length to the response when downloading and added url encoding to the filename

This commit is contained in:
Rostislav Raykov
2024-10-17 23:29:41 +03:00
parent 75ecd54de6
commit 60a5533ddc

View File

@@ -16,6 +16,8 @@ import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
@@ -143,7 +145,9 @@ public class FileService {
try {
Resource resource = new UrlResource(outputFile.toUri());
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileEntity.name + "\"")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + URLEncoder.encode(fileEntity.name, StandardCharsets.UTF_8) + "\"")
.header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(resource.contentLength()))
.body(resource);
} catch (Exception e) {
logger.error("Error reading file: {}", e.getMessage());