使用Spring boot上传文件时出现HttpMediaTypeNotSupportedException

问题描述 投票:0回答:1

我在 Kotlin 应用程序中尝试使用 Spring WebFlux 上传文件时遇到问题(使用 kotlin 和 spring boot)。尽管正确配置了我的控制器来处理分段文件上传,我还是收到了 HttpMediaTypeNotSupportedException,其中包含消息 Content-Type 'image/jpeg' is not support.

这是我的设置:

1.控制器实现:

@RestController
@Validated
class S3Controller {
    private val logger = LoggerFactory.getLogger(S3Controller::class.java)

    @Autowired
    private lateinit var s3: S3Service

    companion object {
        const val UPLOAD_FILE = "/uploadFile"
    }

    @Value("\${minio.buckets.kyc-bucket-name}")
    private val portfoliosBucket: String = ""

    @PostMapping(UPLOAD_FILE, consumes = ["multipart/form-data", MediaType.ALL_VALUE])
    suspend fun uploadFile(@RequestPart("file") file: FilePart): ResponseEntity<String> {
        logger.info("Received file: ${file.filename()} for upload")
        return try {
            val fileName = file.filename()
            s3.upload(
                bucket = portfoliosBucket,
                fileName = fileName,
                folder = "portfolios",
                part = file
            )
            logger.info("File uploaded successfully: $fileName")
            ResponseEntity.ok("File uploaded successfully: $fileName")
        } catch (e: Exception) {
            logger.error("Failed to upload file: ${e.message}", e)
            ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to upload file: ${e.message}")
        }
    }

2.应用程序配置(application.yml):

spring:
  servlet:
    multipart:
      enabled: true
      max-file-size: 10MB

3.错误信息:

我在日志中收到以下警告:

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'image/jpeg' is not supported]
  1. 为什么我的应用程序拒绝 image/jpeg 内容类型?
  2. 我应该检查哪些附加配置以确保正确处理文件上传?
  3. 使用 Spring WebFlux 设置分段文件上传时是否存在我可能遗漏的常见陷阱?
spring-boot spring-mvc
1个回答
0
投票

我在 Spring Boot 应用程序中遇到的问题是,我的

spring-web-mvc
文件中同时包含
spring-web-flux
build.gradle.kts
依赖项。这两个依赖项似乎无法一起启用。为了解决此问题,我修改了我的应用程序属性文件中添加以下行
spring.main.web-application-type=reactive

© www.soinside.com 2019 - 2024. All rights reserved.