Java 使用接口的Restlet客户端-服务器流

Java 使用接口的Restlet客户端-服务器流,java,client-server,restlet,Java,Client Server,Restlet,我想做一些类似的事情,但有一点不同: 我希望使用接口来流式传输数据,而不是使用基本类型。 我想在客户端和服务器之间定义某种接口,在它们之间传输数据,并让restlet无缝地处理数据传输 我心目中的例子: interface Streaming { InputStream startStream(String streamId); } 当客户端调用调用时,它开始从inputstream读取数据。服务器接收调用并通过创建inputstream(例如,视频文件或一些原始数据)开始提供流。Rest

我想做一些类似的事情,但有一点不同:

我希望使用接口来流式传输数据,而不是使用基本类型。

我想在客户端和服务器之间定义某种接口,在它们之间传输数据,并让restlet无缝地处理数据传输

我心目中的例子:

interface Streaming {
  InputStream startStream(String streamId);
}
当客户端调用调用时,它开始从inputstream读取数据。服务器接收调用并通过创建inputstream(例如,视频文件或一些原始数据)开始提供流。Restlet应该从服务器端的inputstream读取数据,并在客户端以inputstream的形式提供数据


有什么办法可以做到这一点吗?一个代码示例或链接就好了。谢谢。

这不能完全解决您的问题,但一种方法是创建一个线程,使用ReadableRepresentation和管道将数据流回到客户端

创建管道:

Pipe pipe = Pipe.open();
创建如下所示的表示:

ReadableRepresentation r = new ReadableRepresentation(pipe.source(), mediatype);
pipe.sink().write(ByteBuffer.wrap(someBytes));
启动一个单独的线程,将成批字节写入管道,如下所示:

ReadableRepresentation r = new ReadableRepresentation(pipe.source(), mediatype);
pipe.sink().write(ByteBuffer.wrap(someBytes));

将表示返回到客户端。

不确定这是否能完全解决您的问题,但一种方法是创建一个线程,使用ReadableRepresentation和管道将数据流回到客户端

创建管道:

Pipe pipe = Pipe.open();
创建如下所示的表示:

ReadableRepresentation r = new ReadableRepresentation(pipe.source(), mediatype);
pipe.sink().write(ByteBuffer.wrap(someBytes));
启动一个单独的线程,将成批字节写入管道,如下所示:

ReadableRepresentation r = new ReadableRepresentation(pipe.source(), mediatype);
pipe.sink().write(ByteBuffer.wrap(someBytes));

将表示返回给客户端。

以下是我迄今所学的示例代码-一个具有流媒体功能的接口和一个客户端-服务器流媒体示例

我还没有向界面添加参数,只是下载,还没有上传

界面:

public interface DownloadResource {
    public ReadableRepresentation download();
}
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));
public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}
与协议的接口:(逻辑与技术的分离):

客户端:

public interface DownloadResource {
    public ReadableRepresentation download();
}
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));
public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}
服务器:

public interface DownloadResource {
    public ReadableRepresentation download();
}
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));
public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}
配置:

public interface DownloadResource {
    public ReadableRepresentation download();
}
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));
public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}

下面是我到目前为止所学的示例代码-一个具有流媒体功能的界面和一个客户端-服务器流媒体示例

我还没有向界面添加参数,只是下载,还没有上传

界面:

public interface DownloadResource {
    public ReadableRepresentation download();
}
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));
public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}
与协议的接口:(逻辑与技术的分离):

客户端:

public interface DownloadResource {
    public ReadableRepresentation download();
}
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));
public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}
服务器:

public interface DownloadResource {
    public ReadableRepresentation download();
}
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));
public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}
配置:

public interface DownloadResource {
    public ReadableRepresentation download();
}
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/");
cr.setRequestEntityBuffering(true);
DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class);
// Remote invocation - seamless:
Representation representation = downloadResource.download();
// Using data:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(representation.getStream(), byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.i("Byte array: " + Arrays.toString(byteArray));
public class DownloadResourceImpl extends ServerResource implements DownloadResourceProtocol {
    @Override
    public ReadableRepresentation download() {
        InputStreamChannel inputStreamChannel;
        try {
            inputStreamChannel = new InputStreamChannel(new ByteArrayInputStream(new byte[]{1,2,3,4,5,6,7,8,9,10}));
            return new ReadableRepresentation(inputStreamChannel, MediaType.ALL);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
public class SampleApplication extends Application {
    @Override
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/download/", DownloadResourceImpl.class);
        return router;
    }
}

看看ReadableRepresentation。我做了一些类似的事情,将巨大的CSV文件发送回服务器,因为它们是建立在服务器上的。我不知道这是否可行。我很高兴有一个流式处理的客户机-服务器java示例(ReadableRepresentation?)-使用goole找不到任何示例:(看ReadableRepresentation。我做过类似的事情,将构建在服务器上的巨大CSV文件发送回。我不知道这是否可行。我很高兴有一个流式处理的客户机-服务器java示例(ReadableRepresentation?)-无法使用goole找到任何内容:(我正在尝试创建一个基于流的客户端-服务器接口。我不知道如何使用ReadableRepresentation实现它。该接口是什么样子的?我真的不知道你现在的意思。你是在尝试实现服务器、客户端还是两者都实现?我的答案是关于如何使用RESTlet表示从RESTlet服务器流数据。客户端和服务器-使用相同的java接口-使连接无缝。我正在尝试创建基于流的客户端-服务器接口。我不知道如何使用ReadableRepresentation实现它。该接口是什么样子的?我真的不确定你现在指的是什么。你是在尝试实现服务器、客户端还是两者都实现?我的answer是关于如何使用RESTlet表示从RESTlet服务器流式传输数据。客户端和服务器使用相同的java接口,使连接无缝。