首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WebFlux:上传文件时出现问题

WebFlux:上传文件时出现问题
EN

Stack Overflow用户
提问于 2018-10-08 05:04:03
回答 2查看 2.8K关注 0票数 6

我正在开发一个使用webflux reactive client上传文件的客户端:

这是我的客户端代码:

代码语言:javascript
运行
复制
private Mono<String> postDocument(String authorization, InputStream content) {
    try {
        ByteArrayResource resource = new ByteArrayResource(IOUtils.toByteArray(content));
        return client.post().uri(DOCS_URI)
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .header(HttpHeaders.AUTHORIZATION, authorization)
                .body(BodyInserters.fromMultipartData("file", resource))
                .exchange()
                .flatMap(res -> readResponse(res, String.class));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

服务器端代码:

代码语言:javascript
运行
复制
    public Mono<ServerResponse> createDocument(ServerRequest request) {
    return request.body(toMultipartData())
            .flatMap(parts -> Mono.just((FilePart) parts.toSingleValueMap().get("file")))
            .flatMap(part -> {
                try {
                    String fileId = IdentifierFactory.getInstance().generateIdentifier();
                    File tmp = File.createTempFile(fileId, part.filename());
                    part.transferTo(tmp);
                    String documentId = IdentifierFactory.getInstance().generateIdentifier();
                    String env = request.queryParam("env")
                            .orElse("prod");
                    CreateDocumentCommand cmd = new CreateDocumentCommand(documentId, tmp, part.filename(), env);
                    return Mono.fromFuture(cmdGateway.send(cmd));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            })
            .flatMap(res -> ok().body(fromObject(res)));
}

我得到了这个错误:

代码语言:javascript
运行
复制
java.lang.ClassCastException: org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader$SynchronossPart cannot be cast to org.springframework.http.codec.multipart.FilePart
EN

Stack Overflow用户

发布于 2020-11-12 16:41:56

我有示例代码,但使用的是Kotlin。

我希望它能帮助你。

memory(节省内存)

代码语言:javascript
运行
复制
upload(stream: InputStream, filename: string): HashMap<String, Any> {
        val request: MultiValueMap<String, Any> = LinkedMultiValueMap();
        request.set("file", MultipartInputStreamFileResource(stream, filename))
        return client.post().uri("http://localhost:8080/files")
            .contentType(MediaType.MULTIPART_FORM_DATA)
            .body(BodyInserters.fromMultipartData(request))
            .awaitExchange().awaitBody()
}

class MultipartInputStreamFileResource(inputStream: InputStream?, private val filename: String) : InputStreamResource(inputStream!!) {
        override fun getFilename(): String? {
            return filename
        }

        @Throws(IOException::class)
        override fun contentLength(): Long {
            return -1 // we do not want to generally read the whole stream into memory ...
        }

}

代码语言:javascript
运行
复制
//You wrote a file to somewhere in the controller then send the file through webclient

upload(stream: InputStream, filename: string): HashMap<String, Any> {
        val request: MultiValueMap<String, Any> = LinkedMultiValueMap();
        request.set("file", FileSystemResource(File("/tmp/file.jpg")))
        return client.post().uri("http://localhost:8080/files")
            .contentType(MediaType.MULTIPART_FORM_DATA)
            .body(BodyInserters.fromMultipartData(request))
            .awaitExchange().awaitBody()
}

代码语言:javascript
运行
复制
upload(file: Part): HashMap<String, Any> {
        val request: MultiValueMap<String, Any> = LinkedMultiValueMap();
        request.set("file", file)
        return client.post().uri("http://localhost:8080/files")
            .contentType(MediaType.MULTIPART_FORM_DATA)
            .body(BodyInserters.fromMultipartData(request))
            .awaitExchange().awaitBody()
}
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52692898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档