猿问

WebFlux:上传文件时出现问题

我正在开发一个客户端来使用 webflux 反应客户端上传文件:


这是我的客户端代码:


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

    }

}

服务器端代码:


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

}

我得到这个错误:


java.lang.ClassCastException: org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader$SynchronossPart cannot be cast to org.springframework.http.codec.multipart.FilePart



POPMUISE
浏览 538回答 2
2回答

幕布斯7119047

我有示例代码,但在 Kotlin 中。我希望它能帮助你。第一种方式(节省内存)upload(stream: InputStream, filename: string): HashMap<String, Any> {&nbsp; &nbsp; &nbsp; &nbsp; val request: MultiValueMap<String, Any> = LinkedMultiValueMap();&nbsp; &nbsp; &nbsp; &nbsp; request.set("file", MultipartInputStreamFileResource(stream, filename))&nbsp; &nbsp; &nbsp; &nbsp; return client.post().uri("http://localhost:8080/files")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .contentType(MediaType.MULTIPART_FORM_DATA)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .body(BodyInserters.fromMultipartData(request))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .awaitExchange().awaitBody()}class MultipartInputStreamFileResource(inputStream: InputStream?, private val filename: String) : InputStreamResource(inputStream!!) {&nbsp; &nbsp; &nbsp; &nbsp; override fun getFilename(): String? {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return filename&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Throws(IOException::class)&nbsp; &nbsp; &nbsp; &nbsp; override fun contentLength(): Long {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1 // we do not want to generally read the whole stream into memory ...&nbsp; &nbsp; &nbsp; &nbsp; }}第二种方式//You wrote a file to somewhere in the controller then send the file through webclientupload(stream: InputStream, filename: string): HashMap<String, Any> {&nbsp; &nbsp; &nbsp; &nbsp; val request: MultiValueMap<String, Any> = LinkedMultiValueMap();&nbsp; &nbsp; &nbsp; &nbsp; request.set("file", FileSystemResource(File("/tmp/file.jpg")))&nbsp; &nbsp; &nbsp; &nbsp; return client.post().uri("http://localhost:8080/files")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .contentType(MediaType.MULTIPART_FORM_DATA)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .body(BodyInserters.fromMultipartData(request))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .awaitExchange().awaitBody()}第三种方式upload(file: Part): HashMap<String, Any> {&nbsp; &nbsp; &nbsp; &nbsp; val request: MultiValueMap<String, Any> = LinkedMultiValueMap();&nbsp; &nbsp; &nbsp; &nbsp; request.set("file", file)&nbsp; &nbsp; &nbsp; &nbsp; return client.post().uri("http://localhost:8080/files")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .contentType(MediaType.MULTIPART_FORM_DATA)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .body(BodyInserters.fromMultipartData(request))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .awaitExchange().awaitBody()}

跃然一笑

这对我有用MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();bodyBuilder.part("file", new ClassPathResource("/bulk_upload/test.xlsx"));List<DataFileAdjustment> list = webClient.post().uri("/bulk").accept(MediaType.APPLICATION_JSON)&nbsp; &nbsp; &nbsp; &nbsp; .body(BodyInserters.fromMultipartData(bodyBuilder.build()))&nbsp; &nbsp; &nbsp; &nbsp; .exchange()&nbsp; &nbsp; &nbsp; &nbsp; .expectStatus().isOk()
随时随地看视频慕课网APP

相关分类

Java
我要回答