Я использую облачную загрузку файлов документов (например, Excel, Docx, PDF) в моем приложении Java Spring Boot. Однако при загрузке возвращенный URL не позволяет загружать файл в правильном формате. Вместо этого он возвращает файл без формата или который не может быть просмотр после Dowload.public String uploadSingleFileToFolder(MultipartFile file, String folderName) throws IOException, AppException {
if (file.getSize() > maxFileSize) {
throw new AppException("File size exceeds the maximum allowed size.");
}
if (!isContentTypeAllowed(file.getContentType())) {
throw new AppException("Unsupported file type: " + file.getContentType());
}
// Get the original filename
String originalFilename = file.getOriginalFilename();
if (originalFilename == null) {
throw new AppException("Invalid file name.");
}
// Remove the file extension for public_id
String filenameWithoutExtension = originalFilename.replaceAll("\\.[^.]+$", "");
// Upload the file with the correct resource_type
var options = ObjectUtils.asMap(
"folder", folderName,
"public_id", filenameWithoutExtension,
"use_filename", true,
"unique_filename", false,
"resource_type", "auto" // Set resource_type to "raw" for documents
);
var uploadResult = cloudinary.uploader().upload(file.getBytes(), options);
return uploadResult.get("secure_url").toString();
}
< /code>
Когда я загружаю файл Excel (example.xlsx), возвращаемый URL выглядит следующим образом: < /p>
https: //res.cloudinary.com/dndoreidh/raw/upload/v1740554207/project-document/example
< /blockquote>
При доступе к этому URL -адресу браузер загружает файл без формата (например, пример вместо примера.xlsx). < /p>
i 'm Используя бесплатный план облачный. «Авто» в параметрах
[*] Проверено возвращаемое URL (secure_url вместо URL)
[*] Проверенный файл имеет правильный тип контента перед загрузкой
У вас, ребята, есть
Подробнее здесь: https://stackoverflow.com/questions/794 ... oudinary-a