java - 如何使用 RSocket 创建文件发送客户端/服务器?

标签 java download file-sharing aeron rsocket

我似乎无法在 RSocket 上找到任何资源/教程,除了阅读他们在 GitHub 上的代码,我不明白。

我的服务器上有一个文件路径:String serverFilePath;

我希望能够从我的客户端下载它(最好使用 RSocket's Aeron implementation )。有谁知道如何使用 RSocket 做到这一点?

提前致谢。

最佳答案

我在 RSocket 上工作,并编写了大部分 java 版本,包括 Aeron 传输。

我目前不建议使用 Aeron 实现。您可以通过多种方式发送文件:

  1. 使用 requestChannel 将数据推送到远程服务器。
  2. 使用 requestChannel 或 requestStream 将字节流式传输到客户端。

这是一个使用 requestStream 的例子:

  public class FileCopy {

  public static void main(String... args) throws Exception {

    // Create a socket that receives incoming connections
    RSocketFactory.receive()
        .acceptor(
            new SocketAcceptor() {
              @Override
              // Create a new socket acceptor
              public Mono<RSocket> accept(ConnectionSetupPayload setup, RSocket sendingSocket) {
                return Mono.just(
                    new AbstractRSocket() {
                      @Override
                      public Flux<Payload> requestStream(Payload payload) {
                        // Get the path of the file to copy
                        String path = payload.getDataUtf8();
                        SeekableByteChannel _channel = null;

                        try {
                          _channel = Files.newByteChannel(Paths.get(path), StandardOpenOption.READ);
                        } catch (IOException e) {
                          return Flux.error(e);
                        }

                        ReferenceCountUtil.safeRelease(payload);

                        SeekableByteChannel channel = _channel;
                        // Use Flux.generate to create a publisher that returns file at 1024 bytes
                        // at a time
                        return Flux.generate(
                            sink -> {
                              try {
                                ByteBuffer buffer = ByteBuffer.allocate(1024);
                                int read = channel.read(buffer);
                                buffer.flip();
                                sink.next(DefaultPayload.create(buffer));

                                if (read == -1) {
                                  channel.close();
                                  sink.complete();
                                }
                              } catch (Throwable t) {
                                sink.error(t);
                              }
                            });
                      }
                    });
              }
            })
        .transport(TcpServerTransport.create(9090))
        .start()
        .subscribe();

    String path = args[0];
    String dest = args[1];

    // Connect to a server
    RSocket client =
        RSocketFactory.connect().transport(TcpClientTransport.create(9090)).start().block();

    File f = new File(dest);
    f.createNewFile();

    // Open a channel to a new file
    SeekableByteChannel channel =
        Files.newByteChannel(f.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);

    // Request a stream of bytes
    client
        .requestStream(DefaultPayload.create(path))
        .doOnNext(
            payload -> {
              try {
                // Write the bytes received to the new file
                ByteBuffer data = payload.getData();
                channel.write(data);

                // Release the payload
                ReferenceCountUtil.safeRelease(payload);
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
            })
        // Block until all the bytes are received
        .blockLast();

    // Close the file you're writing too
    channel.close();
  }
}

关于java - 如何使用 RSocket 创建文件发送客户端/服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50061488/

相关文章:

android - 当用户从浏览器下载文件时,如何启动我的应用程序?

Java下载到目标(找不到目标)和0字节文件下载

iphone - UIFileSharingEnabled 只保存文件

c++ - sh_none 不是 'std::basic_filebuf<_Elem,_Traits>' 的成员

java - 根据从 struts2 应用程序中的下拉列表中选择的值填充表

java - 谷歌应用引擎 : Separating test and production version of cloud endpoints

http - 通过 TcpClient 从 HTTP 服务器下载文件

java - 如何动态创建多个 JSON 对象?

java - 点击后在Android应用程序中传递部分链接

c# - 如何获取Box的刷新 token