Java Jersey 2 客户端如何将输入/输出二进制流发送到服务器和 Vise Versa
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23701106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Jersey 2 Client can send Input/Output binary stream to Server and Vise Versa
提问by Débora
I am new to Jersey. I have 2 things to know. 1. Send domain objects from client to server 2. Send domain Objects to client from server.
我是泽西岛的新手。我有两件事要知道。1. 从客户端向服务器发送域对象 2. 从服务器向客户端发送域对象。
I want to send my custom Objects from client to server application. Since types of objects that are to be sent can be differ (it can be a domain object, File or image ), I supposed to convert those objects to stream and send to server. Also with the stream, I need to send send some parameters as well. Then I need to retrieve the stream in server and process it.
Once domain objects convert into stream, then they should be sent to the client as well.
I use Jersey 2.8 . Java 8 . Tomcate 6 . Here is how I tried to do it, but it fails(Might be a wrong approach)
我想将我的自定义对象从客户端发送到服务器应用程序。由于要发送的对象类型可能不同(它可以是域对象、 File 或 image ),我应该将这些对象转换为流并发送到服务器。同样对于流,我还需要发送一些参数。然后我需要在服务器中检索流并处理它。
一旦域对象转换为流,它们也应该被发送到客户端。
我使用泽西 2.8 。爪哇 8。托克特 6 . 这是我尝试这样做的方法,但失败了(可能是错误的方法)
Here is my client:
这是我的客户:
InputStream returnStrem = (InputStream)client.target("http://localhost:8080/TestJerseyProject")
.path("rest/save")
.request(new MediaType[] {MediaType.APPLICATION_OCTET_STREAM_TYPE})
.get(InputStream.class);
try {
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(returnStrem));
Object o= ois.readObject();
System.out.println("Entity : "+o );
}catch(Exception e){
e.printStackTrace();
}finally {
returnStrem.close();
}
The server side code :
服务器端代码:
@Path("/cache")
public class ObjectCacheAction {
@GET
@Consumes("application/octet-stream")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response streamExample(InputStream is) {
try {
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(is));
Object o= ois.readObject();
System.out.println("Entity : "+o );
is.close();
}catch(Exception e){
e.printStackTrace();
}finally {
}
StreamingOutput stream = new StreamingOutput() {
public void write(OutputStream os) throws IOException,
WebApplicationException {
try {
MyObj m=new MyObj();//My Domain Object
m.setName("sdfsdf");
ObjectOutputStream oos1 = new ObjectOutputStream(os);
oos1.writeObject(m);
oos1.flush();
oos1.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
return Response.ok(stream).build();
}
}
May be my approach is wrong. How ever, please let me know how I can I do this with working code sample. I tried over Internet, but most of them are Jersey 1.X.
可能是我的方法不对。但是,请让我知道如何使用工作代码示例来做到这一点。我通过互联网尝试过,但大多数都是 Jersey 1.X。
采纳答案by Débora
This Questionand its answers solved my problem. In the answers, InputStreams were used. They worked well, but I had to do some modifications to make them work for Jersey 2.8. Also, Instead of stream, direct byte [] also can be used. I tested and it worked well. Big thank to Martin Wilson and sikrip.
这个问题及其答案解决了我的问题。在答案中,使用了 InputStreams。它们运行良好,但我必须做一些修改才能使它们适用于 Jersey 2.8。此外,也可以使用直接字节 [] 代替流。我测试过,效果很好。非常感谢 Martin Wilson和sikrip。
回答by TecOpen
InputStream returnStrem = (InputStream)client.target("http://localhost:8080/TestJerseyProject")
.path("rest/save")
.request(new MediaType[] {MediaType.APPLICATION_OCTET_STREAM_TYPE})
.get(InputStream.class);
With the last statement you are actually asking to collect an instance of InputStream where, in fact, you should be asking for the object you are expecting to receive. From the Server code it seems that you should be requesting a MyObj.class so the client code would actually be more like
在最后一条语句中,您实际上是要求收集 InputStream 的一个实例,实际上,您应该要求的是您希望接收的对象。从服务器代码看来,您应该请求 MyObj.class,因此客户端代码实际上更像是
MyObj myObj = client.target("http://localhost:8080/TestJerseyProject")
.path("rest/save")
.request(new MediaType[] {MediaType.APPLICATION_OCTET_STREAM_TYPE})
.get(MyObj.class);
I am unfamiliar with using streams in this fashion so you may have to tweak it...
我不熟悉以这种方式使用流,因此您可能需要对其进行调整...