- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试记录一个 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);
}
}
);
}
提前致谢。
最佳答案
看起来缺少@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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!