我在 Kotlin 应用程序中尝试使用 Spring WebFlux 上传文件时遇到问题(使用 kotlin 和 spring boot)。尽管正确配置了我的控制器来处理分段文件上传,我还是收到了 HttpMediaTypeNotSupportedException,其中包含消息 Content-Type 'image/jpeg' is not support.
@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}")
}
}
spring:
servlet:
multipart:
enabled: true
max-file-size: 10MB
我在日志中收到以下警告:
Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'image/jpeg' is not supported]
我在 Spring Boot 应用程序中遇到的问题是,我的
spring-web-mvc
文件中同时包含 spring-web-flux
和 build.gradle.kts
依赖项。这两个依赖项似乎无法一起启用。为了解决此问题,我修改了我的应用程序属性文件中添加以下行 spring.main.web-application-type=reactive