使用Webflux和Spring Boot搭配Google Cloud Storage上传MultiPart文件时出现UnsupportedOperationException

时间:2019-03-19 17:30:01

标签: java spring spring-boot spring-webflux

FileSystem中使用备用FilePart.transferTo时,我发现在分段文件上传期间抛出了UnsupportedOperationException

在处理数据时,Spring会使用SynchronossFilePart实现切换到另一个FileSystemProvider。有什么方法可以防止这种情况并迫使Spring仅使用所需的FileSystem?

下面提供了一个代码段,演示了使用google-cloud-nio作为文件系统的问题,该文件系统试图将上传的文件持久保存到google云存储中。

我正在使用Spring Boot 2.1.3

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Mono<Void> multipartUpload( final @RequestPart("file") FilePart filePart
    ) {

        FileSystem fileSystem = CloudStorageFileSystem.forBucket("my-bucket");

        // Code snippet that demonstrates connectivity to GCP is okay
        //try {
        //    Files.write(fileSystem.getPath("successful-file"), "Just here to prove this works, this data is written to the bucket successfully, can be removed".getBytes());
        //} catch (IOException e) {
        //    e.printStackTrace();
        //}
        return filePart.transferTo(fileSystem.getPath("failed-file"));
    }

相关Stacktrace:

java.lang.UnsupportedOperationException: null
    at java.base/java.nio.file.spi.FileSystemProvider.newFileChannel(FileSystemProvider.java:524)
    at java.base/java.nio.channels.FileChannel.open(FileChannel.java:292)
    at java.base/java.nio.channels.FileChannel.open(FileChannel.java:345)
    at org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader$SynchronossFilePart.transferTo(SynchronossPartHttpMessageReader.java:334)

1 个答案:

答案 0 :(得分:0)

CloudStorageFileSystemProvider扩展了Java nio类FileSystemProvider,该类是异常的起源。

CloudStorageFileSystemProvider不会覆盖该类状态的默认行为的newFileChannel方法和documentation方法:

  

需要默认提供程序来支持文件的创建   渠道。当不被覆盖时,默认实现抛出   UnsupportedOperationException。

这似乎是google-cloud-nio库的问题,尽管有可能可以改进webflux的实现,以便不依赖于其他提供程序缺少的实现。

更新: 似乎google-cloud-nio自{0.8} -alpha版本起已经released实现了newFileChannel方法,该方法应该可以解决此问题

相关问题