ChunkedInput 在球衣中不起作用
ChunkedInput not working in jersey
任何人都可以帮助我为什么 java 代码有问题并一次打印所有数据而不是将每个块打印为 java 脚本代码
Java代码:
import org.glassfish.jersey.client.ChunkedInput;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
public class RunClient {
public static void main(String args[]) throws InterruptedException {
Client client = ClientBuilder.newClient();
//2 is to increase amount of data and 3(seconds) is for time b/w chunked output ,can be changed
final Response response = client.target("http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/2/3").request()
.get();
final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() {
});
String chunk;
while ((chunk = chunkedInput.read()) != null) {
System.err.println("Next chunk received: " );
System.out.println(chunk);
}
}
}
Java脚本:(打开页面 http://jerseyexample-ravikant.rhcloud.com/rest/jws,然后在控制台中按下 F12 和 运行,因为 java不允许从其他域调用脚本)
//2 is to increase amount of data and 3(seconds) is for time b/w chunked output ,can be changed
var xhr = new XMLHttpRequest()
xhr.open("GET", "http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/2/3", true)
xhr.onprogress = function () {
console.log("PROGRESS:", xhr.responseText) ;console.log("\n");
}
xhr.send()
编辑:仅供帮助,它也可以正常工作 java 连接
String uri = "http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/3/1";
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
我的网络服务代码
@Path("streaming/{param}/{sleepTime}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public ChunkedOutput<String> getChunkedStream(@PathParam("param") String loopcount,@PathParam("sleepTime") String sleepTime) throws Exception {
final ChunkedOutput<String> output = new ChunkedOutput<>(String.class);
final Integer val=Integer.parseInt(loopcount);
final Integer isleepTime=Integer.parseInt(sleepTime)*1000;
new Thread(new Runnable() {
@Override
public void run() {
try {
StringBuffer chunk = null;
for (int i = 0; i < 10; i++) {
chunk = new StringBuffer();
for (int j = 0; j < val; j++) {
chunk.append(" Message #" + i+ "#"+j);
}
output.write(chunk.toString()+"\n");
System.out.println("write");
Thread.sleep(isleepTime);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
System.out.println("output.close();");
output.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}).start();
return output;
}
形成球衣文档:
使用 ChunkedOutput 写入块很简单,您只需调用方法 write() 即可将一个块写入输出。对于输入读数,它稍微复杂一些。除非开发人员告知,否则 ChunkedInput 不知道如何区分字节流中的块。为了定义自定义块边界,ChunkedInput 提供了注册 ChunkParser 的可能性,它从输入流中读取块并将它们分开。 Jersey 提供了几个块解析器实现,如果需要,您可以实现自己的解析器来分离您的块。在我们上面的示例中,使用了 Jersey 提供的默认解析器,它根据 \r\n 分隔字符序列 .
的存在来分隔块
因此您的服务器必须使用 \r\n 来分隔块,或者您必须注册一个 ChunkParser。
假设您有一个常量来完成每个块,您可以尝试:
Client client = ClientBuilder.newClient();
//2 is to increase amount of data and 3(seconds) is for time b/w chunked output ,can be changed
final Response response = client.target("http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/2/3").request()
.get();
final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() {
});
chunkedInput.setParser(ChunkedInput.createParser(BOUNDARY));
String chunk;
while ((chunk = chunkedInput.read()) != null) {
System.err.println("Next chunk received: " );
System.out.println(chunk);
}
虽然 BOUNDARY 是每个块的最终字符串。
您编辑中的 in.readLine 解决方案将按每个换行符分解 "chunks",即使一个块包含一个 \n,它也会被解释为 2 个块。
任何人都可以帮助我为什么 java 代码有问题并一次打印所有数据而不是将每个块打印为 java 脚本代码
Java代码:
import org.glassfish.jersey.client.ChunkedInput;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
public class RunClient {
public static void main(String args[]) throws InterruptedException {
Client client = ClientBuilder.newClient();
//2 is to increase amount of data and 3(seconds) is for time b/w chunked output ,can be changed
final Response response = client.target("http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/2/3").request()
.get();
final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() {
});
String chunk;
while ((chunk = chunkedInput.read()) != null) {
System.err.println("Next chunk received: " );
System.out.println(chunk);
}
}
}
Java脚本:(打开页面 http://jerseyexample-ravikant.rhcloud.com/rest/jws,然后在控制台中按下 F12 和 运行,因为 java不允许从其他域调用脚本)
//2 is to increase amount of data and 3(seconds) is for time b/w chunked output ,can be changed
var xhr = new XMLHttpRequest()
xhr.open("GET", "http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/2/3", true)
xhr.onprogress = function () {
console.log("PROGRESS:", xhr.responseText) ;console.log("\n");
}
xhr.send()
编辑:仅供帮助,它也可以正常工作 java 连接
String uri = "http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/3/1";
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
我的网络服务代码
@Path("streaming/{param}/{sleepTime}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public ChunkedOutput<String> getChunkedStream(@PathParam("param") String loopcount,@PathParam("sleepTime") String sleepTime) throws Exception {
final ChunkedOutput<String> output = new ChunkedOutput<>(String.class);
final Integer val=Integer.parseInt(loopcount);
final Integer isleepTime=Integer.parseInt(sleepTime)*1000;
new Thread(new Runnable() {
@Override
public void run() {
try {
StringBuffer chunk = null;
for (int i = 0; i < 10; i++) {
chunk = new StringBuffer();
for (int j = 0; j < val; j++) {
chunk.append(" Message #" + i+ "#"+j);
}
output.write(chunk.toString()+"\n");
System.out.println("write");
Thread.sleep(isleepTime);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
System.out.println("output.close();");
output.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}).start();
return output;
}
形成球衣文档:
使用 ChunkedOutput 写入块很简单,您只需调用方法 write() 即可将一个块写入输出。对于输入读数,它稍微复杂一些。除非开发人员告知,否则 ChunkedInput 不知道如何区分字节流中的块。为了定义自定义块边界,ChunkedInput 提供了注册 ChunkParser 的可能性,它从输入流中读取块并将它们分开。 Jersey 提供了几个块解析器实现,如果需要,您可以实现自己的解析器来分离您的块。在我们上面的示例中,使用了 Jersey 提供的默认解析器,它根据 \r\n 分隔字符序列 .
的存在来分隔块因此您的服务器必须使用 \r\n 来分隔块,或者您必须注册一个 ChunkParser。
假设您有一个常量来完成每个块,您可以尝试:
Client client = ClientBuilder.newClient();
//2 is to increase amount of data and 3(seconds) is for time b/w chunked output ,can be changed
final Response response = client.target("http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/2/3").request()
.get();
final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() {
});
chunkedInput.setParser(ChunkedInput.createParser(BOUNDARY));
String chunk;
while ((chunk = chunkedInput.read()) != null) {
System.err.println("Next chunk received: " );
System.out.println(chunk);
}
虽然 BOUNDARY 是每个块的最终字符串。 您编辑中的 in.readLine 解决方案将按每个换行符分解 "chunks",即使一个块包含一个 \n,它也会被解释为 2 个块。