【问题标题】:How can I specify a multipart file upload with params如何使用参数指定分段文件上传
【发布时间】:2019-07-04 02:35:27
【问题描述】:

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

这是我在 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,我会上传文件。

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

提前致谢。

【问题讨论】:

  • 你试过consumes = MediaType.MULTIPART_FORM_DATA_VALUE吗?
  • 我正在使用 micronaut,但这个常量不可用。但至于我检查这只是说“多部分/表单数据”的另一种方式,对吗?

标签: java swagger micronaut


【解决方案1】:

好像不见了@QueryValue

来自文档6.4 Simple Request Binding

来自请求 URI 变量或请求参数的绑定 | @QueryValue String myParam

来自文档6.19 File Uploads

方法设置为消耗MULTIPART_FORM_DATA

方法参数匹配表单属性名称。在这种情况下,文件将匹配例如

StreamingFileUpload.transferTo(java.lang.String) 方法用于将文件传输到服务器。

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")
                    }
                }
    }

}

【讨论】:

  • 嗨 tsarenkotxt,我按照你的建议试过了,但还是有同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 2014-08-08
  • 1970-01-01
  • 2018-08-09
  • 2018-03-07
  • 2021-04-15
  • 2013-11-17
相关资源
最近更新 更多