最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to upload and download document files (Excel, DOCX, PDF,...) to Cloudinary and get the url to view and download the c

programmeradmin3浏览0评论

I am using Cloudinary to upload document files (like Excel, DOCX, PDF) in my Java Spring Boot application. However, when uploading, the returned URL does not allow downloading the file in the correct format. Instead, it returns a file with no format or that cannot be viewed after dowload.

I use the uploadSingleFileToFolder method to upload the file:

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();
}

When I upload an Excel file (example.xlsx), the returned URL looks like this:

When accessing this URL, the browser downloads a file without a format (for example, example instead of example.xlsx).

I'm using free plan Cloudinary.

I have tried all those thing but still not working:

  • Used resource_type: "auto" in options
  • Checked returned URL (secure_url instead of url)
  • Checked file has correct content type before uploading

Do you guys have any

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论