gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:47 24 4
gpt4 key购买 nike

我正在尝试记录一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com