java - 如何使用参数指定分段文件上传

标签 java swagger micronaut

我正在尝试记录一个 API 方法,该方法将接收一个文件和两个 int 参数。使用 swagger 编辑器,我能够描述我想要的内容,但无法使用注释来复制它。

what I want

这是我在 swagger 编辑器上画的

requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                flow:
                  type: integer
                environment:
                  type: integer
                file:
                  type: string
                  format: binary
        required: true

如果我使用 consumes = MediaType.MULTIPART_FORM_DATA 我会得到参数。如果我使用 consumes = MediaType.APPLICATION_OCTET_STREAM 我就可以上传文件。

failed attemp 1 failed attemp 2

@Operation(summary = "Unpack Files",
            description = "Receives a packed zip or gzip file with xml files inside or receives xml files",
            security = @SecurityRequirement(name = "apiKey"),
            responses = {
                    @ApiResponse(responseCode = "201", description = "Created"),
                    @ApiResponse(responseCode = "400", description = "Something Went Wrong"),
                    @ApiResponse(responseCode = "401", description = "Unauthorized"),
                    @ApiResponse(responseCode = "503", description = "Service Unavailable")
            },
            requestBody = @RequestBody(
                content = @Content(
                    mediaType = MediaType.MULTIPART_FORM_DATA,
                    schema = @Schema(implementation = Document.class, format = "binary"),
                    encoding = @Encoding(
                            name = "file",
                            contentType = "application/xml, application/zip, application/gzip"
                    )
                ),
                required = true
            )
    )
    @Post(value = "/unpack", consumes = MediaType.APPLICATION_OCTET_STREAM)
    public Single<HttpResponse<String>> upload(StreamingFileUpload file, int flow, int environment) throws IOException {
        return Single.just(new Document(file.getFilename(), environment, flow))
            .flatMap(DocumentValidation::validateDocumentExtension)
            .doOnError(throwable -> {
                log.error("Validation exception: {}", throwable.getMessage());
                exception = throwable.getMessage();
            })
            .doOnSuccess(doc -> {
                log.info("File saved successfuly");
                File tempFile = File.createTempFile(file.getFilename(), "temp");
                file.transferTo(tempFile);
            })
            .map(success -> {
                if (exception != null || !exception.equals("")) {
                    return HttpResponse.<String>status(HttpStatus.CREATED).body("Uploaded");
                } else {
                    return HttpResponse.<String>status(HttpStatus.SERVICE_UNAVAILABLE).body(exception);
                }
            }
        );
    }

提前致谢。

最佳答案

看起来缺少@QueryValue

来自文档 6.4 Simple Request Binding :

Bindings from a request URI variable or request parameter | @QueryValue String myParam

来自文档 6.19 File Uploads :

The method is set to consume MULTIPART_FORM_DATA

The method parameters match form attribute names. In this case the file will match for example an

The StreamingFileUpload.transferTo(java.lang.String) method is used to transfer the file to the server.

Kotlin 简单:

@Controller
class SomeController {

    @Post(value = "/", consumes = [MediaType.MULTIPART_FORM_DATA])
    fun upload(file: StreamingFileUpload,
               @QueryValue flow: Int,
               @QueryValue environment: Int): Single<HttpResponse<String>> {
        val tempFile = File.createTempFile(file.filename, "temp")
        return Single.fromPublisher(file.transferTo(tempFile))
                .map { success ->
                    if (success) {
                        HttpResponse.ok("Uploaded");
                    } else {
                        HttpResponse.status<String>(HttpStatus.CONFLICT)
                                .body("Upload Failed")
                    }
                }
    }

}

关于java - 如何使用参数指定分段文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56879969/

相关文章:

spring-mvc - Swagger for SpringMVC-Springfox的替代品?

swagger - 如何将 swagger 2.0 JSON 文件分解为多个模块

java - 如何在 Micronaut 中下载(流式传输)大型(生成)文件

java - micronaut 安全中的自定义 JWT 过期

java - 如何分析远程运行的 headless (headless) Java 应用程序?

java - 使用构建器模式添加到 Set<>

c# - 使用 Swagger 在 ASP.NET Core 中实现 OAuth

java - JSR 303 : How to Validate a Collection of annotated objects?

java - 单击事件在java中的selenium webdriver中不起作用?

java - Micronaut 中的多个身份验证提供程序